Java Pool - 75 Questions
Java Pool - 75 Questions
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
Ans:B
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
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.
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++;
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;
Ans:D
class Customer {
public int custId;
public String custName;
}
public class Tester {
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?
Ans:c
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
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;
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;
}
class Overload_methods
int a = 2;
double b = 3.2;
obj.add(a, a);
obj.add(b, b);
a) 4 6.4
b) 6.4 6
c) 6.4 6.4
d) 6 6
Ans: a
int i;
class B extends A
int j;
void display()
super.i = j + 1;
class inheritance
obj.i=1;
obj.j=2;
obj.display();
}
a) 2 2
b) 3 3
c) 2 3
d) 3 2
Ans: c
class A
public int i;
public int j;
A()
i = 1;
j = 2;
class B extends A
int a;
B()
super();
}
class super_use
a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error
Ans: a
class A
public int i;
private int j;
class B extends A
void display()
super.j = super.i + 1;
System.out.println(super.i + " " + super.j);
class inheritance
obj.i=1;
obj.j=2;
obj.display();
a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
Ans:d
class A
public int i;
public int j;
A()
{
i = 1;
j = 2;
class B extends A
int a;
B()
super();
class super_use
a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error
Ans:a
class A
int i;
void display()
System.out.println(i);
class B extends A
int j;
void display()
System.out.println(j);
class method_overriding
obj.j=2;
obj.display();
a) 0
b) 1
c) 2
d) Compilation Error
Ans:c
class A
public int i;
protected int j;
class B extends A
int j;
void display()
super.j = 3;
}
}
class Output
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 InterfaceTest4
{
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(); }
A)
@Override
public void addInput() { }
}
B)
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() {}
D) All the above
Ans:a
interface calculate
{
void cal(int item);
}
class display implements calculate
{
int x;
public void cal(int item)
{
x = item * item;
}
}
class Main
{
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();
}
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
import java.io.IOException;
import java.util.EmptyStackException;
a) 12345
b) 15
c) 135
d) 145
Ans: d
A a = 10
B a = 20
C Compilation error
Answer : C
24. 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
Answer: A
Answer: B
32.
class Base {
final public void show() {
System.out.println("Base::show() called");
}
}
33.
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
Answer: A
34.
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
Answer: A
35.
Output of following Java program?
class Base {
public void Print() {
System.out.println("Base");
}
}
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
B. Base
Base
Derived
C. Base
Derived
Base
D. Compiler Error
Answer: A
36.
Predict the output of following program.
class Base {
public void foo() { System.out.println("Base"); }
}
37.
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
Answer: A
38.
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()");
}
}
39.
final class Complex {
private final double re;
private final double im;
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
C. Complex number is SOME_GARBAGE
D. Complex number is Complex@8e2fb5
Here 8e2fb5 is hash code of c
Answer: A
40. What should be the execution order, if a class has a method, static block, instance block, and
constructor, as shown below?
{
System.out.println(" Instance Block");
}
Answer: D
43. Given that Student is a class, how many reference variables and objects are created by the
following code?
Student studentName, studentId;
studentName = new Student();
Student stud_class = new Student();
a. Three reference variables and two objects are created.
b. Two reference variables and two objects are created.
c. One reference variable and two objects are created.
d. Three reference variables and three objects are created.
Answer: A
private Person()
{
age = 24;
}
}
public class Test
{
public static void main(String[] args)
{
Person p = new Person();
System.out.println(p.age);
}
}
A 24
B Compilation error
C Runtime error
D None of the above
Answer: B
try
{
public Test()
{
Thread.sleep(5000);
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
Ans. (c)
Ans. (a)
49. What is the output of the following program?
class Derived
{
public void getDetails(String temp)
{
System.out.println("Derived class " + temp);
}
}
Ans. (c)
class A
{
public A(String s)
{
System.out.print("A");
}
}
a) Compilation fails
b) B
c) C
d) None
Ans. (a)
Ans. (c)
Answer: D
54. Given:
1. interface TestA { String toString(); }
2. public class Test {
3. public static void main(String[] args) {
4. System.out.println(new TestA() {
5. public String toString() { return “test”; }
6. });
7. }
8. }
What is the result?
A. test
B. null
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 1.
Answer: A
55. Given:
55. int []x= {1, 2,3,4, 5};
56. int y[] =x;
57. System.out.println(y[2]); Which is true?
Answer: B
56. Given:
10.int x=0;
11.int y 10;
12. do {
13. y--;
14. ++x;
15. } while (x < 5);
16. System.out.print(x + “,“ + y); What is the result?
A. 5,6
B. 5,5
C. 6,5
D. null
Answer: B
57. Given:
35. int x= 10;
36. do {
37. x--;
38. } while(x< 10);
How many times will line 37 be executed?
A. ten times
B. zero times
C. one to me times
D. more than ten times
Answer: D
58. Given:
11. public static void main(String[] args) {
12. try {
13. args=null;
14. args[0] = “test”;
15. System.out.println(args[0]);
16. } catch (Exception ex) {
17. System.out.println(”Exception”);
18. } catch (NullPointerException npe) {
19. System.out.println(”NullPointerException”);
20. }
21. }
What is the result?
A. test
B. Exception
C. Compilation fails.
D. NullPointerException
Answer: C
Answer: A
Answer: C
61. Which of the modifier can't be used for constructors?
A. public
B. private
C. static
D. protected
Answer: C
Answer: A
Answer: B
Answer: C
Answer: B
Answer: A
67. When a class extends the Thread class ,it should override ............ method of Thread class to
start that thread.
A. start()
B. run()
C. init()
D. go()
Answer: B
Answer: C
69. Given:
11. public static void main(String[] args) {
12. Object obj =new int[] { 1,2,3 };
13. int[] someArray = (int[])obj;
14. for (int i: someArray) System.out.print(i +“ “)
15. }
‘What is the result?
A. 1 2 3
B. Compilation fails because of an error in line 12.
C. Compilation fails because of an error in line 13.
D. Compilation fails because of an error in line 14.
Answer: A
Answer: C
70. A programmer has an algorithm that requires a java.util.List that provides an efficient
implementation of add(0,object), but does NOT need to support quick random access. What
supports these requirements?
A. java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList
Answer: D
Answer: A
75. Given:
12. public class Yippee2 {
13.
14. static public void main(String [] yahoo) {
15. for(int x= 1; x<yahoo.length; x++) {
16. System.out.print(yahoo[x] + “ “);
17. }
18. }
19. }
and the command line invocation:
java Yippee2 a b c
What is the result?
A.a b
B.b c
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: B