- A 2 bytes
- B4 bytes
- C 8 bytes
- D16 bytes
Time Taken:
Correct Answer:
Wrong Answer:
Percentage: %
'\n' in Java?'\n' is a character literal → newline character.
In Java, every character is stored in char type.
char in JavaUnlike C/C++, where char is 1 byte,
In Java, char is always 2 bytes (16 bits) because Java uses Unicode.
'\n' is simply a char, so its size = 2 bytes.
✅ Answer: (a) 2 bytes
%, *, / (multiplicative operators)
+, - (additive operators)
b % c → modulus first
Then multiply * d
Then addition + a
Finally subtraction - e
+ → lower precedence
% → higher precedence
- → lower precedence
* → same precedence as %, but evaluation happens left-to-right
👉 *So the highest precedence operator in this expression is %.
✅ Answer: (b) %
Java keywords are case-sensitive.
They must be written in lowercase only.
(a) If → ❌ Invalid (Java keyword is lowercase if)
(b) BOOLEAN → ❌ Invalid (Java keyword is lowercase boolean)
(c) static → ✅ Valid keyword (used to declare class-level variables/methods)
(d) Switch → ❌ Invalid (Java keyword is lowercase switch)
✅ Answer: (c) static
For a String, length() returns an int (number of characters).
✅ Not a String.
Returns the character (char) at a given index.
✅ Not a String.
Returns a new String with characters replaced.
✅ This one returns a String.
Returns the int index of the substring (or -1 if not found).
✅ Not a String.
Checks for equality between the input expression and case labels → ✅ True
Supports floating point constants → ❌ Not allowed
In Java, switch can only work with:
byte, short, char, int
Their wrapper classes (Byte, Short, Character, Integer)
String
enum types
float and double are not allowed.
break is used to exit the switch block → ✅ True
Case labels are unique → ✅ True
✅ Answer: (b) supports floating point constants
public class LoopTest { public static void main(String[] args) { int count = 0; // to count iterations for (int i = 11; i <= 30; i += 2) { System.out.println("i = " + i); count++; } System.out.println("Total iterations: " + count); } }
for (int i = 11; i <= 30; i += 2)
Sequence: 11, 13, 15, …, 29
Total iterations: (30-11)/2 + 1 = 10 ✅
Looks correct.
for (int i = 11; i <= 30; i += 3)
Sequence: 11, 14, 17, …
Iterations: (30-11)/3 + 1 = 7 ❌
Not correct.
for (int i = 11; i < 20; i++)
Sequence: 11, 12, …, 19
Iterations: 20-11 = 9 ❌
Not correct.
for (int i = 11; i <= 21; i++)
Sequence: 11, 12, …, 21
Iterations: 21-11+1 = 11 ❌
Not correct.
✅ Answer: (a) for (int i=11;i<=30;i+=2)
Method name: compute
Accepts two integer arguments → (int a, int b)
Returns true/false → the return type should be boolean
(a) void compute(int a, int b) → ❌ void means no return value
(b) boolean compute(int a, int b) → ✅ Correct, returns a primitive boolean
(c) Boolean compute(int a, int b) → ✅ Technically correct too, returns wrapper class Boolean, but usually we use boolean for primitives
(d) int compute(int a, int b) → ❌ Returns integer, not true/false
✅ Best Answer: (b) boolean compute(int a, int b)
Note: Option (c) is technically valid but primitive
booleanis standard for such cases.
The statement that brings the control back to the calling method:
break → exits a loop or switch, but control does not go to the calling method.
System.exit(0) → terminates the entire program.
continue → skips the current iteration of a loop, but stays inside the loop.
return → exits from the current method and returns control to the calling method. ✅
Answer: (d) return
We are asked to swap two variables a and b using a third variable t. Let's carefully analyze the correct sequence.
The standard swapping logic using a temporary variable t is:
Declare t → int t = 0;
Store a in t → t = a;
Assign b to a → a = b;
Assign t to b → b = t;
So, the correct order of statements:
int t = 0; → t = a; → a = b; → b = t;
Looking at the labels given in the question:
a = b; → (1)
b = t; → (2)
int t = 0; → (3)
t = a; → (4)
Mapping to our logic: 3 → 4 → 1 → 2 ✅
Answer: (b) (3) (4) (1) (2)