Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 15

MCQ Discussion: oops, class, interface, ab class, exception handling

=======================================

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 . 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

Q17. 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

What is the op?


---------------------
class T{
public static final int temp=100;
static {
System.out.println(" i am static block");
}
}
public class Demo {

public static void main(String[] args) {

System.out.println(T.temp);
}
}

What happens if remove final?

What is the output?


===============

public class Demo {

Demo(){
System.out.println("inside ctr");
}
Demo demo=new Demo();
public static void main(String[] args) {

new Demo();

}
}

What is the output?


===============
interface A{
public static final int i=0;
public abstract void foo();
}
class B implements A{
private void foo(){}

}
What is the o/p?
------------------
public class Demo {

public static void main(String[] args) {

Integer i1=127;
Integer i2=127;
System.out.println(i1==i2);

Integer i3=128;
Integer i4=128;
System.out.println(i3==i4);
}
}

// Integer.valueOf();
Integer cache ( for performance)

null with static method : What will be the O/P?


-------------------------------------

class A{
public static void foo(){
System.out.println(" i am static method");
}
}
public class Demo {

public static void main(String[] args) {

A a=null;
a.foo();

}
Which code with compile?
-----------------------------

Integer val=new Integer(null);


String s=new String (null);

ambigous ctr call


------------------
What if i run it?
What is the op?
---------------------

Set<String> s=new HashSet<String>();


s.add("QA");
s.add("dev");
System.out.println(s);

Set<String> s2=new HashSet<String>(){{


add("QA");
s.add("dev");
}};

System.out.println(s2);

=> double brace init {{ }}


=> first brace create an annomous class and inner brace is used to
init.......

What is the op?


--------------------

public class Demo {

public static void main(String[] args) {

System.out.println(returnSomething());

private static int returnSomething() {


try{
return 5;// throws new NullPointerException();
}finally{
return 9;
}
}

=>maigic of finally
=>never ever return value from finally block ? why?

autoboxing, int and wrapper classes


----------------------------------

What is the op?


---------------

Long l=new Long(0);


System.out.println(l.equals(0));

What is the O/P?


-----------------

class Cat{
public static void meow(){
System.out.println("meow");
}
}

class CostlyCat extends Cat{


public static void meow(){
System.out.println("costly meow");
}
}
public class Demo {

public static void main(String[] args) {

Cat c=new CostlyCat();

c.meow();
}

}
What is the O/P?
-----------------

class Cat{
public static String foo="value";
}

class CostlyCat extends Cat{


private static String foo="value2";
}
public class Demo {

public static void main(String[] args) {

System.out.println(new CostlyCat().foo);
}
}

=>hiding superclass variable method

What is the O/P?


-----------------

Autoboxing, Inheritance and Overriding


--------------------------------------
class A{

void method(int i){


System.out.println("base method ");
}
}
class B extends A{
void method(Integer i){
System.out.println("overriden method ");
}
}
public class Demo {

public static void main(String[] args) {


A a=new B();
a.method(22);
}
}

Constructor overloading and NULL


---------------------------------

What is the op?


------------------

public class Confusion {

public Confusion(Object o){


System.out.println("public Confusion(Object o)");
}

public Confusion(double[] o){


System.out.println("public Confusion( double[] o)");
}

public static void main(String[] args) {

new Confusion(null);

character addition, subtraction


-------------------------------
System.out.println("H" + "i");
System.out.println('H'+'i');

// on add of char java convert them to there ascii value


double subtraction and BigDecimal-
------------------------------------
What is the op?
-------------

double d=1.1-1.00;

if(d==0.10)
System.out.println("hello");
else
System.out.println("hi");

Why?

=> never compare double , float , object with ==


=> actual value of d is not 0.10

What to do?
---------
=> if you want to manipulate double never use double / float
use BigDecimal class

Correct approach?
----------------
BigDecimal d2=new BigDecimal("1.10").subtract(new BigDecimal("1.00"));
System.out.println(d2);

if(d2.doubleValue()==0.10)
System.out.println("hello");
else
System.out.println("hi");
What is the op?
------------------

class Foo {
private int i;

public Foo(int i) {
this.i = i;
}

public boolean equals(Foo obj) {


if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Foo other = (Foo) obj;
if (i != other.i)
return false;
return true;
}

public class Confusion {

public static void main(String[] args) {

Foo f1 = new Foo(22);


Foo f2 = new Foo(22);

System.out.println(f1==f2);

What is the op?


---------------------

public class Confusion {

private static Random random=new Random();


public static void main(String[] args) {

StringBuffer word=null;
switch (random.nextInt(5)) {
case 1:
word=new StringBuffer('M');
break;

default:
word=new StringBuffer('N');
}

word.append('a');
word.append('b');

System.out.println(word);
}

=> StringBuffer Constructor and Char

What is the op?


------------------

class String {

public class Confusion {

public static void main(String[] args) {


System.out.println("hi");
}

You might also like