0% found this document useful (0 votes)
144 views

DXC - Java MCQs - 1-25

The document provides sample Java code snippets and questions to test knowledge of Java concepts like classes, objects, inheritance, polymorphism, interfaces and abstract classes. The key points covered are: 1. Output of code with class methods overriding variable scope. 2. Number of objects and references created in a class. 3. Inheritance and overriding of private and protected methods. 4. Interface implementation and overriding constants. 5. Missing code for classes implementing multiple interfaces. 6. Missing code for abstract classes.

Uploaded by

Mahesh Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views

DXC - Java MCQs - 1-25

The document provides sample Java code snippets and questions to test knowledge of Java concepts like classes, objects, inheritance, polymorphism, interfaces and abstract classes. The key points covered are: 1. Output of code with class methods overriding variable scope. 2. Number of objects and references created in a class. 3. Inheritance and overriding of private and protected methods. 4. Interface implementation and overriding constants. 5. Missing code for classes implementing multiple interfaces. 6. Missing code for abstract classes.

Uploaded by

Mahesh Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

DXC Preparation

Week 1
1. What is the output of the following code snippet?

class Bill {
int itemPrice;

public Bill(int itemPrice) {


this.itemPrice = itemPrice;
}

void display() {
int itemPrice = 20;
System.out.println(itemPrice);
}
}

class Demo {
public static void main(String[] args) {
Bill billobj = new Bill(10);
System.out.println(billobj.itemPrice);
billobj.display();
}
}

A. 10
0

B. 10
20

C. 10
10

D. Error in the class as there is no default constructor defined

Ans:B

2. What will be the output of the following java program?

class Parent {
int x = 10;
}

class Child extends Parent {


int x = 20;
}

class Test {
public static void main(String[] args) {
Parent p = new Parent();
System.out.print(p.x + " ");
Child c = new Child();
System.out.print(c.x + " ");
Parent p1 = new Child();
System.out.print(p1.x);
}
}
a)10 20 20
b)10 20 10
c)10 10 10
d)20 20 20
Ans: b

3. Consider the code given below.

Identify the code that needs to be filled in Line 1, 2, and 3 respectively such that:
• The student id is auto-generated starting from 501 in steps of 1
• The method 'getNoOfStudent' returns the total number of students enrolled at any given point.

public class Student {


private int studentId;
private String studentName;
private int yearofEnrollment;
public static int counter;
static {
//Line 1
}

public Student(String name, int yearOfEnrollment) {


this.studentName = name;
this.yearofEnrollment = yearofEnrollment;
//Line 2
}

public static int getNoOfStudent() {


//Line 3
}
}

A. Line 1: Student.counter=501;

Line 2: this.studentid=Student.counter++;

Line 3: return (Student.counter-500);

B. Line 1: Student.counter=501;

Line 2: this.studentid=++Student.counter;

Line 3: return (Student.counter-501);

C. Line 1: Student.counter=500;

Line 2: this.studentId=Student.counter++;

Line 3: return (Student.counter-500);

D. Line 1: Student.counter=500;

Line 2: this.studentid=++Student.counter;

Line 3: return (Student.counter-500);


Ans:C & D

4. Consider the code snippet given below:


class Customer {
public int custId;
public String custName;
}

public class Tester_1 {


public static void main(String args[]) {
Customer obj = new Customer();
Customer objone = new Customer();
Customer objTwo;
Customer objThree = obj;
}
}

How many object and reference variables of class Customer will be created?

A. 3 objects and 1 reference variable

B. 2 objects and 4 reference variables

C. 4 objects and 4 reference variables

D. 2 objects and 3 reference variables

Ans: B

5. What is the output of the following code?

package a;

class Base {
private int fun() {
return 0;
}

public int run() {


return 3;
}
}

class Derived extends Base {


private int fun() {
return 1;
}

public int run() {


return fun();
}
}

class Derivedl extends Derived {


public int fun() {
return 2;
}
}

class Tester {
public static void main(String[] args) {
Base baseRef = new Derivedl();
System.out.println(baseRef.run());
}
}

A. 1
B. 2
C. 0
D. 3
Ans: A

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

package a;

final class A {
int i;
}

class B extends A
{
int j;
System.out.println(j + " " + i);
}

class inheritance {
public static void main(String args[]) {
B obj = new B();
obj.display();
}
}

a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error

Ans: d

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

class overload {
int x;
double y;

void add(int a, int b) {


x = a + b;
}

void add(double c, double d) {


y = c + d;
}

overload() {
this.x = 0;
this.y = 0;
}

public class Overload_Method{


public static void main(String args[]) {
overload obj = new overload();
int a = 2;
double b = 3.2;
obj.add(a, a);
obj.add(b, b);
System.out.println(obj.x + " " + obj.y);
}
}

a) 4 6.4
b) 6.4 6
c) 6.4 6.4
d) 6 6

Ans: a

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

class A1 {
int i;
}

class B1 extends A1 {
int j;

void display() {
super.i = j + 1;
System.out.println(j + " " + i);
}
}

class Inheritance_1 {
public static void main(String args[]) {
B1 obj = new B1();
obj.i = 1;
obj.j = 2;
obj.display();
}
}

a) 2 2
b) 3 3
c) 2 3
d) 3 2

Ans: c

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

class A2 {
public int i;
public int j;
A2() {
i = 1;
j = 2;
}
}

class B2 extends A2{


int a;
B2(){
super();
}
}

class Super_Use{
public static void main(String args[]) {
B2 obj = new B2();
System.out.println(obj.i + " " + obj.j);
}
}

a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error

Ans: a

10. What will be the output of the following Java code?

class A3{
public int i;
private int j;
}

class B3 extends A3{


void display() {
super.j = super.i + 1;
System.out.println(super.i + " " + super.j);
}
}

class Inheritance_3{
public static void main(String args[]){
B3 obj = new B3();
obj.i=1;
obj.j=2;
obj.display();
}
}

a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error

Ans:d

11. What will be the output of the following Java code?


class A_4{
public static int i;
public int j;
A_4() {
i = 1;
j = 2;
}
}
class B_4 extends A_4{
int a;
B_4() {
super();
}
}

class Super_Use_2{
public static void main(String args[]) {
B_4 obj1 = new B_4();
System.out.println(obj1.i + " " + obj1.j);
B_4 obj2 = new B_4();
obj2.i=obj1.i+1;
System.out.println(obj1.i + " " + obj1.j);
System.out.println(obj2.i + " " + obj2.j);
}
}

a) 1 2
22
22
b) 2 1
22
12
c) 1 2
12
22
d) Compilation Error

Ans:a

12. What will be the output of the following Java code?

class A_5{
int i;
void display() {
System.out.println(i);
}
}

class B_5 extends A_5{


int j;
void display() {
System.out.println(j);
}
}

class Method_Overridding{
public static void main(String args[]) {
B_5 obj = new B_5();
obj.i = 1;
obj.j = 2;
obj.display();
A_5 obj1 = new A_5();
//obj1.display();
}
}

a) 0
b) 1
c) 2
d) Compilation Error

Ans:c

13. What will be the output of the following Java code?

class A_6{
public int i;
protected int j;
}

class B_6 extends A_6 {


int j;
void display() {
super.j = 3;
System.out.println(i + " " + j);
}
}

class Output{
public static void main(String args[]) {
B_6 obj = new B_6();
obj.i=1;
obj.j=2;
obj.display();
}
}

a) 1 2
b) 2 1
c) 1 3
d) 3 1

Ans:a

14. What is the output of the below Java program with an Interface?

interface Worm {
int teeth = 2;
}

class BookWorm implements Worm {


int teeth = 4;

void show() {
teeth = 5;
System.out.println("Teeth: " + teeth);
}
}

public class InterfaceTest {


public static void main(String[] args) {
new BookWorm().show();
}
}

A) Teeth: 4
B) Teeth: 5
C) Compiler error as teeth is a constant in Worm interface.
D) None of the above
Ans:b

15. Which is the missing java code in the class implementing an Interface below?

interface Linein{
void addInput();
}

interface Lineout{
void addOutput();
}

class Speaker implements Linein, Lineout{


//MISSING CODE
}

A)

class Speaker implements Linein, Lineout


{
@Override
public void addOutput() { }

@Override
public void addInput() { }
}

B)

class Speaker implements Linein, Lineout


{
@Override
public void addOutput() { }
}

C)

class Speaker implements Linein, Lineout


{
@Override
public void addInput() { }
}

D) All the above

Ans:a

16. Which is the missing code to successfully compile the below Java program with abstract classes and Interfaces?

public class AbstractTest {


public static void main(String[] a) {
//Some Code
}
}

interface A {
void a();
}

abstract class B implements A {


abstract void b();
}

class C extends B {
// Missing methods
}

A)

@Override
public void a() { }

@Override
void b() {}

B)

@Override
public void a() { }

C)

@Override
void b() {}

D) All the above

Ans:a

17. What is the output of this program?

interface Calculate {
void cal(int item);
}

class Display implements Calculate {


int x;

public void cal(int item) {


x = item * item;
}
}

class Main_Test {
public static void main(String args[]) {
Display arr = new Display();
arr.x = 0;
arr.cal(2);
System.out.print(arr.x);
}
}

A. 0
B. 2
C. 4
D. None of the mentioned

Ans:c

18. What is the output of the following program?


public class Test_1 {
public Test_1() {
System.out.printf("1");
new Test_1(10);
System.out.printf("5");
}

public Test_1(int temp) {


System.out.printf("2");
new Test_1(10, 20);
System.out.printf("4");
}

public Test_1(int data, int temp) {


System.out.printf("3");

public static void main(String[] args) {


Test_1 obj = new Test_1();

}
}

a) 12345
b) Compilation error '
c) 15
d) Runtime error

Ans:a

19. What is the output of the following program?

class Base{
public static String s = " Super Class ";
public Base() {
System.out.printf("1");
}
}
public class Derived extends Base{
public Derived() {
System.out.printf("2");
super();
}

public static void main(String[] args){


Derived obj = new Derived();
System.out.printf(s);
}
}

a) 21 Super Class
b) Super Class 21
c) Compilation error
d) 12 Super Class

Ans. c

20. What is the output of the following program?


import java.io.IOException;
import java.util.EmptyStackException;

public class NewClass {


public static void main(String[] args) {
try {
System.out.printf("%d", 1);
throw (new Exception());
} catch (IOException e) {
System.out.printf("%d", 2);
} catch (EmptyStackException e) {
System.out.printf("%d", 3);
} catch (Exception e) {
System.out.printf("%d", 4);
} finally {
System.out.printf("%d", 5);
}
}
}

a) 12345
b) 15
c) 135
d) 145

Ans: d

21. Which of the following is FALSE about abstract classes in Java.


A. If we derive an abstract class and do not implement all the abstract methods, then the derived class should also be marked as
abstract using 'abstract' keyword
B. Abstract classes can have constructors
C. A class can be made abstract without any abstract method
D. A class can inherit from multiple abstract classes.
Answer : B

22. Which of the following is true about interfaces in java.


1) An interface can contain the following type of members.
.... public, static, final fields (i.e., constants)
....default and static methods with bodies
2) An instance of interface can be created.
3) A class can implement multiple interfaces.
4) Many classes can implement the same interface.
A 1, 3 and 4
B 1, 2 and 4
C 2, 3 and 4
D 1, 2, 3 and 4
Answer: A

23. Predict the output of the following program.


abstract class Demo {
public int a;

Demo() {
a = 10;
}

abstract public void set();


abstract final public void get();
}

class Test_3 extends Demo {


public void set(int a) {
this.a = a;
}

final public void get() {


System.out.println("a = " + a);
}

public static void main(String[] args) {


Test_3 obj = new Test_3();
obj.set(20);
obj.get();
}
}
24.

A a = 10
B a = 20
C Compilation error
Answer : C

25. Which of the following statement(s) with regard to an abstract class in JAVA is/are TRUE ?
I. An abstract class is one that is not used to create objects.
II. II. An abstract class is designed only to act as a base class to be inherited by other classes.
A Only I
B Only II
C Neither I nor II
D Both I and II
Answer: D

You might also like