0% found this document useful (0 votes)
7 views24 pages

Java MCQs

The document contains multiple-choice questions (MCQs) related to Java programming concepts, including inheritance, method overriding, access modifiers, and object creation. Each question presents a code snippet and asks for the expected output or behavior of the program. The questions cover various topics such as polymorphism, constructors, and method overloading, testing the reader's understanding of Java fundamentals.

Uploaded by

Sapna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
7 views24 pages

Java MCQs

The document contains multiple-choice questions (MCQs) related to Java programming concepts, including inheritance, method overriding, access modifiers, and object creation. Each question presents a code snippet and asks for the expected output or behavior of the program. The questions cover various topics such as polymorphism, constructors, and method overloading, testing the reader's understanding of Java fundamentals.

Uploaded by

Sapna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 24

Java MCQs

1. What is the output of the following Java program?

class Automobile {
private String drive() {
return "Driving vehicle";
}
//first check in polymorpic reference class then main then rest
} class depend on accesibility modifier
class Car extends Automobile {
protected String drive() {
return "Driving car";
}
}

public class ElectricCar extends Car {

@Override
public final String drive() {
return "Driving an electric car";
}

public static void main(String[] wheels) {


final Car car = new ElectricCar();
System.out.print(car.drive());
}
}

A. Driving vehicle
B. Driving an electric car
C. Driving car
D. The code does not compile

2. Look at the following code and choose the right option for the word :

// Shape.java
public class Shape {
protected void display() {
System.out.println("Display-base");
}
}
// Circle.java
public class Circle extends Shape { <
< access - modifier > void display() {
System.out.println("Display-derived");
}
}

a) Only the protected can be used.


b) public and protected both can be used.
c) public, protected, and private can be used.
d) Only the public can be used.

3. What will be the output of the following Java program?

class Base {
public Base() {
System.out.println("Base");
}
}

class Derived extends Base {


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

class DeriDerived extends Derived {


public DeriDerived() {
System.out.println("DeriDerived");
}
}

public class Test {


public static void main(String[] args) {
Derived b = new DeriDerived();
}
}

a)

Base
Derived
DeriDerived

b)

Derived
DeriDerived

c)

DeriDerived
Derived
Base

d)

DeriDerived
Derived

4. What is the output of the following Java program?

class One{
public One(){
System.out.print("One,");
}
}
class Two extends One{
public Two(){
System.out.print("Two,");
}
}
class Three extends Two{
public Three(){
System.out.print("Three");
}
}

public class Test{

public static void main(String[] args){


Three three = new Three();
}
}

a) Three
b) One
c) One,Two,Three
d) Run-time error

5. Consider the following program:

class Base {
public Base() {
System.out.print("Base ");
}

public Base(String s) {
System.out.print("Base: " + s);
}
}

class Derived extends Base {


public Derived(String s) {
super(); // Stmt-1
super(s); // Stmt-2
System.out.print("Derived ");
}
}

class Test {
public static void main(String[] args) {
Base base = new Derived("Hello ");
}
}

Select three correct options from the following list:


a) Removing Stmt-1 will make the program compilable and it will print the following: Base
Derived.
b) Removing Stmt-1 will make the program compilable and it will print the following: Base: Hello
Derived.
c) Removing Stmt-2 will make the program compilable and it will print the following: Base
Derived.
d) Removing both Stmt-1 and Stmt-2 will make the program compilable and it will print the
following: Base Derived.
e) Removing both Stmt-1 and Stmt-2 will make the program compilable and it will print the
following: Base: Hello Derived.

6. What is the output of the following Java program?

abstract class Car {


static {
System.out.print("1");
}

public Car(String name) {


super();
System.out.print("2");
}

{
System.out.print("3");
}
}

public class BlueCar extends Car {


{
System.out.print("4");
}
public BlueCar() {
super("blue");
System.out.print("5");
}

public static void main(String[] gears) {


new BlueCar();
}
}

a) 23451
b) 12354
c) 13245
d) The code does not compile.

7. What is the output of the following Java program?

class Math {
public final double secret = 2;
}

class ComplexMath extends Math {


public final double secret = 4;
}

public class InfiniteMath extends ComplexMath {


public final double secret = 8;

public static void main(String[] numbers) {


Math math = new InfiniteMath();
System.out.print(math.secret);
}
}

A. 2
B. 4
C. 8
D. The code does not compile.

8. What is the output of the following Java program?

public class Test {


public void print(Integer i) {
System.out.println("Integer");
}

public void print(int i) {


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

public void print(long i) {


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

public static void main(String args[]) {


Test test = new Test();
test.print(10);
}
}

a) The program results in a compiler error (“ambiguous overload”).


b) long
c) Integer
d) int

9. What is the output of the following Java program?

class One{
public static void print(){
System.out.println("1");
}
}

class Two extends One{


public static void print(){
System.out.println("2");
}
}

public class Test{


public static void main(String args[]){
One one = new Two();
one.print();
}
}

a) 2
b) 1
c) Compile-time error
d) Run-time error

10. What is the output of the following Java program?

class Parent{
public void className(){
System.out.println("Parent");
}
}
class Child extends Parent{
void className(){
System.out.println("Child");
}
}

public class Test{

public static void main(String[] args){


Parent parent = new Child();
parent.className();
}
}

a) Parent
b) Child
c) Compile-time error
d) Run-time error

11. What is the output of the following Java program?

class Demo{
public Demo(int i){
System.out.println("int");
}

public void Demo(short s){


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

public class Test{

public static void main(String[] args){


short s = 10;
Demo demo = new Demo(s);
}
}

a) int
b) short
c) Compile-time error
d) Run-time error
Q1 - Consider the following program and predict the output:

public class Test {


public void print(Integer i) {
System.out.println("Integer");
}

public void print(int i) {


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

public void print(long i) {


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

public static void main(String args[]) {


Test test = new Test();
test.print(10);
}
}

a) The program results in a compiler error (“ambiguous overload”).

b) long

c) Integer

d) int

Q2 - What is the output of the following program?

public class Test {

public static void main(String[] args) {


String s1 = "hello";
String s2 = new String("hello");

s2 = s2.intern();
System.out.println(s1 == s2);
}
}

a) false

b) true

c) None
Q3 - What will be the output of the following program?

class Base {
public Base() {
System.out.println("Base");
}
}

class Derived extends Base {


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

class DeriDerived extends Derived {


public DeriDerived() {
System.out.println("DeriDerived");
}
}

public class Test {


public static void main(String[] args) {
Derived b = new DeriDerived();
}
}

a)

Base
Derived
DeriDerived

b)

Derived
DeriDerived

c)

DeriDerived
Derived
Base

d)

DeriDerived
Derived
Q4 - Consider the following program:

public class Overloaded {


public static void foo(Integer i) {
System.out.println("foo(Integer)");
}

public static void foo(short i) {


System.out.println("foo(short)");
}

public static void foo(long i) {


System.out.println("foo(long)");
}

public static void foo(int... i) {


System.out.println("foo(int ...)");
}

public static void main(String[] args) {


foo(10);
}
}

Which one of the following options correctly describes the output of this program?

a) foo(Integer)

b) foo(short)

c) foo(long)

d) foo(int ...)

Q5 - Look at the following code and choose the right option for the word :

// Shape.java
public class Shape {
protected void display() {
System.out.println("Display-base");
}
}
// Circle.java
public class Circle extends Shape { <
< access - modifier > void display() {
System.out.println("Display-derived");
}
}

a. Only protected can be used.


B. public and protected both can be used.

C. public, protected, and private can be used.

d. Only public can be used.

Q6 - Consider the following program:

public class BaseClass {


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

void bar() {
System.out.println("In BaseClass.bar()");
}

public static void main(String[] args) {


BaseClass po = new DerivedClass();
po.foo(); // BASE_FOO_CALL
po.bar();
}
}

class DerivedClass extends BaseClass {


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

void bar() {
System.out.println("In Derived.bar()");
}
}

Which one of the following options correctly describes the behavior of this program?

a)

This program results in a compiler error in the line marked with the comment
BASE_FOO_CALL.

b) This program prints the following:

In BaseClass.foo()
In BaseClass.bar()

c) This program prints the following:


In BaseClass.foo()
In Derived.bar()

d) This program prints the following:

In Derived.foo()
In Derived.bar()

Q7 - Consider the following program and predict the output:

class MyThread extends Thread {


@Override
public void run() {
System.out.println("In run method; thread name is: " +
Thread.currentThread().getName());
}
}

public class ThreadTest {

public static void main(String args[]) {


Thread myThread = new MyThread();
myThread.run(); // #1
System.out.println("In main method; thread name is: " +
Thread.currentThread().getName());
}
}

a) The program results in a compiler error at statement #1.

b) The program results in a runtime exception.

c) The program prints the following:

In run method; thread name is: main

In main method; thread name is: main

d) The program prints:

In the run method; the thread name is: thread-0

In the main method; the thread name is: main


Q8 - Consider the following program and choose the correct option from the list of
options:

class Base {
public void test() {
}
}

class Base1 extends Base {


public void test() {
System.out.println("Base1");
}
}

class Base2 extends Base {


public void test() {
System.out.println("Base2");
}
}

class Test {
public static void main(String[] args) {
Base obj = new Base1();
((Base2) obj).test(); // CAST
}
}

a) The program will print the following: Base1.

b) The program will print the following: Base2.

c) The compiler will report an error in the line marked with comment CAST.

d) The program will result in an exception (ClassCastException).

Q9 - Consider the following program:

public class StrEqual {


public static void main(String[] args) {
String s1 = "hello";
String s2 = new String("hello");
String s3 = "hello";
if (s1 == s2) {
System.out.println("s1 and s2 equal");
} else {
System.out.println("s1 and s2 not equal");
}
if (s1 == s3) {
System.out.println("s1 and s3 equal");
} else {
System.out.println("s1 and s3 not equal");
}
}
}

Which one of the following options provides the output of this program when executed?

a)

s1 and s2 equal
s1 and s3 equal

b)

s1 and s2 equal
s1 and s3 not equal

c)

s1 and s2 not equal


s1 and s3 equal

d)

s1 and s2 not equal


s1 and s3 not equal

Q10 - Consider the following program and predict the output:

public class Test {


public static void main(String[] args) {
String s = new String("5");
System.out.println(1 + 10 + s + 1 + 10);
}
}

a) 11511

b) 1105110

c) 115110

d) 27
Q1 - What will be the output of the following program?

class Base {
public Base() {
System.out.println("Base");
}
}

class Derived extends Base {


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

class DeriDerived extends Derived {


public DeriDerived() {
System.out.println("DeriDerived");
}
}

public class Test {


public static void main(String[] args) {
Derived b = new DeriDerived();
}
}

a)

Base
Derived
DeriDerived

b)

Derived
DeriDerived

c)

DeriDerived
Derived
Base

d)

DeriDerived
Derived
Q2 - Consider the following program:

class Base {
public Base() {
System.out.print("Base ");
}

public Base(String s) {
System.out.print("Base: " + s);
}
}

class Derived extends Base {


public Derived(String s) {
super(); // Stmt-1
super(s); // Stmt-2
System.out.print("Derived ");
}
}

class Test {
public static void main(String[] args) {
Base base = new Derived("Hello ");
}
}

Select three correct options from the following list:

a) Removing Stmt-1 will make the program compilable and it will print the following: Base
Derived.

b) Removing Stmt-1 will make the program compilable and it will print the following: Base: Hello
Derived.

c) Removing Stmt-2 will make the program compilable and it will print the following: Base
Derived.

d) Removing both Stmt-1 and Stmt-2 will make the program compilable and it will print the
following: Base Derived.

e) Removing both Stmt-1 and Stmt-2 will make the program compilable and it will print the
following: Base: Hello Derived.

Q3 - Consider the following program and choose the correct option from the list of
options:

class Base {
public void test() {
}
}

class Base1 extends Base {


public void test() {
System.out.println("Base1");
}
}

class Base2 extends Base {


public void test() {
System.out.println("Base2");
}
}

class Test {
public static void main(String[] args) {
Base obj = new Base1();
((Base2) obj).test(); // CAST
}
}

a) The program will print the following: Base1.

b) The program will print the following: Base2.

c) The compiler will report an error in the line marked with comment CAST.

d) The program will result in an exception (ClassCastException).

Q4 - Consider the following program and predict the behavior of this program:

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

abstract class Test extends Base { // #1


public static void main(String[] args) {
Base obj = new Base();
obj.print(); // #2
}
}

a) Compiler error “an abstract class cannot extend from a concrete class” at statement #1.

b) Compiler error “cannot resolve call to print method” at statement #2.

c) The program prints the following: Base:print.


d) The program will throw a runtime exception of AbstractClassInstantiationException.

Q5 - Consider the following program:

class SuperClass {
SuperClass() {
foo();
}

public void foo() {


System.out.println("In SuperClass.foo()");
}
}

class SubClass extends SuperClass {


private String member;

public SubClass() {
member = "HI";
}

public void foo() {


System.out.println("In SubClass.foo(): " + member.toLowerCase());
}
}

public class Test {


public static void main(String[] args) {
SuperClass reference = new SubClass();
reference.foo();
}
}

This program prints the following:

a) In SuperClass.foo()

b) In Derived.foo(): hi

c) In SuperClass.foo() In Derived.foo(): hi

d) This program throws a NullPointerException.

Q6 - Which one of the following relationships describes the OO design concept of


“composition”?

a) is-a
b) is-a-kind-of
c) has-a
d) is-implemented-in-terms-of
e) composed-as

Q1 - Consider the following program:

public class BaseClass {


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

void bar() {
System.out.println("In BaseClass.bar()");
}

public static void main(String[] args) {


BaseClass po = new DerivedClass();
po.foo(); // BASE_FOO_CALL
po.bar();
}
}

class DerivedClass extends BaseClass {


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

void bar() {
System.out.println("In Derived.bar()");
}
}

Which one of the following options correctly describes the behavior of this program?

a)

This program results in a compiler error in the line marked with the comment
BASE_FOO_CALL.

b) This program prints the following:

In BaseClass.foo()
In BaseClass.bar()

c) This program prints the following:

In BaseClass.foo()
In Derived.bar()

d) This program prints the following:

In Derived.foo()
In Derived.bar()

Q2 - Consider the following program:

public class Overloaded {


public static void foo(Integer i) {
System.out.println("foo(Integer)");
}

public static void foo(short i) {


System.out.println("foo(short)");
}

public static void foo(long i) {


System.out.println("foo(long)");
}

public static void foo(int... i) {


System.out.println("foo(int ...)");
}

public static void main(String[] args) {


foo(10);
}
}

Which one of the following options correctly describes the output of this program?

a) foo(Integer)

b) foo(short)

c) foo(long)

d) foo(int ...)

Q3 - What will be the output of this program?

class Color {
int red, green, blue;

void Color() {
red = 10;
green = 10;
blue = 10;
}

void printColor() {
System.out.println("red: " + red + " green: " + green + " blue: " +
blue);
}
}

public class Test {


public static void main(String[] args) {
Color color = new Color();
color.printColor();
}
}

A. Compiler error: no constructor provided for the class.

B. Compiles without errors, and when run, it prints the following: red: 0 green: 0 blue: 0.

C. Compiles without errors, and when run, it prints the following: red: 10 green: 10 blue: 10.

D. Compiles without errors, and when run, crashes by throwing NullPointerException.

Q4 - Look at the following code and choose the right option for the word :

// Shape.java
public class Shape {
protected void display() {
System.out.println("Display-base");
}
}
// Circle.java
public class Circle extends Shape { <
< access - modifier > void display() {
System.out.println("Display-derived");
}
}

a. Only protected can be used.

B. public and protected both can be used.

C. public, protected, and private can be used.

d. Only public can be used.


1. What will be the output of the below program?

public class StrEqual {


public static void main(String[] args) {
String s1 = "hello";
String s2 = new String("hello");
String s3 = "hello";
if (s1 == s2) {
System.out.println("s1 and s2 equal");
} else {
System.out.println("s1 and s2 not equal");
}
if (s1 == s3) {
System.out.println("s1 and s3 equal");
} else {
System.out.println("s1 and s3 not equal");
}
}
}

2. What will be the output of the below program?

public class Test {


public static void main(String[] args) {
String s = new String("5");
System.out.println(1 + 10 + s + 1 + 10);
}
}

3. What will be the output of the below program?

public class Test {


public static void main(String[] args) {
String str = null;
System.out.println(str.valueOf(10));
}
}
4. What will be the output of the below statements?

public class Test {

public static void main(String[] args) {


String s1 = "abc";
StringBuffer s2 = new StringBuffer(s1);
System.out.println(s1.equals(s2));
}
}

5. What is the output of the below program?

public class A {
public static void main(String[] args) {
String s1 = new String("javaguides");
String s2 = new String("javaguides");
System.out.println(s1 = s2);
}
}

6. What is the output of the following program?

public class Test {

public static void main(String[] args) {


String s1 = "hello";
String s2 = new String("hello");

s2 = s2.intern();
System.out.println(s1 == s2);
}
}

7. How many objects will be created in the following code and where they will be stored

in the memory?

String s1 = "javaguides";

String s2 = "javaguides";
8. How many objects will be created in the following code and where they will be stored?

String s1 = new String("javaguides");

String s2 = "javaguides";

9. What is the output of the below code snippet?

String s1 = new String("javaguides");


String s2 = new String("javaguides");
System.out.println(s1 == s2);

10. What is the output of the below code snippet?

String s1 = "javaguides"
String s2 = "javaguides";
System.out.println(s1 == s2);

You might also like