1.
public class Example1 {
int i=1000;
public static void main(String[] args) {
System.out.println("Mr. "+args[1]+"get salary "+i);
//compile time error:saying
//non static variable i can not be referenced from static area
}
}
2.class Base
{
public void m1()
{
}
}
class Example2 extends Base{
protected void m1()//m1() in Example2 can not m1() in Base
//attempting to assign weaker access privileges;was public
{
}
}
3.class B
{
final private void m1()
{
}
}
class Example3 extends B{
void m1()
{
//no error because private methods are not inherited
}
}
4.public class Example4 {
public static int m1(boolean b)
{
try
{
if(b)
{
return 1;
}
return 0;
}
finally
{
System.out.println("om sai");
}
}
public static void main(String[] args) {
m1(true);
}
//output: om sai
}
5.public class Example5 {
Example5() {
this(10,10);
//consrtuctor Example5 in class Example can not be applied to given types
//required: no argument
//found:int,int
}
public static void main(String[] args) {
Example5 e=new Example5();
}
}
6.class Ba
{
void fr(int x)
{
if(x>10)
{
System.out.println("super : "+x);
}
else
fr(++x);
}
}
class Example6 extends Ba{
void fr(int x)
{
if(x>10)
{
System.out.println("sub : "+x);
}
else
super.fr(x);
}
public static void main(String[] args) {
Example6 ex=new Example6();
ex.fr(10);
}
}
//output: sub 11
7.class Bas
{
void m1()
{
System.out.println("base class");
}
}
class Example7 extends Bas {
void m1()
{
System.out.println("derived class");
}
public static void main(String[] args) {
Example7 e=new Example7();
Bas b= (Bas)e;
b.m1();
}
//output:derived class
}
upto here perfect questions
last one just guess
8.public class Example9 {
int x;
public static void main(String[] args) {
int x=10;
Example9 e=new Example9();
e.m1(x);
System.out.println("x"+x);
}
public void m1(int y)
{
x=y*y;
}
}
//output: 10