Java@InfyTQ
MCQ Problems (1-20). Day 1
Compiled by Java Team
Dr B P Sharma, Mr Prashant Tomer, Mr Gagan Thakral, Ms Jasmine
Source: https://infytq.onwingspan.com/
Java MCQ 1
Which component is responsible to optimize reusable byte
code conversion to machine code?
a. Class Loader
b. Interpreter
c. JIT Compiler
d. Byte Code Verifier
MCQ 2
John uses the Java compiler to generate (x)_________
from .java file. The extension of this file will be
(y)________.
a. (x) class file (y) .project
b. (x) executable (y) .exe
c. (x) bytecode (y) .class
d. (x) executable (y) .class
MCQ 3
Which of the following are valid identifiers?
a. abc
b. Student
c. class
d. public
e. 1num
f. result
MCQ 4
Identifiers cannot start with _____.
a. character
b. digit
c. _
d. $
MCQ 5
What will be the output of the below code?
public static void main(String args[]) {
int age;
System.out.println(age);
}
a. 0
b. garbage value
c. compilation error : variable needs to be initialized before use
d. 2147483647
MCQ 6
Which of the following are valid variable declarations?
a. float = 10.5f
b. boolean isOn = "true"
c. char gender = 'F'
d. int value = 20
MCQ 7
Which of these values can a boolean variable contain?
a. true
b. false
c. 0
d. 1
MCQ 8
What will be the output of the below code?
public class Demo {
public static void main(String args[]) {
int a = 10;
int b = 2;
System.out.println((a < b) ? a++ : --b);
}
}
a. 2
b. 1
c. 10
d. 11
MCQ 9
What will be the output of the below code?
public static void main(String args[]) {
int i = 10;
if(i)
System.out.println("Hello");
else
System.out.println("Bye");
}
a. Hello
b. Bye
c. compilation error: cannot convert from int to boolean
d. runtime error
MCQ 10
What is the order of precedence (highest to lowest) of the
below operators?
&&, ++ (post increment), =, !
a. ++ , ! , && , =
b. ++ , && , ! , =
c. ! , ++ , && , =
d. ++ , = , && , !
MCQ 11
What will be the output of the below code?
public static void main(String args[]) {
int rollNo = 101;
float newRollNo = rollNo;
System.out.println(newRollNo);
}
a. 101
b. compilation error : cannot convert from int to float
c. 101.0
d. runtime error
MCQ 12
Which is the correct order of widening conversion for the
below data types?
a. byte, double, int, short
b. short --> int --> byte --> double
c. byte --> int --> short --> double
d. byte --> short --> int --> double
e. double --> int --> short --> byte
MCQ 13
What will be the output of the below code?
public static void main(String args[]) {
short number = 32767;
short numberTwo = (short)(number + 1);
System.out.println(numberTwo);
}
a. compilation error: cannot convert from int to short
b. 32767
c. 32768
d. -32768
MCQ 14
If an arithmetic expression contains variables of int, byte
and short data types, what will be the data type of its result?
a. int
b. byte
c. float
d. short
MCQ 15
What will be the output of the below program?
public class Demo {
public static void main(String[] args) {
int num1 = 20;
double num2 = 0;
if (num1 >= 20)
num2 = 1.5;
if (num1 < 30)
num2 = 2;
System.out.println("Value of num2 is " + num2);
}
}
a. Value of num2 is 2
b. Value of num2 is 1.5
c. Value of num2 is 2.0
MCQ 16
What will be the output of the below program?
public class Demo {
public static void main(String[] args) {
int num1 = 25;
int num2 = 34;
if (num1 / 3 >= num2 / 4)
num1 = num1 + 1;
else
num2 = num2 + 1;
System.out.println(num1 + "," + num2);
}
}
a. 25,35
b. 25,34
c. 26,34
d. 26,36
MCQ 17
What will be the output of the below program?
public class Demo {
public static void main(String[] args) {
boolean x = true;
boolean y = false;
if (x && y)
System.out.println(true);
else
System.out.println(false);
}
}
a. true
b. false
c. true false
MCQ 18
What will be the output of the below program?
a. a = 32 b = 76
b. a = 33 b = 76
public class Demo {
c. a = 33 b = 77
public static void main(String[] args) {
d. a = 32 b = 77
int a = 12 * 3 - 9 / 2;
int b = 14 * 4 + 175 / 8;
if (a++ % 2 == 0) {
if (b-- % 4 == 0)
System.out.println("a = " + a + " b = " + b);
else
System.out.println("a = " + a + " b = " + b);
} else {
System.out.println("a = " + a + " b = " + --b);
}
}
}
MCQ 19
What will be the output of the below program?
public class Demo {
public static void main(String[] args) { a. 1
int num1 = 100;
b. 2
int num2 = 200;
c. 3
d. 13
int num3 = 6;
if (5 >= num3) {
if (num1 > 100 || num2 > 150) {
System.out.println("1");
}
} else if (num1 >= 100 && num2 > 150) {
System.out.println("2");
} else {
System.out.println("3");
}
}
}
MCQ 20
What should be the value of num1 and num2 to get the output as 2?
public class Demo { a. num1 = -10, num2 = 3
public static void main(String[] args) { b. num1 = 5, num2 = 1
int num1; c. num1 = 0, num2 = 5
d. num1 =11, num2 = 2
int num2;
if ((num1 / num2 == 5) && (num1 + num2) > 5) {
System.out.println("1");
} else if ((num1 - num2) >= 1 || (num1 % num2) == 0)
{
System.out.println("2");
} else {
System.out.println("3");
}
}
}
What makes Java Platform Independent?
Rules applied to the identifier
It can contain alphanumeric characters([a-z], [A-Z], [0-9]), dollar sign ($), underscore (_)
It should not start with a digit ([0-9])
It should not have spaces
It should not be a Java keyword
It is case-sensitive
It has no length restrictions
Primitive Types and their default values in Java
Truth Table of Logical Operators
Used in MCQ 17
Operators Precedence Table
Widening Conversion among Data Types (Implicit Conversion)
Used in MCQ 14