Java MCQ Set 1
1. Which of these keyword can be used in subclass to call the constructor of superclass?
a) super
b) this
c) extent
d) extends
Answer
Answer: a [Reason:] None.
2. What is the process of defining a method in subclass having same name & type signature as a method in its superclass?
a) Method overloading
b) Method overriding
c) Method hiding
d) None of the mentioned
Answer
Answer: b [Reason:] None.
3. Which of these keywords can be used to prevent Method overriding?
a) static
b) constant
c) protected
d) final
Answer
Answer: d [Reason:] To disallow a method from being overridden, specify final as a modifier at the start of its declaration. Methods declared as final cannot be overridden.
4. Which of these is correct way of calling a constructor having no parameters, of superclass A by subclass B?
a) super(void);
b) superclass.();
c) super.A();
d) super();
Answer
Answer: d [Reason:] None.
5. At line number 2 below, choose 3 valid data-type attributes/qualifiers among “final, static, native, public, private, abstract, protected”
-
public interface Status
-
{
-
/* insert qualifier here */ int MY_VALUE = 10;
-
}
a) final, native, private
b) final, static, protected
c) final, private, abstract
d) final, static, public
Answer
Answer: d [Reason:] Every interface variable is implicitly public static and final.
6. Which of these is supported by method overriding in Java?
a) Abstraction
b) Encapsulation
c) Polymorphism
d) None of the mentioned
Answer
Answer: c [Reason:] None.
7. What is the output of this program?
-
class Alligator
-
{
-
public static void main(String[] args)
-
{
-
int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};
-
int [][]y = x;
-
System.out.println(y[2][1]);
-
}
-
}
a) 2
b) 3
c) 7
d) Compilation Error
Answer
Answer: c [Reason:] Both x,and y are pointing to the same array.
8. What is the output of this program?
-
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
Answer
Answer: d [Reason:] class A has been declared final hence it cannot be inherited by any other class. Hence class B does not have member i, giving compilation error.
output:
$ javac inheritance.java
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
i cannot be resolved or is not a field
9. What is the output of this program?
-
class Abc
-
{
-
public static void main(String[]args)
-
{
-
String[] elements = { "for", "tea", "too" };
-
String first = (elements.length > 0) ? elements[0]: null;
-
}
-
}
a) Compilation error
b) An exception is thrown at run time
c) The variable first is set to null
d) The variable first is set to elements[0].
Answer
Answer: d [Reason:] The value at the 0th position will be assigned to the variable first.
10. What is the output of this program?
-
class A
-
{
-
int i;
-
public void display()
-
{
-
System.out.println(i);
-
}
-
}
-
class B extends A
-
{
-
int j;
-
public void display()
-
{
-
System.out.println(j);
-
}
-
}
-
class Dynamic_dispatch
-
{
-
public static void main(String args[])
-
{
-
B obj2 = new B();
-
obj2.i = 1;
-
obj2.j = 2;
-
A r;
-
r = obj2;
-
r.display();
-
}
-
}
a) 1
b) 2
c) 3
d) 4
Answer
Answer: b [Reason:] r is reference of type A, the program assigns a reference of object obj2 to r and uses that reference to call function display() of class B.
output:
$ javac Dynamic_dispatch.java
$ java Dynamic_dispatch
2
Java MCQ Set 2
1. Which of these package contains classes and interfaces for networking?
a) java.io
b) java.util
c) java.net
d) java.network
Answer
Answer: c [Reason:] None.
2. Which of these is a protocol for breaking and sending packets to an address across a network?
a) TCIP/IP
b) DNS
c) Socket
d) Proxy Server
Answer
Answer: a [Reason:] TCP/IP – Transfer control protocol/Internet Protocol is used to break data into small packets an send them to an address across a network.
3. How many ports of TCP/IP are reserved for specific protocols?
a) 10
b) 1024
c) 2048
d) 512
Answer
Answer: b [Reason:] None.
4. How many bits are in a single IP address?
a) 8
b) 16
c) 32
d) 64
Answer
Answer: c [Reason:] None.
5. Which of these is a full form of DNS?
a) Data Network Service
b) Data Name Service
c) Domain Network Service
d) Domain Name Service
Answer
Answer: d [Reason:] None.
6. Which of these class is used to encapsulate IP address and DNS?
a) DatagramPacket
b) URL
c) InetAddress
d) ContentHandler
Answer
Answer: c [Reason:] InetAddress class encapsulate both IP address and DNS, we can interact with this class by using name of an IP host.
Java MCQ Set 3
1. Which of these class is superclass of every class in Java?
a) String class
b) Object class
c) Abstract class
d) ArrayList class
Answer
Answer: b [Reason:] Object class is superclass of every class in Java.
2. Which of these method of Object class can clone an object?
a) Objectcopy()
b) copy()
c) Object clone()
d) clone()
Answer
Answer: c [Reason:] None.
3. Which of these method of Object class is used to obtain class of an object at run time?
a) get()
b) void getclass()
c) Class getclass()
d) None of the mentioned
Answer
Answer: c [Reason:] None.
4. Which of these keywords can be used to prevent inheritance of a class?
a) super
b) constant
c) class
d) final
Answer
Answer: d [Reason:] Declaring a class final implicitly declares all of its methods final, and makes the class inheritable.
5. Which of these keywords cannot be used for a class which has been declared final?
a) abstract
b) extends
c) abstract and extends
d) none of the mentioned
Answer
Answer: a [Reason:] A abstract class is incomplete by itself and relies upon its subclasses to provide complete implementation. If we declare a class final then no class can inherit that class, an abstract class needs its subclasses hence both final and abstract cannot be used for a same class.
6. Which of these class relies upon its subclasses for complete implementation of its methods?
a) Object class
b) abstract class
c) ArrayList class
d) None of the mentioned
Answer
Answer: b [Reason:] None.
7. What is the output of this program?
-
abstract class A
-
{
-
int i;
-
abstract void display();
-
}
-
class B extends A
-
{
-
int j;
-
void display()
-
{
-
System.out.println(j);
-
}
-
}
-
class Abstract_demo
-
{
-
public static void main(String args[])
-
{
-
B obj = new B();
-
obj.j=2;
-
obj.display();
-
}
-
}
a) 0
b) 2
c) Runtime Error
d) Compilation Error
Answer
Answer: b [Reason:] class A is an abstract class, it contains a abstract function display(), the full implementation of display() method is given in its subclass B, Both the display functions are the same. Prototype of display() is defined in class A and its implementation is given in class B.
output:
$ javac Abstract_demo.java
$ java Abstract_demo
2
8. What is the output of this program?
-
class A
-
{
-
int i;
-
int j;
-
A()
-
{
-
i = 1;
-
j = 2;
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
A obj1 = new A();
-
A obj2 = new A();
-
System.out.print(obj1.equals(obj2));
-
}
-
}
a) false
b) true
c) 1
d) Compilation Error
Answer
Answer: a [Reason:] obj1 and obj2 are two different objects. equals() is a method of Object class, Since Object class is superclass of every class it is available to every object.
output:
$ javac Output.java
$ java Output
false
9. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
Object obj = new Object();
-
System.out.print(obj.getclass());
-
}
-
}
a) Object
b) class Object
c) class java.lang.Object
d) Compilation Error
Answer
Answer: c [Reason:] None.
output:
$ javac Output.java
$ java Output
class java.lang.Object
10. What is the output of this program?
-
class A
-
{
-
int i;
-
int j;
-
A()
-
{
-
i = 1;
-
j = 2;
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
A obj1 = new A();
-
System.out.print(obj1.toString());
-
}
-
}
a) true
b) false
c) String associated with obj1
d) Compilation Error
Answer
Answer: c [Reason:] toString() is method of class Object, since it is superclass of every class, every object has this method. toString() returns the string associated with the calling object.
output:
$ javac Output.java
$ java Output
Java MCQ Set 4
1. What is process of defining two or more methods within same class that have same name but different parameters declaration?
a) method overloading
b) method overriding
c) method hiding
d) none of the mentioned
Answer
Answer: a [Reason:] Two or more methods can have same name as long as their parameters declaration is different, the methods are said to be overloaded and process is called method overloading. Method overloading is a way by which Java implements polymorphism.
2. Which of these can be overloaded?
a) Methods
b) Constructors
c) All of the mentioned
d) None of the mentioned
Answer
Answer: c [Reason:] None.
3. Which of these is correct about passing an argument by call-by-value process?
a) Copy of argument is made into the formal parameter of the subroutine
b) Reference to original argument is passed to formal parameter of the subroutine
c) Copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have effect on original argument
d) Reference to original argument is passed to formal parameter of the subroutine and changes made on parameters of subroutine have effect on original argument
Answer
Answer: a [Reason:] When we pass an argument by call-by-value a copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have no effect on original argument, they remain the same.
4. What is the process of defining a method in terms of itself, that is a method that calls itself?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion
Answer
Answer: d [Reason:] None.
5. What is the output of the following code?
-
class San
-
{
-
public void m1 (int i,float f)
-
{
-
System.out.println(" int float method");
-
}
-
-
public void m1(float f,int i);
-
{
-
System.out.println("float int method");
-
}
-
-
public static void main(String[]args)
-
{
-
San s=new San();
-
s.m1(20,20);
-
}
-
}
a) int float method
b) float int method
c) compile time error
d) run time error
Answer
Answer: c [Reason:] While resolving overloaded method, compiler automatically promotes if exact match is not found. But in this case, which one to promote is an ambiguity.
6. What is the output of this program?
-
class overload
-
{
-
int x;
-
int y;
-
void add(int a)
-
{
-
x = a + 1;
-
}
-
void add(int a, int b)
-
{
-
x = a + 2;
-
}
-
}
-
class Overload_methods
-
{
-
public static void main(String args[])
-
{
-
overload obj = new overload();
-
int a = 0;
-
obj.add(6);
-
System.out.println(obj.x);
-
}
-
}
a) 5
b) 6
c) 7
d) 8
Answer
Answer: c [Reason:] None.
output:
$ javac Overload_methods.java
$ java Overload_methods
7
7. What is the output of this program?
-
class overload
-
{
-
int x;
-
int y;
-
void add(int a)
-
{
-
x = a + 1;
-
}
-
void add(int a , int b)
-
{
-
x = a + 2;
-
}
-
}
-
class Overload_methods
-
{
-
public static void main(String args[])
-
{
-
overload obj = new overload();
-
int a = 0;
-
obj.add(6, 7);
-
System.out.println(obj.x);
-
}
-
}
a) 6
b) 7
c) 8
d) 9
Answer
Answer: c [Reason:] None.
output:
$ javac Overload_methods.java
$ java Overload_methods
8
8. What is the output of this 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;
-
}
-
}
-
class Overload_methods
-
{
-
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) 6 6
b) 6.4 6.4
c) 6.4 6
d) 4 6.4
Answer
Answer: d [Reason:] For obj.add(a,a); ,the function in line number 4 gets executed and value of x is 4. For the next function call, the function in line number 7 gets executed and value of y is 6.4
output:
$ javac Overload_methods.java
$ java Overload_methods
4 6.4
9. What is the output of this program?
-
class test
-
{
-
int a;
-
int b;
-
void meth(int i , int j)
-
{
-
i *= 2;
-
j /= 2;
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
test obj = new test();
-
int a = 10;
-
int b = 20;
-
obj.meth(a , b);
-
System.out.println(a + " " + b);
-
}
-
}
a) 10 20
b) 20 10
c) 20 40
d) 40 20
Answer
Answer: a [Reason:] Variables a & b are passed by value, copy of their values are made on formal parameters of function meth() that is i & j. Therefore changes done on i & j are not reflected back on original arguments. a & b remain 10 & 20 respectively.
output:
$ javac Output.java
$ java Output
10 20
10. What is the output of this program?
-
class test
-
{
-
int a;
-
int b;
-
test(int i, int j)
-
{
-
a = i;
-
b = j;
-
}
-
void meth(test o)
-
{
-
o.a *= 2;
-
O.b /= 2;
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
test obj = new test(10 , 20);
-
obj.meth(obj);
-
System.out.println(obj.a + " " + obj.b);
-
}
-
}
a) 10 20
b) 20 10
c) 20 40
d) 40 20
Answer
Answer: b [Reason:] Class objects are always passed by reference, therefore changes done are reflected back on original arguments. obj.meth(obj) sends object obj as parameter whose variables a & b are multiplied and divided by 2 respectively by meth() function of class test. a & b becomes 20 & 10 respectively.
output:
$ javac Output.java
$ java Output
20 10
Java MCQ Set 5
1. What is Recursion in Java?
a) Recursion is a class
b) Recursion is a process of defining a method that calls other methods repeatedly
c) Recursion is a process of defining a method that calls itself repeatedly
d) Recursion is a process of defining a method that calls other methods which in turn call again this method
Answer
Answer: b [Reason:] Recursion is the process of defining something in terms of itself. It allows us to define method that calls itself.
2. Which of these data types is used by operating system to manage the Recursion in Java?
a) Array
b) Stack
c) Queue
d) Tree
Answer
Answer: b [Reason:] Recursions are always managed by using stack.
3. Which of these will happen if recursive method does not have a base case?
a) An infinite loop occurs
b) System stops the program after some time
c) After 1000000 calls it will be automatically stopped
d) None of the mentioned
Answer
Answer: a [Reason:] If a recursive method does not have a base case then an infinite loop occurs which results in stackoverflow.
4. Which of these is not a correct statement?
a) A recursive method must have a base case
b) Recursion always uses stack
c) Recursive methods are faster that programmers written loop to call the function repeatedly using a stack
d) Recursion is managed by Java’s Run – Time environment
Answer
Answer: d [Reason:] Recursion is always managed by operating system.
5. Which of these packages contains the exception Stackoverflow in Java?
a) java.lang
b) java.util
c) java.io
d) java.system
Answer
Answer: a [Reason:] None.
6. What is the output of this program?
-
class recursion
-
{
-
int func (int n)
-
{
-
int result;
-
result = func (n - 1);
-
return result;
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
recursion obj = new recursion() ;
-
System.out.print(obj.func(12));
-
}
-
}
a) 0
b) 1
c) Compilation Error
d) Runtime Error
Answer
Answer: d [Reason:] Since the base case of the recursive function func() is not defined hence infinite loop occurs and results in stackoverflow.
Output:
$ javac Output.javac
$ java Output
Exception in thread “main” java.lang.StackOverflowError
7. What is the output of this program?
-
class recursion
-
{
-
int func (int n)
-
{
-
int result;
-
if (n == 1)
-
return 1;
-
result = func (n - 1);
-
return result;
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
recursion obj = new recursion() ;
-
System.out.print(obj.func(5));
-
}
-
}
a) 0
b) 1
c) 120
d) None of the mentioned
Answer
Answer: b [Reason:] None.
Output:
$ javac Output.javac
$ java Output
1
8. What is the output of this program?
-
class recursion
-
{
-
int fact(int n)
-
{
-
int result;
-
if (n == 1)
-
return 1;
-
result = fact(n - 1) * n;
-
return result;
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
recursion obj = new recursion() ;
-
System.out.print(obj.fact(5));
-
}
-
}
a) 24
b) 30
c) 120
d) 720
Answer
Answer: c [Reason:] fact() method recursively calculates factorial of a number, when value of n reaches 1, base case is excuted and 1 is returned.
Output:
$ javac Output.javac
$ java Output
120
9. What is the output of this program?
-
class recursion
-
{
-
int fact(int n)
-
{
-
int result;
-
if (n == 1)
-
return 1;
-
result = fact(n - 1) * n;
-
return result;
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
recursion obj = new recursion() ;
-
System.out.print(obj.fact(1));
-
}
-
}
a) 1
b) 30
c) 120
d) Runtime Error
Answer
Answer: a [Reason:] fact() method recursively calculates factorial of a number, when value of n reaches 1, base case is excuted and 1 is returned.
Output:
$ javac Output.javac
$ java Output
1
10. What is the output of this program?
-
class recursion
-
{
-
int fact(int n)
-
{
-
int result;
-
if (n == 1)
-
return 1;
-
result = fact(n - 1) * n;
-
return result;
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
recursion obj = new recursion() ;
-
System.out.print(obj.fact(6));
-
}
-
}
a) 1
b) 30
c) 120
d) 720
Answer
Answer: d [Reason:] None.
Output:
$ javac Output.javac
$ java Output
720