Explore topic-wise MCQs in Java Programming.

This section includes 10 Mcqs, each offering curated multiple-choice questions to sharpen your Java Programming knowledge and support exam preparation. Choose a topic below to get started.

1.

What is the output of this program?

class box_area
{
int w;
int l;
int h;
box_area()
{
w = 7;
l = 4;
h = 2;
}
void volume()
{
int vol = w * h * l;
System.out.println(vol);
}
}
public class cons_method
{
public static void main(String args[])
{
box_area obj = new box_area();
obj.volume();
}
}

A. 36
B. 46
C. 54
D. 56
E. 66
Answer» E. 66
2.

What is the output of this program?

class modifier
{
static int p;
static int q;
void addition(int n1 , int n2)
{
p = n1 + n2;
q = p + n2;
}
}
public class static_use
{
public static void main(String args[])
{
modifier obj1 = new modifier();
modifier obj2 = new modifier();
int n1 = 5;
obj1.addition(n1, n1 + 3);
obj2.addition(7, n1);
System.out.println(obj1.p + " " + obj2.q);
}
}

A. 12 17
B. 17 12
C. 11 12
D. 12 11
E. None of these
Answer» B. 17 12
3.

What is the output of this program?

public class Result
{
static void main(String args[])
{
int p , q = 1;
p = 20;
if(p != 20 && p / 0 == 0)
System.out.println(q);
else
System.out.println(++q);
}
}

A. 2
B. 0
C. Compilation error
D. Runtime error
E. None of these
Answer» D. Runtime error
4.

What is the output of this program?


A. 1 25
B. 20 1
C. 30 20
D. 25 1
E. 1 20
Answer» E. 1 20
5.

What is the output of this program?

class access_Example
{
public int p;
private int q;
void calc(int n1, int n2)
{
p = n1 + 1;
q = n2;
}
}
public class access_specifier
{
public static void main(String args[])
{
access_Example obj = new access_Example();
obj.cal(5, 7);
System.out.println(obj.p + " " + obj.q);
}
}

A. 5 7
B. 7 7
C. Compiletime error
D. Runtime error
E. None of these
Answer» D. Runtime error
6.

What is the output of this program?

class static_Access
{
static int p;
static int q;
void add(int n1, int n2)
{
p = n1 + n2;
q = p + n2;
}
}
public class static_output
{
public static void main(String args[])
{
static_Access obj1 = new static_Access();
static_Access obj2 = new static_Access();
int n1 = 5;
obj1.add(n1, n1 + 2);
obj2.add(6, n1);
System.out.println(obj1.p + " " + obj2.q);
}
}

A. 11 16
B. 16 11
C. 17 11
D. 11 17
E. None of these
Answer» B. 16 11
7.

What is the output of this program?

class modifier
{
static int p;
void method()
{
p++;
}
}
public class static_keyword
{
public static void main(String args[])
{
modifier obj1 = new modifier();
modifier obj2 = new modifier();
obj1.p = 5;
obj1.method();
obj2.method();
System.out.println(obj1.p + " " + obj2.p);
}
}

A. 9 9
B. 8 8
C. 7 7
D. 6 6
E. 5 5
Answer» D. 6 6
8.

What is the output of this program?

class Modifier
{
public int p;
static int q;
void cal(int n1, int n2)
{
p += n1;
q += n2;
}
}
public class static_keyword
{
public static void main(String args[])
{
Modifier obj1 = new Modifier();
Modifier obj2 = new Modifier();
obj1.p = 2;
obj1.q = 3;
obj1.cal(4, 5);
obj2.p = 4;
obj2.cal(4, 6);
System.out.println(obj1.p + " " + obj2.q);
}
}

A. 6 14
B. 7 14
C. 14 6
D. 14 7
E. None of these
Answer» B. 7 14
9.

What happens if constructor of class N is made private?

A. Any class can instantiate objects of class N
B. Inherited class can instantiate objects of class N
C. classes within the same package as class A can instantiate objects of class N
D. Objects of class N can be instantiated only within the class where it is declared
E. None of these
Answer» E. None of these
10.

Can a class be declared with protected modifier?

A. False
B. True
C. NA
D. NA
E. NA
Answer» B. True