Java MCQ's
1) Predict the output of following Java Programs.
Program 1:
// filename Main.java
class Test {
protected int x, y;
}
class Main {
public static void main(String args[]) {
Test t = new Test();
System.out.println(t.x + " " + t.y);
}
}
Output: 0 0
Explanation:
In Java, a protected member is accessible in all classes of same package and in inherited classes of other packages. Since Test and Main are in same package, no access related problem in the above program. Also, the default constructors initialize integral variables as 0 in Java (See this GFact for more details). That is why we get output as 0 0.
Program 2:
// filename Test.java
class Test {
public static void main(String[] args) {
for(int i = 0; 1; i++) {
System.out.println("Hello");
break;
}
}
}
Output: Compiler Error
Explanation:
There is an error in condition check expression of for loop. Java differs from C++(or C) here. C++ considers all non-zero values as true and 0 as false. Unlike C++, an integer value expression cannot be placed where a boolean is expected in Java. Following is the corrected program.
// filename Test.java
class Test {
public static void main(String[] args) {
for(int i = 0; true; i++) {
System.out.println("Hello");
break;
}
}
}
// Output: Hello
Program 3:
// filename Main.java
class Main {
public static void main(String args[]) {
System.out.println(fun());
}
int fun() {
return 20;
}
}
Output: Compiler Error
Explanation:
Like C++, in Java, non-static methods cannot be called in a static method. If we make fun() static, then the program compiles fine without any compiler error. Following is the corrected program.
// filename Main.java
class Main {
public static void main(String args[]) {
System.out.println(fun());
}
static int fun() {
return 20;
}
}
// Output: 20
Program 4:
// filename Test.java
class Test {
public static void main(String args[]) {
System.out.println(fun());
}
static int fun() {
static int x= 0;
return ++x;
}
}
Output: Compiler Error
class Test {
private static int x;
public static void main(String args[]) {
System.out.println(fun());
}
static int fun() {
return ++x;
}
}
// Output: 1
Explanation:
In Java, a static variable is a class variable (for whole class). So if we have static local variable (a variable with scope limited to function), it violates the purpose of static. Hence compiler does not allow static local variable.
5) Static members are not inherited to subclass.
a) True
b) False
Answer: b
Explanation: Static members are also inherited to subclasses.Static methods in Java are inherited, but can not be overridden. If you declare the same method in a subclass, you hide the superclass method instead of overriding it. Static methods are not polymorphic. At the compile time, the static method will be statically linked
class A {
public static void display() {
System.out.println("Inside static method of superclass");
}
}
class B extends A {
public void show() {
display();
}
public static void display() {
System.out.println("Inside static method of this class");
}
}
public class Test {
public static void main(String[] args) {
B b = new B();
// prints: Inside static method of this class
b.display();
A a = new B();
// prints: Inside static method of superclass
a.display();
}
}
This is due to static methods are class methods.
A.display() and B.display() will call method of their respective classes.
6) The while loop repeats a set of code while the condition is not met?
a) True
b) False
Answer: b
Explanation: While loop repeats a set of code only until the condition is met.
7) What is the valid data type for variable “a” to print “Hello World”?
switch(a)
{
System.out.println("Hello World");
}
a) int and float
b) byte and short
c) char and long
d) byte and char
Answer: d
Explanation: The switch condition would only meet if variable “a” is of type byte or char.
8) Which of the following is a type of polymorphism in Java?
a) Compile time polymorphism
b) Execution time polymorphism
c) Multiple polymorphism
d) Multilevel polymorphism
Answer: a
Explanation: There are two types of polymorphism in Java. Compile time polymorphism (overloading) and runtime polymorphism (overriding).
9)What is it called if an object has its own lifecycle and there is no owner?
a) Aggregation
b) Composition
c) Encapsulation
d) Association
Answer: d
Explanation: It is a relationship where all objects have their own lifecycle and there is no owner. This occurs where many to many relationships are available, instead of one to one or one to many.
10) What is it called where child object gets killed if parent object is killed?
a) Aggregation
b) Composition
c) Encapsulation
d) Association
Answer: b
Explanation: Composition occurs when child object gets killed if parent object gets killed. Aggregation is also known as strong Aggregation.
11) What will be the output of the following Java program?
class main_class
{
public static void main(String args[])
{
int x = 9;
if (x == 9)
{
int x = 8;
System.out.println(x);
}
}
}
a) 9
b) 8
c) Compilation error
d) Runtime error
Answer: c
Explanation: Two variables with the same name can’t be created in a class.
output:
$ javac main_class.java
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Duplicate local variable x
12) What will be the output of the following Java program?
class box
{
int width;
int height;
int length;
}
class mainclass
{
public static void main(String args[])
{
box obj = new box();
System.out.println(obj);
}
}
a) 0
b) 1
c) Runtime error
d) classname@hashcode in hexadecimal form
Answer: d
Explanation: When we print object internally toString() will be called to return string into this format classname@hashcode in hexadecimal form.
output:
$ javac mainclass.java
$ java mainclass
box@130671e
13) What is the process of defining more than one method in a class differentiated by method signature?
a) Function overriding
b) Function overloading
c) Function doubling
d) None of the mentioned
Answer: b
Explanation: Function overloading is a process of defining more than one method in a class with same name differentiated by function signature i:e return type or parameters type and number. Example – int volume(int length, int width) & int volume(int length , int width , int height) can be used to calculate volume.
14) In the following Java code, which call to sum() method is appropriate?
class Output
{
public static int sum(int ...x)
{
return;
}
static void main(String args[])
{
sum(10);
sum(10,20);
sum(10,20,30);
sum(10,20,30,40);
}
}
a) only sum(10)
b) only sum(10,20)
c) only sum(10) & sum(10,20)
d) all of the mentioned
Answer: d
Explanation: sum is a variable argument method and hence it can take any number as an argument.
15) Which of these keywords can be used to prevent Method overriding?
a) static
b) constant
c) protected
d) final
Answer: d
Explanation: 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.
16) 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: d
Explanation: None.
17) At line number 2 in the following code, 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: d
Explanation: Every interface variable is implicitly public static and final.
18) A class member declared protected becomes a member of subclass of which type?
a) public member
b) private member
c) protected member
d) static member
Answer: b
Explanation: A class member declared protected becomes a private member of subclass.
Happy Coding 💻:-)
Comments
Post a Comment