First 50 of 200 questions. Each one lists the choices, the correct answer, and an explanation. The checker at the top of this page saves progress in this browser when it is available.
Continue in the practice tool above to load more questions, 50 at a time.
Question 1Programming fundamentals: syntax, variables, data types, operators, expressions
A program declares `int a = 17; int b = 5; int c = a / b; int d = a % b;` using a language where both operands are 32-bit integers (Java, C#, C). What are the resulting values of c and d?
- c = 3, d = 3
- c = 3, d = 2 Correct answer.
- c = 4, d = 2
- c = 3.4, d = 2
Explanation
When both operands of / are integers, the fractional part is discarded, so 17 / 5 gives 3 rather than 3.4. The % operator yields the remainder, computed as 17 - (5 * 3), which is 2.
Question 2Programming fundamentals: syntax, variables, data types, operators, expressions
In a C-family language with the usual precedence table (Java, C#, C), what value does the integer expression `10 + 2 * 3 - 8 / 4` produce?
- 7
- 16
- 14 Correct answer.
- 34
Explanation
Multiplication and division bind tighter than addition and subtraction, so 2 * 3 = 6 and 8 / 4 = 2 are computed first. The expression then reduces to 10 + 6 - 2, and the remaining operators of equal precedence run left to right, giving 14.
Question 3Programming fundamentals: syntax, variables, data types, operators, expressions
Given `System.out.println(1 + 2 + "3" + 4 + 5);` in Java, what is printed?
- The code does not compile because an int cannot be added to a String
- 12345
- 15
- 3345 Correct answer.
Explanation
The + operator is evaluated left to right. Both 1 and 2 are ints, so 1 + 2 is the numeric addition 3; as soon as the String "3" is reached, every following + becomes concatenation, producing "3345".
Question 4Programming fundamentals: syntax, variables, data types, operators, expressions
In a language with short-circuit && (such as Java or C#), the code `int x = 0; boolean ok = (x != 0) && (10 / x > 2);` is executed. What is the outcome?
- ok is true because the division is skipped
- The code does not compile because the second operand is invalid
- ok is false and nothing is thrown Correct answer.
- ok is false and an ArithmeticException is thrown by the division
Explanation
The && operator short-circuits: when the left operand x != 0 evaluates to false, the right operand is never evaluated. The division by zero therefore never happens, and the whole expression is false with no exception.
Question 5Programming fundamentals: syntax, variables, data types, operators, expressions
In Java or C#, a local variable is declared inside the braces of a for loop body rather than in the loop's initialization clause. Where can that variable legally be read?
- After the loop ends, but not before it starts
- Only inside the loop body block where it is declared Correct answer.
- Only in the loop's initialization and update clauses
- Anywhere in the method that contains the loop
Explanation
Braces introduce a block scope, so a local variable exists from its declaration to the closing brace of that block. Code in the initialization clause or after the loop is outside that scope and cannot reference the variable.
Question 6Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, the statements `int max = Integer.MAX_VALUE; int next = max + 1;` are executed. What value does next hold?
- -2147483648 Correct answer.
- The code fails to compile because the sum exceeds the int range
- 2147483648
- 0
Explanation
Adding 1 to 0x7FFFFFFF flips the sign bit and yields 0x80000000, the bit pattern of the smallest int. Java wraps silently on two's-complement overflow instead of raising an error, so next becomes -2147483648.
Question 7Programming fundamentals: syntax, variables, data types, operators, expressions
Consider the Java statements `double d = 9.99; int i = d;`. What happens when they are compiled?
- Compilation fails unless the value is explicitly cast to int Correct answer.
- i becomes 9.99 because int is automatically widened to double
- i becomes 9 because the fractional part is truncated
- i becomes 10 because the double is rounded to the nearest integer
Explanation
Assigning a double to an int is a narrowing conversion that may lose information, so Java requires an explicit cast such as (int) d. With that cast the fraction is truncated toward zero and i would hold 9.
Question 8Programming fundamentals: syntax, variables, data types, operators, expressions
After the statements `int a = 3; int b = a++ * 2;` are executed in Java or C#, what are the values of a and b?
- a = 4, b = 6 Correct answer.
- a = 3, b = 8
- a = 4, b = 8
- a = 3, b = 6
Explanation
The postfix operator a++ supplies the current value of a, which is 3, to the surrounding expression and only then increments a. The product is therefore 3 * 2 = 6, and a ends up as 4.
Question 9Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, given `int v = -8; int r = v >> 1; int u = v >>> 1;`, what values do r and u receive?
- r = -4 and u = 2147483644 Correct answer.
- r = 2147483644 and u = -4
- r = 4 and u = 4
- r = -4 and u = -4
Explanation
The >> operator is an arithmetic shift that replicates the sign bit, so shifting -8 right by one keeps the value negative and halves it to -4. The >>> operator always fills the vacated high bit with zeros, so the same negative bit pattern becomes the large positive value 2147483644.
Question 10Programming fundamentals: syntax, variables, data types, operators, expressions
In a language whose floating-point type follows IEEE 754 double precision (Java double, C double, Python float), what does the expression `(0.1 + 0.2) == 0.3` yield?
- false, because the sum is not exactly 0.3 Correct answer.
- true, because 0.1 and 0.2 are stored exactly in binary
- It raises a runtime error because the operands have different precision
- true, because both sides are rounded to the same double value
Explanation
None of 0.1, 0.2 or 0.3 has an exact finite binary representation, so each literal is stored as the nearest double. The rounded sum comes out as 0.30000000000000004, which is not the double nearest 0.3, so the equality test is false and comparisons should use a tolerance.
Question 11Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, which line correctly declares a local variable named `score` of primitive type `int` and initializes it to 10?
- int score = 10; Correct answer.
- int score := 10;
- score int = 10;
- integer score = 10;
Explanation
Java declarations place the type before the variable name and use a single equals sign for initialization. `integer` is not a Java primitive, `:=` is not a Java assignment operator, and reversing the type and name is invalid syntax.
Question 12Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, which primitive type is designed to hold exactly one of the two values `true` or `false`?
- bit
- boolean Correct answer.
- logic
- bool
Explanation
Java's primitive type for a truth value is `boolean`, with the two literals `true` and `false`. `bool` is used in some other languages but is not a Java keyword, and neither `bit` nor `logic` is a Java primitive type.
Question 13Programming fundamentals: syntax, variables, data types, operators, expressions
Which Java literal correctly represents the single character `A` as a `char` value?
- "A"
- char(A)
- A
- 'A' Correct answer.
Explanation
A `char` literal in Java is written with single quotation marks, so `'A'` has type `char`. Double quotation marks create a `String`, a bare `A` would be an identifier, and `char(A)` is not literal syntax.
Question 14Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, what happens when the statement `if (x = 5) { }` is compiled, assuming `x` is an `int` variable?
- It compiles and the condition is true only when `x` was already 5.
- It compiles only if `x` is declared `final`.
- It compiles and the condition is true whenever `x` becomes 5.
- It fails to compile because an `int` cannot be used as a boolean condition. Correct answer.
Explanation
Java requires the condition of an `if` statement to be a boolean expression. The expression `x = 5` is an assignment whose value is `int`, not a comparison, so the compiler rejects it; equality would be written `x == 5`.
Question 15Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, after `int total = 7; double avg = total / 2;` what value does `avg` hold?
- 3.5
- 3.0 Correct answer.
- 3
- 4.0
Explanation
Both operands of `total / 2` are `int` values, so integer division runs before the assignment and truncates 3.5 to 3. The resulting `int` is then widened to `double` for storage, giving 3.0.
Question 16Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, why does `byte b = 10; b += 5;` compile even though `b + 5` produces an `int`?
- The literal 5 is converted to a byte before the addition is performed.
- Compound assignment implicitly casts the result to the left operand's type. Correct answer.
- The compiler inserts a cast only after the whole statement finishes.
- Arithmetic on byte operands is carried out in byte precision.
Explanation
`b += 5` is equivalent to `b = (byte)(b + 5)`, so the compound assignment operator includes an implicit narrowing cast. The operands are promoted to `int` for the addition, but the final result is cast back to `byte`.
Question 17Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, what is printed by `int n = -3; System.out.println(n > 0 ? "positive" : n == 0 ? "zero" : "negative");`?
- zero
- positive
- compile error: ternary operators cannot be nested
- negative Correct answer.
Explanation
The first condition `n > 0` is false, so evaluation moves to the second ternary. There `n == 0` is also false, so the final branch produces `negative`. Nested conditional expressions are valid in Java.
Question 18Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, after `int count = 0; boolean flag = false & (++count > 0);` what is the value of `count`?
- 0
- compile error: `&` cannot be used with boolean operands
- 2
- 1 Correct answer.
Explanation
The single `&` operator on booleans is non-short-circuiting, so both operands are evaluated. The right side `++count > 0` increments `count` to 1 even though the left side is false.
Question 19Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, what is the value of `int x = 1 << 33;`?
- 1
- 0
- 2 Correct answer.
- 8589934592
Explanation
For an `int` shift, Java uses only the low five bits of the shift distance, so 33 is masked to `33 & 31 = 1`. Therefore `1 << 33` is evaluated as `1 << 1`, giving 2.
Question 20Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, what is the compile-time type of `r` after `var r = true ? 1 : 2.0;`?
- double Correct answer.
- int
- Object
- Number
Explanation
When one branch of a conditional expression is `int` and the other is `double`, binary numeric promotion applies to the whole expression. The `int` branch is widened to `double`, so the inferred type of `r` is `double`.
Question 21Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, which of the following is a valid identifier for a local variable?
- 2ndValue
- class
- total-amount
- _count Correct answer.
Explanation
A Java identifier may start with a letter, underscore, or dollar sign and may then contain letters, digits, underscores, or dollar signs. It cannot begin with a digit, contain a hyphen, or be a reserved keyword such as class.
Question 22Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, which primitive type is an 8-bit signed integer with the range -128 through 127?
- short
- byte Correct answer.
- char
- int
Explanation
In Java, byte is the 8-bit signed primitive integer type, so its range is -128 through 127. short is 16-bit, int is 32-bit, and char is an unsigned 16-bit Unicode value.
Question 23Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, which operator compares two primitive values for equality and returns a boolean?
- == Correct answer.
- =
- <>
- equals()
Explanation
The == operator compares two primitive values directly and yields a boolean result. A single = performs assignment, and equals() is a method used with objects rather than a primitive comparison operator.
Question 24Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, after `char letter = 'A'; int code = letter + 1;` what value does `code` hold?
- 66 Correct answer.
- 65
- 'B'
- 67
Explanation
When a char participates in arithmetic, Java promotes it to its int Unicode code value. The code for 'A' is 65, so adding 1 produces 66.
Question 25Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, what is the value of `int n = (int) 3.99;`?
- A compile-time error occurs
- 3.99
- 3 Correct answer.
- 4
Explanation
A narrowing cast from double to int discards the fractional part and truncates toward zero. Therefore 3.99 becomes 3 rather than being rounded to 4.
Question 26Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, why does the declaration `float ratio = 1.5;` fail to compile?
- Because 1.5 is a double literal, not a float literal. Correct answer.
- Because ratio is a reserved word in Java.
- Because assigning any decimal to float requires the final keyword.
- Because float cannot store decimal values at all.
Explanation
A decimal literal such as 1.5 has type double by default in Java. Assigning it to a float requires an explicit cast or an f suffix on the literal, so the declaration alone is a compile-time error.
Question 27Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, what is the value of the expression `5 ^ 3`?
- 15
- 8
- 6 Correct answer.
- 2
Explanation
The ^ operator performs bitwise exclusive OR on the binary representations. 5 is 101 and 3 is 011, so the result is 110, which is 6.
Question 28Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, for two String variables s1 and s2, what does the expression `s1 == s2` test?
- Whether the two strings contain the same characters.
- Whether s1 and s2 refer to the same String object. Correct answer.
- Whether both s1 and s2 are non-null references at the same time.
- Whether the lengths of the strings are equal.
Explanation
For object references, == tests whether both variables point to the same object rather than whether their contents are equal. String content comparison should use equals, not ==.
Question 29Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, what value does the expression `(int) 1e10` produce?
- 10000000000
- A compile-time error occurs
- 2147483647 Correct answer.
- -2147483648
Explanation
Converting a double that is too large for int to int saturates to Integer.MAX_VALUE in Java. The literal 1e10 exceeds that range, so the cast yields 2147483647.
Question 30Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, what is the value of `int result = 2 + 3 * 4 & 7;`?
- 5
- 0
- 14
- 6 Correct answer.
Explanation
Multiplication has higher precedence than addition, and bitwise AND has lower precedence than addition. Thus 3 * 4 becomes 12, 2 + 12 becomes 14, and 14 & 7 is 6.
Question 31Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, what is printed by `System.out.println("Total: " + 1 + 2);`?
- A compile-time error occurs
- Total: 12 Correct answer.
- Total: 123
- Total: 3
Explanation
The `+` operator is left-associative, so the String operand on the left forces concatenation. Java first builds `Total: 1` and then appends `2`, producing `Total: 12`. The integers are not added numerically because a String is already involved.
Question 32Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, what happens when the following code is compiled?
```java
int x;
System.out.println(x);
```
- It prints the default value 0 for an int.
- It throws a NullPointerException at runtime.
- It fails to compile. Correct answer.
- It prints the text null.
Explanation
A local variable must be definitely assigned before it is read. The declaration `int x;` gives no initial value, so using `x` in `System.out.println(x);` is a compile-time error. Unlike fields, local variables do not receive a default value.
Question 33Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, what is the decimal value of the integer literal `0x1F`?
- 32
- 15
- 31 Correct answer.
- 16
Explanation
The prefix `0x` marks a hexadecimal integer literal. In base 16, `1F` equals 1×16 + 15, which is 31. Therefore the literal `0x1F` has the decimal value 31.
Question 34Programming fundamentals: syntax, variables, data types, operators, expressions
Given the Java declaration `boolean r = true || false && false;`, what is the value of `r`?
- A compile-time error occurs because || and && cannot be mixed.
- true Correct answer.
- false
- A runtime exception is thrown.
Explanation
The `&&` operator has higher precedence than `||`, so the expression groups as `true || (false && false)`. The right side evaluates to `false`, and `true || false` is `true`. Mixing the two operators is legal and does not by itself cause an error.
Question 35Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, what is the value of the expression `-7 % 3`?
- 1
- -1 Correct answer.
- -2
- 2
Explanation
Java's remainder operator keeps the sign of the dividend. Here -7 divided by 3 is -2 with remainder -1, so `-7 % 3` evaluates to -1. The result is not adjusted to a positive value.
Question 36Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, what happens when the following declaration is compiled?
```java
long x = 10000000000;
```
- It compiles, but the value silently overflows to a negative int.
- It compiles and x holds the value 10000000000L.
- It fails to compile because long variables require a cast.
- It fails to compile: the literal is too large for int. Correct answer.
Explanation
An integer literal without a suffix has type int. The value 10000000000 exceeds Integer.MAX_VALUE, so the compiler rejects it before any assignment to long occurs. Adding the `L` suffix would make it a long literal.
Question 37Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, what is the value of `j` after the following code executes?
```java
int i = 5;
int j = i++ + ++i;
```
- 11
- 10
- 13
- 12 Correct answer.
Explanation
Postfix `i++` yields the old value 5 and then increments i to 6. Prefix `++i` first increments i to 7 and yields 7. The sum is 5 + 7, so j receives 12.
Question 38Programming fundamentals: syntax, variables, data types, operators, expressions
Which declaration compiles without an explicit cast in Java?
- boolean b = 1;
- double d = 5; Correct answer.
- byte c = 200;
- int a = 5.0;
Explanation
Assigning an int to a double is a widening primitive conversion, which Java performs automatically. The other declarations are invalid: a double does not fit into an int without a cast, an int is not a boolean, and 200 is outside byte's range.
Question 39Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, what happens when the following statement executes?
```java
double d = 5 / 0;
```
- d is assigned 0.0.
- d is assigned positive infinity.
- An ArithmeticException is thrown. Correct answer.
- The code fails to compile.
Explanation
Both operands of `5 / 0` are int, so Java performs integer division before assigning to d. Integer division by zero throws ArithmeticException at runtime; the double target does not turn the operation into floating-point division.
Question 40Programming fundamentals: syntax, variables, data types, operators, expressions
In Java, what value does `b` hold after the following declaration?
```java
byte b = (byte) 200;
```
- 200
- 56
- 0
- -56 Correct answer.
Explanation
A cast to byte keeps the low 8 bits of the int value. The value 200 is 11001000 in binary, which as a signed 8-bit value is -56. The cast is explicit, so the code compiles, but the value changes.
Question 41Control flow: conditionals, loops, and logical decision making
A while loop is written as `while (count < 10) { ... }` and the variable `count` holds the value 10 just before the loop is reached. How many times does the loop body execute?
- The loop runs forever
- One time
- Ten times
- Zero times Correct answer.
Explanation
A while loop tests its condition before every iteration, including the very first one. Because 10 < 10 is false at entry, the body is skipped completely and control passes to the statement after the loop.
Question 42Control flow: conditionals, loops, and logical decision making
A programmer needs a block of statements to run at least once, even when the controlling condition is false at the moment the structure is entered. Which control structure is designed for this?
- switch statement
- for loop
- while loop
- do-while loop Correct answer.
Explanation
A do-while loop evaluates its condition after the body, so the body always runs one time before the first test. Both while and for test the condition before the first pass and can therefore execute zero times.
Question 43Control flow: conditionals, loops, and logical decision making
The variable `score` holds 72. What does this chain output?
if (score >= 90) print "A"
else if (score >= 80) print "B"
else if (score >= 70) print "C"
else print "D"
- C Correct answer.
- D
- B
- A
Explanation
An if/else-if chain is evaluated top to bottom and stops at the first condition that is true. The tests for 90 and 80 both fail, while score >= 70 succeeds, so only the third branch runs.
Question 44Control flow: conditionals, loops, and logical decision making
Why does the guard `if (items != null && items.length > 0)` avoid a runtime error when `items` is null?
- Because the OR operator short-circuits when the left side is true.
- Because && stops evaluating once the left operand is false. Correct answer.
- Because && always evaluates both operands before deciding.
- Because null comparisons raise an exception that && catches.
Explanation
The && operator evaluates its left operand first and short-circuits: when that operand is false, the right operand is never evaluated. With items null, the length access is skipped, so no null dereference can occur.
Question 45Control flow: conditionals, loops, and logical decision making
What is printed by this loop?
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
print i;
}
- 3
- 1 2
- 1 2 4 5 Correct answer.
- 1 2 3 4 5
Explanation
The continue statement abandons the rest of the current iteration and jumps to the loop's update step, so the value 3 is never printed. The loop itself keeps running because continue does not end the loop.
Question 46Control flow: conditionals, loops, and logical decision making
In a C-style switch statement, the case 1 label contains several statements but no break, and the case 2 label follows and ends with a break. If the switch value is 1, which statements execute?
- Only the statements written under case 1
- Only the statements written under case 2
- Neither label, because the switch exits at a label that lacks a break
- The statements under case 1, then those under case 2 Correct answer.
Explanation
A switch jumps to the matching label and then keeps executing statements downwards until it meets a break or reaches the end of the switch. Because case 1 has no break, control falls through into case 2.
Question 47Control flow: conditionals, loops, and logical decision making
Which condition is logically equivalent to `!(a > 0 && b > 0)`?
- a > 0 && b <= 0
- !(a > 0) && !(b > 0)
- a <= 0 || b <= 0 Correct answer.
- a <= 0 && b <= 0
Explanation
De Morgan's law states that the negation of a conjunction is the disjunction of the negations, so `!(a > 0 && b > 0)` becomes `!(a > 0) || !(b > 0)`. Since `!(a > 0)` is the same as `a <= 0`, the result is `a <= 0 || b <= 0`.
Question 48Control flow: conditionals, loops, and logical decision making
What is the value of `total` after this code runs?
int total = 0;
for (int i = 0; i <= 4; i++) {
total += i;
}
- 10 Correct answer.
- 4
- 15
- 6
Explanation
The `<=` comparison means the loop still runs when i equals 4, so the values added are 0, 1, 2, 3 and 4. Their sum is 10, and the loop stops only when i becomes 5.
Question 49Control flow: conditionals, loops, and logical decision making
How many times does the print statement execute?
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (j == i) continue;
print i + "," + j;
}
}
- 4
- 8
- 16
- 12 Correct answer.
Explanation
The outer loop runs 4 times and the inner loop runs 4 times for each outer pass, giving 16 inner iterations in total. On each outer pass exactly one inner iteration has j equal to i and is skipped by continue, so 16 minus 4 leaves 12 prints.
Question 50Control flow: conditionals, loops, and logical decision making
With `a = false`, `b = true` and `c = false`, the expression `a || b && c` evaluates to false. Which statement explains how the operators group?
- Logical operators have no relative precedence and are always applied strictly left to right
- || has higher precedence, so a || b is computed first and the result is AND-ed with c
- The operands are grouped from right to left, so c is combined with b before a is considered
- && has higher precedence, so b && c is computed first and the result is OR-ed with a Correct answer.
Explanation
In languages with C-style operator precedence, ! binds tightest, then &&, then ||. The expression therefore groups as `a || (b && c)`: b && c is false, and false OR false yields false.