Java MCQ's Questions Set 3
Q.1. What will be the output of the following program?
public class First_C {
public void myMethod()
{ System.out.println("Method"); }
{
System.out.println(" Instance Block");
}
public void First_C()
{ System.out.println("Constructor "); }
static {
System.out.println("static block"); }
public static void main(String[] args) {
First_C c = new First_C();
c.First_C();
c.myMethod();
}
}
|
- Instance block, method, static block, and constructor
- Method, constructor, instance block, and static block
- Static block, method, instance block, and constructor
- Static block, instance block, constructor, and method
Answer:- (4)
Q.2. What will be the output of the following Java code?
class bool_operator
{
public static void main(String args[])
{
boolean a = true;
boolean b = !true;
boolean c = a | b;
boolean d = a & b;
boolean e = d ? b : c;
System.out.println(d + " " + e);
}
}
|
- false false
- true ture
- true false
- false true
Answer:- (4)
Q.3. What will be the output of the following program?
public class MyFirst {
public static void main(String[] args) {
MyFirst obj = new MyFirst(n);
}
static int a = 10;
static int n;
int b = 5;
int c;
public MyFirst(int m) {
System.out.println(a + ", " + b + ", " + c + ", " + n + ", " + m);
}
// Instance Block
{
b = 30;
n = 20;
}
// Static Block
static
{
a = 60;
}
}
|
- 10, 5, 0, 20, 0
- 10, 30, 20
- 60, 5, 0, 20
- 60, 30, 0, 20, 0
Answer:- (4)
Q.4. Which of the following is/are true about constructors in Java?
- Constructor name should be same as class name.
- If you don't define a constructor for a class, a default parameterless constructor is automatically created by the compiler.
- The default constructor calls super() and initializes all instance variables to default value like 0, null.
- If we want to parent class constructor, it must be called in first line of constructor.
(A). 1
(B). 1, 2
(C). 1,2,4
(D). 1,2,3 and 4
Answer:- (D)
Q.5. Predict the output of following Java program
class Test {
int i;
}
class Main {
public static void main(String args[]) {
Test t = new Test();
System.out.println(t.i);
}
}
|
- Garbage value
- 0
- Compile Error
- Runtime Error
Answer:- (2)
Q.6. Predict the output of the following program.
class First
{
void display()
{
System.out.println("Inside First");
}
}
class Second extends First
{
void display()
{
System.out.println("Inside Second");
}
}
class Test
{
public static void main(String[] args)
{
First obj1 = new First();
Second obj2 = new Second();
First ref;
ref = obj1;
ref.display();
ref = obj2;
ref.display();
}
}
|
1. Compilation Error
2. Inside First
Inside Second
3. Inside First
Inside First
4. Runtime Error
Answer:- (2)
Q.7. Which method of DataInputStream class reads a line from the file and returns it as a string?
- WriteInt()
- readLine()
- readInt()
- writeDouble()
Answer:- (2)
Q.8. Which state is the thread still alive, but is currently not eligible to run?
- Non-Runnable
- Terminated
- Runnable
- Running
Answer:- (1)
Q.9. Nested interface must be public if it is declared outside the interface but it cannot have any access modifier if declared within the class.
- True
- False
Answer:- (2)
Q.10. In Method overriding a subclass in a different package can only override the non-final methods declared public or protected
- True
- False
Answer:- (1)
For Java Interview Questions Click here
For more Technical MCQ's and Interview Questions Click here