Sei sulla pagina 1di 5

Q1public class Person{

public String name;


public static void main(String str[]){
Person p=null;
System.out.print(p instanceof Person);
}
What is the result of compilation or execution of the code?
1. Prints false
2. Prints true
3. Results in compilation error
4. Results in runtime error

Q2. Which of these statements are true?

A. If a RuntimeException is not caught, the method will terminate and normal


execution of the thread will resume.
B. An overriding method must declare that it throws the same exception classes as the
method it overrides.
C. The main method of a program can declare that it throws checked exceptions.
D. finally blocks are executed if and only if an exception gets thrown while inside the
corresponding try block.

Q3. Which of the following is false?


A. Class name and constructor name must be same
B. Constructor does not have return type
C. Constructor must be public
D. Constructor cannot be static

Q4. What will be the result of compiling and running the following code?
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
}
}
Select 1 option
A. It will print 1
B. It will print 2.
C. Compile time error at //1
D. Compile time error at //2

Q5. Which of the following are valid classes?

Select 1 option
A. public class ImaginaryNumber extends Number {
}
B. public class ThreeWayBoolean extends Boolean {
}
C. public class NewSystem extends System {
}
D. public class ReverseString extends String {
}
Q6. class A {
public static void f(){
System.out.println(“fA”); }}

class B extends A{
public void f(){
System.out.println(“fB”); }
public static void main(String[] args) {
A a= new B();
a.f(); }}
What will happen on compilation or execution of code?
A. fA
B. fB
C. Code will not compile
D. Code will throw runtime error

Q7. What will be the result of attempting to compile and run the following program?
public class TestClass{
public static void main(String args[ ] ){
A o1 = new C( );
B o2 = (B) o1;
System.out.println(o1.m1( ) );
System.out.println(o2.i );
}
}

class A { int i = 10; int m1( ) { return i; } }


class B extends A { int i = 20; int m1() { return i; } }
class C extends B { int i = 30; int m1() { return i; } }
Select 1 option
A. The program will fail to compile.
B. Class cast exception at runtime.
C. It will print 30, 20.
D. It will print 30, 30.
E. It will print 20, 20.

Q8. Which of the following statements are true?


A. Private methods cannot be overridden in subclasses.
B. A subclass can override any method of a non-final superclass.
C. An overriding method can declare that it throws a wider spectrum of checked
exceptions than the method it is overriding.
D. The parameter list of an overriding method must be a subset of the parameter list of
the method that it is overriding.

Q9. class Base {


public static void foo(Base bObj) {
System.out.println("In Base.foo()");
bObj.bar();
}

public void bar() {


System.out.println("In Base.bar()");
}
}

class Derived extends Base {


public static void foo(Base bObj) {
System.out.println("In Derived.foo()");
bObj.bar();
}

public void bar() {


System.out.println("In Derived.bar()");
}
}
class OverrideTest {
public static void main(String[] args) {
Base bObj = new Derived();
bObj.foo(bObj);
}
}
What is the output of this program when executed?
a)
In Base.foo()
In Base.bar()
b)
In Base.foo()
In Derived.bar()
c)
In Derived.foo()
In Base.bar()
d)
In Derived.foo()
In Derived.bar()

Q10. class Tree {


Tree getInstance() { return new Tree();}
}
class Fruit extends Tree {
//line 1
}

class Mango extends Fruit{ }


Which statement(s), inserted at line 1, will NOT compile?
A. Fruit getInstance() { return this;}
B. Mango getInstance() { return this;}
C. Tree getInstance() { return this;}
D. Object getInstance() { return this;}

Q11. Consider following example:


public interface xyz {
void abc() throws IOException;
}
public interface pqr {
void abc() throws FileNotFoundException;
}
public class Implementation implements xyz, pqr {
// insert code
{ /*implementation*/ }
}
Which of the following statement(s) can you insert in place of “// insert code” comment?
A. public void abc() throws IOException
B. public void abc() throws FileNotFoundException
C. public void abc() throws FileNotFoundException, IOException
D. public void abc() throws IOException, FileNotFoundException

Q12. Consider the following program:


class ExceptionTest {
public static void foo() {
try {
throw new ArrayIndexOutOfBoundsException();
} catch(ArrayIndexOutOfBoundsException oob) {
throw new Exception(oob);
}
}
public static void main(String []args) {
try {
foo();
} catch(Exception re) {
System.out.println(re.getCause());
}
}
}
Which one of the following options correctly describes the behavior of this program?
A. java.lang.Exception
B. java.lang.ArrayIndexOutOfBoundsException
C. class java.lang.IllegalStateException
D. T his program fails with compiler error(s)

Q13. Which are true? (Choose all that apply.)


A. "X extends Y" is correct if and only if X is a class and Y is an interface.
B. "X extends Y" is correct if and only if X is an interface and Y is a class.
C. "X extends Y" is correct if X and Y are either both classes or both interfaces.
D. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces.

Q14. . Given:
class Rocket {
private void blastOff() { System.out.print("bang "); }
}
public class Shuttle extends Rocket {
public static void main(String[] args) {
new Shuttle().go();
}
void go() {
blastOff();
// Rocket.blastOff(); // line A
}
private void blastOff() { System.out.print("sh-bang "); }
}
Which are true?
A. As the code stands, the output is bang
B. As the code stands, the output is sh-bang
C. As the code stands, compilation fails.
D. If line A is uncommented, the output is bang bang

Q15. interface Gadget {


void doStuff();
}
abstract class Electronic {
void getPower() { System.out.print("plug in "); }
}
public class Tablet extends Electronic implements Gadget {
void doStuff() { System.out.print("show book "); }
public static void main(String[] args) {
new Tablet().getPower();
new Tablet().doStuff();
}
}
Which are true? (Choose all that apply.)
A. The class Tablet will NOT compile
B. The interface Gadget will NOT compile
C. The output will be plug in show book
D. The abstract class Electronic will NOT compile

Q16. import java.util.*;


class UtilitiesTest {
public static void main(String []args) {
List<int> intList = new ArrayList<>();
intList.add(10);
intList.add(20);
System.out.println("The list is: " + intList);
}
}
A. I t prints the following: The list is: [10, 20].
B. I t prints the following: The list is: [20, 10].
C. It results in a compiler error.
D. I t results in a runtime exception.

Q17. String s2= new String("Van");


StringBuilder s1= new StringBuilder("now");
s1.replace(0,0,"S");
s2.replace('V','M');
System.out.println(s1+s2);
What will the statements above print when executed?
1. SnowMan
2. SnowVan
3. SowMan
4. SowVan

Q18. Predict the outcome of the following program:


public class ParseString1 {
public static void main(String[] s) {
String quote = "Never lend books-nobody ever returns them!";
String [] words = quote.split(" ", 2);
// split strings based on the delimiter " " (space)
for (String word : words) {
System.out.println(word);
}
}
}
A. It will result in a compile-time error.
B. It will result in a runtime exception.
C. It will print the following output when executed
Never lend
D. It will print the following output when executed
Never
lend books-nobody ever returns them!

Q19 . What is not true about java interface


1. We can not define instance variable inside Java interface
2. We can define constant inside java interface
3. We can have constructor inside java interface
4. All are correct

Q20. class T extends String{


public static void main(String[] a){
String t = new T();
System.out.println(t);
}}
What is the result of compilation and execution of the code?
A. Code does not compile because inheritance from String is prohibited
B. Code does not compile because String class does not have no-argument constructor
C. Code does not compile because of invalid conversion of subclass object into super class object.
D. Code compiles clean

Potrebbero piacerti anche