You are on page 1of 11

MCQ’S

1. What is true about private constructor?

a) Private constructor ensures only one instance of a class exist at


any point of time

b) Private constructor ensures multiple instances of a class exist at


any point of time

c) Private constructor eases the instantiation of a class

d) Private constructor allows creating objects in other classes

2. What would be the behaviour if this() and super() used in a


method?

a) Runtime error

b) Throws exception

c) compile time error

d) Runs successfully

3. What is false about constructor?

a) Constructors cannot be synchronized in Java

b) Java does not provide default copy constructor

c) Constructor can be overloaded

d) “this” and “super” can be used in a constructor


4. What is true about Class.getInstance()?

a) Class.getInstance calls the constructor

b) Class.getInstance is same as new operator

c) Class.getInstance needs to have matching constructor

d) Class.getInstance creates object if class does not have any


constructor

5. What is true about constructor?

a) It can contain return type

b) It can take any number of parameters

c) It can have any non access modifiers

d) Constructor cannot throw an exception

6. Abstract class cannot have a constructor.

a) True

b) False

7. What is true about protected constructor?

a) Protected constructor can be called directly

b) Protected constructor can only be called using super()


c) Protected constructor can be used outside package

d) protected constructor can be instantiated even if child is in a


different package

8. What is not the use of “this” keyword in Java?

a) Passing itself to another method

b) Calling another constructor in constructor chaining

c) Referring to the instance variable when local variable has the same
name

d) Passing itself to method of the same class

9. What would be the behaviour if one parameterized constructor is


explicitly defined?

a) Compilation error

b) Compilation succeeds

c) Runtime error

d) Compilation succeeds but at the time of creating object using


default constructor, it throws compilation error

10. What would be behaviour if the constructor has a return type?

a) Compilation error

b) Runtime error

c) Compilation and runs successfully


d) Only String return type is allowed

Output of following Java Program?


class Base {
public void show() {
System.out.println("Base::show() called");
}
}

class Derived extends Base {


public void show() {
System.out.println("Derived::show() called");
}
}

public class Main {


public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}
ADerived::show() called
Base::show() called

Question 2
class Base {
final public void show() {
System.out.println("Base::show() called");
}
}

class Derived extends Base {


public void show() {
System.out.println("Derived::show() called");
}
}

class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}
A Base::show() called
B Derived::show() called
C Compiler Error
D Runtime Error

Question 3
class Base {
public static void show() {
System.out.println("Base::show() called");
}
}

class Derived extends Base {


public static void show() {
System.out.println("Derived::show() called");
}
}

class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}
A Base::show() called
B Derived::show() called
C Compiler Error
Question 4
Which of the following is true about inheritance in Java?

1) Private methods are final.


2) Protected members are accessible within a package and
inherited classes outside the package.
3) Protected methods are final.
4) We cannot override private methods.
A 1, 2 and 4
B Only 1 and 2
C 1, 2 and 3
D 2, 3 and 4

Question 5
Output of following Java program?
class Base {
public void Print() {
System.out.println("Base");
}
}

class Derived extends Base {


public void Print() {
System.out.println("Derived");
}
}

class Main{
public static void DoPrint( Base o ) {
o.Print();
}
public static void main(String[] args) {
Base x = new Base();
Base y = new Derived();
Derived z = new Derived();
DoPrint(x);
DoPrint(y);
DoPrint(z);
}
}

A Base
Derived
Derived
Base
B Base
Derived
Base
C Derived
Base
D Compiler Error

Question 6
Predict the output of following program. Note that fun() is public in base
and private in derived.
class Base {
public void foo() { System.out.println("Base"); }
}

class Derived extends Base {


private void foo() { System.out.println("Derived"); }
}

public class Main {


public static void main(String args[]) {
Base b = new Derived();
b.foo();
}
}
A Base
B Derived
C Compiler Error
D Runtime Error

Question 7
Which of the following is true about inheritance in Java.
1) In Java all classes inherit from the Object class directly or indirectly.
The Object class is root of all classes.
2) Multiple inheritance is not allowed in Java.
3) Unlike C++, there is nothing like type of inheritance in Java where we
can specify whether the inheritance is protected, public or private.

A
1, 2 and 3
B 1 and 2
C 2 and 3
D 1 and 3

Question 8
Predict the output of following Java Program
// filename Main.java
class Grandparent {
public void Print() {
System.out.println("Grandparent's Print()");
}
}

class Parent extends Grandparent {


public void Print() {
System.out.println("Parent's Print()");
}
}

class Child extends Parent {


public void Print() {
super.super.Print();
System.out.println("Child's Print()");
}
}

public class Main {


public static void main(String[] args) {
Child c = new Child();
c.Print();
}
}

A
Compiler Error in super.super.Print()
B
Grandparent's Print()
Parent's Print()
Child's Print()

C
Runtime Error

Question 9
final class Complex {

private final double re;


private final double im;

public Complex(double re, double im) {


this.re = re;
this.im = im;
}

public String toString() {


return "(" + re + " + " + im + "i)";
}
}

class Main {
public static void main(String args[])
{
Complex c = new Complex(10, 15);
System.out.println("Complex number is " + c);
}
}

A Complex number is (10.0


15.0i)
B Compiler Error
Complex number is
C
SOME_GARBAGE

Complex number is
Complex@8e2fb5

Here 8e2fb5 is hash code of c

You might also like