1. What will be the output?
public class Test1 {
public static void main(String[] args) {
int a = 5;
double b = 2;
System.out.println(a / b);
}
}
A) 2.0
B) 2.5
C) 2
D) Compile error
Answer: B) 2.5
Explanation: a is promoted to double before division, resulting in floating-point
division.
2. Which of the following identifiers are valid in Java?
int _num1 = 10;
int $value = 20;
int total#sum = 30;
int class = 40;
A) _num1 and $value only
B) All are valid
C) _num1, $value, total#sum
D) None are valid
Answer: A) _num1 and $value only
Explanation: Identifiers cannot contain # or be keywords like class.
3.what will happen here?
public class Test5 {
public static void main(String[] args) {
int a = 5;
long b = a;
int c = b;
System.out.println(c);
}
}
A) 5
B) Compile error
C) Runtime error
D) 0
Answer: B) Compile error
Explanation: Implicit narrowing (long → int) is not allowed without explicit
casting.
4. What is the output?
public class Test6 {
public static void main(String[] args) {
int a = 5;
float b = 2.0f;
System.out.println(a / b);
}
}
A) 2
B) 2.0
C) 2.5
D) 2.500000
Answer: C) 2.5
Explanation: a is promoted to float, so floating-point division occurs.
5. Which statement is true about this code?
public class Test7 {
public static void main(String[] args) {
int _x = 10;
int $y = 20;
int 2z = 30;
System.out.println(_x + $y);
}
}
A) All identifiers are valid
B) Compile error because 2z is invalid
C) Compile error because $y is invalid
D) Runs successfully and prints 30
Answer: B) Compile error because 2z is invalid
Explanation: Identifiers cannot start with a digit.
6. Output of the program?
public class Test8 {
public static void main(String[] args) {
int a = 'A';
System.out.println(a);
}
}
A) 65
B) 'A'
C) Compile error
D) Garbage value
Answer: A) 65
Explanation: char value 'A' is implicitly converted to its ASCII code.
7. What is the output?
public class Test9 {
public static void main(String[] args) {
double num = 5 / 2;
System.out.println(num);
}
}
A) 2.0
B) 2.5
C) 2
D) Compile error
Answer: A) 2.0
Explanation: 5/2 is integer division before assignment, so result is 2 which is
then stored as 2.0.
8. What will be the result?
public class Test10 {
public static void main(String[] args) {
var value = 100;
value = 200;
System.out.println(value);
}
}
A) 100
B) 200
C) Compile error
D) Garbage value
Answer: B) 200
Explanation: var infers type at compile-time (int here). Value can be reassigned if
not final.
9.
public class Test1 {
public static void main(String[] args) {
int x;
// System.out.println(x); // Line A
}
}
Question:
What happens if Line A is uncommented?
A) Prints 0
B) Compilation error
C) Runtime error
D) Prints null
Answer: B – Local variables must be initialized before use.
10. Default Values of Instance Variables
class Test2 {
int num;
public static void main(String[] args) {
Test2 obj = new Test2();
System.out.println(obj.num);
}
}
Question:
What will be the output?
A) 0
B) Compilation error
C) null
D) Garbage value
Answer: A – Instance variables have default values (0 for int).
11.
public class Test5 {
public static void main(String[] args) {
double d = 9.78;
int i = (int) d;
System.out.println(i);
}
}
Question:
What will be the output?
A) 9.78
B) 9
C) 10
D) Compilation error
Answer: B – Casting double to int truncates decimal part.
12. What will be the output of the following Java program?
class Test {
public static void main(String[] args) {
int _value = 50;
int $number = 20;
System.out.println(_value + $number);
}
}
A. 5020
B. 70
C. Compilation Error
D. Runtime Error
Correct Answer: B
Explanation: _value and $number are valid identifiers; addition results in 70.
13. Which statement about the identifiers in this program is correct?
class Demo {
public static void main(String[] args) {
int class = 5;
System.out.println(class);
}
}
A. Prints 5
B. Compilation Error – class is a keyword
C. Compilation Error – Invalid syntax for integer
D. Runtime Error
Correct Answer: B
Explanation: class is a reserved keyword and cannot be used as an identifier.
14. What will be the output of this program?
class Main {
public static void main(String[] args) {
int _ = 100;
int __ = 200;
System.out.println(_ + __);
}
}
A. 300
B. Compilation Error – _ is invalid
C. 100200
D. Runtime Error
Correct Answer: A
Explanation: _ and __ are valid identifiers in Java (before Java 9; after Java 9, _
alone is reserved but __ is valid).
15. Which of the following variable declarations will cause a compilation error in
Java?
class Test {
public static void main(String[] args) {
int total#marks = 90;
int marks_1 = 85;
int $marks = 75;
System.out.println(marks_1 + $marks);
}
}
A. int total#marks = 90;
B. int marks_1 = 85;
C. int $marks = 75;
D. None of the above
Correct Answer: A
Explanation: # is not allowed in Java identifiers.