JavaQuizzes

Multiple Choice Question

Given:

public class ExceptionCheck {
	
	static int i = 10;
	
	public static void main (String[] argds)
	{
		System.out.println(myMethod());
		System.out.println(i);
	}
	
	public static int myMethod()
	{
		try{
			throw new Exception();
		}
		catch(Exception e)
		{
			return i;
		}
		finally{
			i = 12;
            return i;
		}
	}
}

 

Select 1 option

A. Compilation Fail

B. Print 10 and 12.

C. Print 10 and 10.

D. Print 12 and 12.

Multiple Choice Question

Given:

public class ExceptionCheck {
	
	static int i = 10;
	
	public static void main (String[] argds)
	{
		System.out.println(myMethod());
		System.out.println(i);
	}
	
	public static int myMethod()
	{
		try{
			throw new Exception();
		}
		catch(Exception e)
		{
			return i;
		}
		finally{
			i = 12;
            return i;
		}
        return i;
	}
}

 

Select 1 option

A. Compilation Fail

B. Print 10 and 12.

C. Print 10 and 10.

D. Print 12 and 12.

Multiple Choice Question

Given

class TestClass{

public static void main(String[] args){

A a = new A();

B b = new B();

};

}

class A implements T1, T2{}

class B extends A implements T1{}

interface T1 { }

interface T2 { }

Select 4 options

A. (a instanceof T1) will return true.

B. (a instanceof T2) will return true.

C. (b instanceof T1) will return true.

D. (b instanceof T2) will return true.

E. (b instanceof A) will return false.

Multiple Choice Question

What will be the result of compiling and running the following code?

Select 1 option

class Base{

public short getValue(){ return 1; } //1

}

class Base2 extends Base{

public byte getValue(){ return 2; } //2

}

public class TestClass{

public static void main(String[] args){

Base b = new Base2();

System.out.println(b.getValue()); //3

}

}

A. It will print 1

B. It will print 2.

C. Compile time error at //1

D. Compile time error at //2

E. Compile time error at //3

Multiple Choice Question

Consider the following code:

class Super { static String ID = "SUPER"; }

class Sub extends Super{

static { System.out.print("In Sub"); }

}

public class Test{

public static void main(String[] args){

System.out.println(Sub.ID);

}

}

What will be the output when class Test is run?

 

Select 1 option

A. It will print In Sub and SUPER.

B. It will print SUPER.

C. Depends on the implementation of JVM.

D. It will not even compile.

E. None of the above.

Multiple Choice Question

Given:

interface I {
}

class A implements I {
}

class B extends A {
}

class C {
}

public class TestClass1 {
	public static void main(String[] args) {
		I i = new B();
		B b = new B();
		C c = new C();
		if (i instanceof I)
			System.out.println("i is a I");
		if (b instanceof A)
			System.out.println("b is a A");
		if (c instanceof A)
			System.out.println("c is a A");
	}
}

 

What will be printed when the above code is compiled and run?

Select 1 option

A. It will not compile.

B. It will throw an exception when run.

C. i is a I
    b is a A

D. i is a I

E. b is a A

F. c is a A

Descriptive Question

Find output or error: 

class OOP1 {

void x (int i) {

System.out.println("one");

}

void x (String s) {

System.out.println("two");

}

void x (double d) {

System.out.println("three");

}

public static void main(String[] args) {

new OOP1().x (4.0);

}

} 

 

Descriptive Question

Given:

public class X implements Z {

public String toString() {

return "I am X";

}

public static void main(String[] args) {

Y myY = new Y();

X myX = myY;

Z myZ = myX;

System.out.println(myZ);

}

}

class Y extends X {

public String toString() {

return "I am Y";

}

}

interface Z {}

What is the reference type of myZ and what is the type of the object it references? 

Descriptive Question

Find output or error: 

class OOP2 {

int x(double d) {

System.out.println("one");

return 0;

}

String x(double d) {

System.out.println("two");

return null;

}

double x(double d) {

System.out.println("three");

return 0.0;

}

public static void main(String[] args) {

new OOP2().x(4.0)

}

} 

 

Descriptive Question

Find output or error:

public class NullCheck {

public void myMethod(String str) {

System.out.println("String");

}

public void myMethod(Double d) {

System.out.println("Double");

}

public static void main(String[] args) {

NullCheck nc = new NullCheck();

nc.myMethod(null);

}

}

 

Pages

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!