DXC - Java MCQs - 1-25
DXC - Java MCQs - 1-25
Week 1
1. What is the output of the following code snippet?
class Bill {
int 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
Ans:B
class Parent {
int x = 10;
}
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
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.
A. Line 1: Student.counter=501;
Line 2: this.studentid=Student.counter++;
B. Line 1: Student.counter=501;
Line 2: this.studentid=++Student.counter;
C. Line 1: Student.counter=500;
Line 2: this.studentId=Student.counter++;
D. Line 1: Student.counter=500;
Line 2: this.studentid=++Student.counter;
How many object and reference variables of class Customer will be created?
Ans: B
package a;
class Base {
private int fun() {
return 0;
}
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
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
class overload {
int x;
double y;
overload() {
this.x = 0;
this.y = 0;
}
a) 4 6.4
b) 6.4 6
c) 6.4 6.4
d) 6 6
Ans: a
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
class A2 {
public int i;
public int j;
A2() {
i = 1;
j = 2;
}
}
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
class A3{
public int i;
private int 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
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
class A_5{
int i;
void display() {
System.out.println(i);
}
}
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
class A_6{
public int i;
protected int 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;
}
void show() {
teeth = 5;
System.out.println("Teeth: " + teeth);
}
}
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();
}
A)
@Override
public void addInput() { }
}
B)
C)
Ans:a
16. Which is the missing code to successfully compile the below Java program with abstract classes and Interfaces?
interface A {
void a();
}
class C extends B {
// Missing methods
}
A)
@Override
public void a() { }
@Override
void b() {}
B)
@Override
public void a() { }
C)
@Override
void b() {}
Ans:a
interface Calculate {
void cal(int 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
}
}
a) 12345
b) Compilation error '
c) 15
d) Runtime error
Ans:a
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();
}
a) 21 Super Class
b) Super Class 21
c) Compilation error
d) 12 Super Class
Ans. c
a) 12345
b) 15
c) 135
d) 145
Ans: d
Demo() {
a = 10;
}
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