Java Variables, Data Types and Loops MCQs: 12 Solved Questions with Output Traces
Trace 12 Java MCQs on variables, primitive and reference types, literals, expressions and loop syntax. Each answer includes the rule and exact output.
KnowledgeGate Team
Exam prep & CS education

A Java MCQ can look like a memory test, but it usually turns on one distinction: class versus local variable, primitive versus reference type, decimal versus octal literal, or loop header versus body. Identify the rule, trace the values, and then compare the options. The concept-first Variables, Data Types and Loops in Java: Worked Traces and Exam Traps post builds the model; the published Loops in Java Tutorial: for, while, do-while and Worked Examples post owns the full loop walkthrough. These 12 solved questions apply the rules to exam-style options and exact traces.
Java variables: defaults, identifiers and post-increment
static int defaults to 0; identifiers allow letters, digits, _ and $, not -; a++ produces the old value before changing a.
Question 1: default value of a static int
(Beltron Programmer 2025, Shift-3)
In Java, what is the default value of an uninitialised static variable of type int declared in a class?
(a) Garbage value
(b) Compiler Error
(c) Null
(d) 0
Answer: (d) 0. A static int is a class variable, so Java initialises it to 0 when the class is initialised. This does not apply to a local int, which must be definitely assigned before it can be read.
Question 2: valid Java variable names
(RPSC 2024, Programmer - P2)
Which of the following is not a valid variable name in Java language?
(a) x15
(b) rate
(c) rate_of_taxi
(d) rate-of-taxi
Answer: (d) rate-of-taxi. Java reads - as the subtraction operator, so it cannot occur inside an identifier. x15, rate and rate_of_taxi all follow the identifier-character rule relevant to these options.
Question 3: post-increment output
(RPSC 2024, Programmer - P2)
What will be the output of the code after the given code executes?
int a = 5;
int b = a++;
System.out.println("a=" + a + " b=" + b);(a) a = 6 b = 5
(b) a = 5 b = 6
(c) a = 6 b = 6
(d) a = 5 b = 5
Answer: (a) a = 6 b = 5. The state change is a=5 -> b=5 -> a=6: post-increment gives the old value 5 to b, then increments a. The exact console string is a=6 b=5, without spaces around the equals signs, but its values match option (a).

Java data types: fixed widths, primitives and type discipline
Java fixes its primitive types across platforms. Do not transfer C integer-size assumptions into Question 4.
Rule | Java fact |
|---|---|
Integer width |
|
Other primitives |
|
Reference type |
|
Type discipline | Strong and static typing describe different properties |
Question 4: size of int, topic practice hub
(RPSC 2024, Programmer - P2)
Size of “int” data type in Java language is –
(a) 64-bits
(b) 16-bits
(c) 8-bits
(d) 32-bits
Answer: (d) 32-bits. Java defines int as a signed 32-bit integral type. Its range, -2,147,483,648 to 2,147,483,647, checks the 32-bit two's-complement width; it says nothing about implementation-dependent integer sizes in C.
Question 5: primitive versus reference type
In Java, which of the following is not a primitive data type?
(a) boolean
(b) byte
(c) String
(d) double
Answer: (c) String. boolean, byte and double are primitive type keywords, while capitalised String names the java.lang.String class. In String name = "KG";, name holds a reference, unlike the primitive values in byte n = 8; and double x = 8.0;.
Question 6: Java type discipline
(Kendriya Vidyalaya Sangathan 2017)
Java is a ______ language. This means that you must be explicit about what type of data you are working with.
(a) weakly typed
(b) strongly typed
(c) dynamically typed
(d) loosely typed
Answer: (b) strongly typed. Assignments and operations must satisfy Java's type rules, so String s = 5; is rejected while long total = 5; is an allowed widening conversion. The stem also evokes static typing, but that option is absent, and strong typing is not a synonym for static typing.
Java expressions inside loops: concatenation and octal literals
For +, work left to right: "exam:" + 9 -> "exam:9", then "exam:99", then "exam:999". By contrast, 9 + 9 + 9 + "exam" calculates 27 first and produces "27exam".
A leading zero changes the base: octal 016 = 1 x 8 + 6 = 14, and the array stores 14, not the spelling 016.
Question 7: string concatenation order
What is the output of the following JAVA code?
String s = "exam:" + 9 + 9 + 9;
System.out.println(s);(a) exam 27
(b) exam:27
(c) exam:9
(d) exam:999
Answer: (d) exam:999. The first operation already has a String operand, so each following + 9 concatenates instead of adding. The intermediate strings are exam:9, exam:99 and exam:999.
Question 8: octal literal inside an array
(RPSC 2024, Programmer - P2)
What will be the output of the following program?
public class Demo {
public static void main(String[] args) {
int[] arr = {120, 200, 016};
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}(a) 120 200 14
(b) 120 200 016
(c) Compilation Error
(d) 120 200 16
Answer: (a) 120 200 14. Convert the third initializer first: octal 016 = 1 x 8 + 6 = 14. Since arr.length=3, the loop visits indices 0, 1 and 2, producing the exact print stream 120 200 14 with a trailing space; its visible values match option (a).
Java loop syntax: while, for and do-while
Loop | Canonical Java form | Semicolon rule |
|---|---|---|
|
| No semicolon between the header and its block |
|
| Two separators inside the header |
|
| A semicolon follows the final condition |
Question 9: while-loop syntax, topic practice hub
(LTI Mindtree 2024)
Choose the correct syntax of the WHILE loop in Java below.
(a)
while(condition) { //statements }(b)
while(condition); { //statements }(c)
while { //statements }(condition)(d) None
Answer: (a) while(condition) { //statements }. This is the canonical form whose braces contain the loop body. Java can parse the semicolon in option (b) as an empty loop body and its braces as a separate block, making it dangerous rather than the intended while-with-block form; option (c) reverses the grammar.
Question 10: for-loop syntax, topic practice hub
(LTI Mindtree 2023)
Choose the correct syntax of a FOR loop in Java from the options below.
(a)
for(initialization; condition; increment-or-decrement)
{
//statements
}(b)
for(condition; increment-or-decrement; initialization)
{
//statements
}(c)
for(increment-or-decrement; condition; initialization)
{
//statements
}(d) None
Answer: (a). In for (int i=0; i<3; i++), int i=0 runs once, i<3 is checked before each iteration, and i++ runs after each body. The body sees i=0, i=1 and i=2, then the condition fails at i=3.
Question 11: do-while syntax, topic practice hub
(LTI Mindtree 2024)
Choose the correct syntax of the DO WHILE loop in Java below.
(a)
do { //statements }while(condition);(b)
do { //statements }while(condition)(c)
do while(condition) { //statements }(d) None
Answer: (a) do { //statements }while(condition);. A do-while runs its body once before the first condition check, and its trailing condition statement ends with a semicolon. With int i=3; do { i--; } while(i>0);, the body produces 2, 1 and 0, followed by a false check at i=0.
Java worked loop trace: where two values meet
After check n, m=1000+n, k=3000-n, and m+k=4000.
Question 12: prefix updates in an empty while loop
What is the output of the following Java code? int m = 1000;
int k = 3000;
while (++m < --k);
System.out.println(m);(a) Error
(b) 1000
(c) 2000
(d) 4000
Answer: (c) 2000. The pre-increment and pre-decrement run before every comparison, including the final failed comparison. Checks 1 through 999 satisfy m<k; check 1000 first produces m=2000 and k=2000, then evaluates 2000<2000 as false, so the final print is 2000.
Check |
|
|
|
|
|---|---|---|---|---|
0 | 1000 | 3000 | not yet | 4000 |
1 | 1001 | 2999 | true | 4000 |
999 | 1999 | 2001 | true | 4000 |
1000 | 2000 | 2000 | false | 4000 |
The semicolon is the empty body, but both prefix operations still run on every evaluation.

Java MCQ error-pattern revision map
Write one reason beside every mistake: scope rule, type rule, literal/evaluation rule, or loop-control rule.
Error pattern | Questions | Revision checkpoint |
|---|---|---|
Scope/defaults | Question 1 |
|
Identifiers and side effects | Questions 2-3 |
|
Data types | Questions 4-6 |
|
Expression reading | Questions 7-8 |
|
Loop grammar | Questions 9-11 | Condition placement and the one required |
Boundary tracing | Question 12 |
|
Retest the output questions, then choose your next Java step
Without notes, predict the exact output of Questions 3, 7, 8 and 12 before running them: post-increment 5 -> 6, string concatenation exam:9 -> exam:999, octal 016 -> 14, and the meeting point 2000/2000.
Use the Java Course: Concepts, MCQs & Coding Questions for fundamentals, DSA using Java: Placement Preparation Course for data structures, or Coding & Skill Development Courses to compare the catalog.
Keep learning
Variables, Data Types and Loops in Java: Worked Traces and Exam Traps
Learn to trace Java programs by tracking each variable's type, scope and current value through every condition and update.
C Data Types Interview Questions and Answers: 12 Model Answers with Output Traces
Prepare C fundamentals with speakable answers, portable type rules and step-by-step output traces for integer division, promotions and unsigned conversion.
Python Tokens, Variables, Data Types and I/O: Worked Examples from Input to Output
Trace one Python program from keyboard text through name binding, numeric conversion, a Boolean decision and formatted output, then test the common traps.
Loops and Iteration Interview Questions in C: 14 Model Answers with Exact Output Traces
Can you predict a C loop after continue moves an update or unsigned arithmetic crosses zero? Work through 14 interview-ready answers, exact traces and termination checks.





