Oracle Java Foundations study materials
Oracle Java Foundations study resources

Oracle Java Foundations practice questions

One question at a time. Build your Oracle Java Foundations understanding.

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 1Java Basics

Which primitive type in Java is used to store a single 16-bit Unicode character?

  1. char Correct answer.
  2. byte
  3. String
  4. short

Explanation

char is the 16-bit primitive type that holds one Unicode character, written with single quotes such as 'A'. byte and short are 8-bit and 16-bit integer types that hold numeric values, and String is a reference type rather than a primitive.

Question 2Java Basics

Which declaration is a valid entry point for a standalone Java application?

  1. public static int main(String[] args) throws Exception
  2. public void main(String[] args)
  3. public static void main()
  4. public static void main(String[] args) Correct answer.

Explanation

The JVM looks for a method that is public, static, returns void, is named main, and accepts a single String[] parameter, because it is invoked without creating an instance of the class. Any deviation from that signature means the class has no launchable entry point.

Question 3Java Basics

Which of the following is a legal Java identifier?

  1. _total Correct answer.
  2. 2ndValue
  3. class
  4. my-Value

Explanation

An identifier must begin with a letter, an underscore, or a dollar sign, and may not contain punctuation such as a hyphen or be a reserved keyword. Digits are allowed only after the first character.

Question 4Java Basics

Given the declaration int[] arr = new int[3];, which expression returns the number of elements in the array?

  1. arr.length Correct answer.
  2. arr.length()
  3. arr.size()
  4. arr.size

Explanation

For arrays, the element count is exposed through the final field length, accessed without parentheses. Method-style accessors belong to other types: size() is used by collections and length() by String.

Question 5Java Basics

What is the value of y after this code runs? int x = 5; int y = x > 3 ? 10 : 20;

  1. 10 Correct answer.
  2. 15
  3. 5
  4. 20

Explanation

The conditional operator evaluates the boolean expression first and returns the second operand when it is true, otherwise the third. Since 5 > 3 is true, y receives 10 and the value 20 is never used.

Question 6Java Basics

What is printed by the following statement? System.out.println("Total: " + 2 + 3);

  1. Total: 2 3
  2. Total: 23 Correct answer.
  3. Total: 5
  4. Total: 32

Explanation

The + operator is evaluated left to right, and once the left operand is a String the operator performs concatenation rather than addition. The first step yields "Total: 2", and appending 3 gives "Total: 23".

Question 7Java Basics

A local variable is declared with the final modifier and already assigned a value. What happens if a later statement in the same block attempts to assign it a new value?

  1. It compiles and the new value replaces the old one
  2. It fails to compile Correct answer.
  3. It compiles but the assignment only takes effect inside loops
  4. It compiles with a warning but ignores the assignment

Explanation

A final variable may be assigned exactly once, after which any further assignment is a compile-time error. The restriction is enforced by the compiler, so the program never runs.

Question 8Java Basics

What is printed by the following code? int a = 7; int b = 2; System.out.println(a / b + " " + a % b);

  1. 4 1
  2. 3.5 1
  3. 3 0
  4. 3 1 Correct answer.

Explanation

Division of two ints performs integer division, discarding the fractional part, so 7 / 2 is 3. The remainder operator gives 7 % 2 = 1, and the values are joined by string concatenation.

Question 9Java Basics

What is the value of i after this code runs? double d = 9.99; int i = (int) d;

  1. 9.0
  2. 10
  3. 9 Correct answer.
  4. compilation error

Explanation

A narrowing cast from double to int discards the fractional part, truncating toward zero. It does not round, and the cast is legal when written explicitly.

Question 10Java Basics

What is printed by the following code? String a = new String("hi"); String b = new String("hi"); System.out.println(a == b);

  1. ClassCastException
  2. false Correct answer.
  3. hi
  4. true

Explanation

For reference types, == compares whether two variables refer to the same object, not whether the contents match. Two separate new String calls create two distinct objects, so the comparison is false.

Question 11Java Basics

What is printed by the following code? int i = 10; do { System.out.print(i); i++; } while (i < 5);

  1. 11
  2. nothing
  3. 5
  4. 10 Correct answer.

Explanation

A do-while loop executes its body before testing the condition for the first time. The body runs once printing 10, and the condition 11 < 5 then ends the loop.

Question 12Java Basics

What is printed by the following code? int n = 2; switch (n) { case 1: System.out.print("A"); case 2: System.out.print("B"); case 3: System.out.print("C"); break; default: System.out.print("D"); }

  1. C
  2. B
  3. BC Correct answer.
  4. BCD

Explanation

Execution jumps to the matching case label and then continues into subsequent cases until a break or the end of the switch is reached. Matching case 2 prints B and falls through to case 3, which prints C before breaking.

Question 13Java Basics

What is printed by the following code? for (int i = 1; i <= 4; i++) { if (i == 2) continue; System.out.print(i); }

  1. 1234
  2. 13
  3. 134 Correct answer.
  4. 124

Explanation

continue abandons the rest of the current iteration and moves on to the update and next condition check; it does not end the loop. Only the iteration where i equals 2 skips its print, so 1, 3, and 4 are printed.

Question 14Java Basics

What is the result of compiling and running the following code? { int x = 5; } System.out.println(x);

  1. prints 5
  2. compile error Correct answer.
  3. NullPointerException
  4. prints 0

Explanation

A local variable declared inside a block exists only within that block. Once the closing brace is reached, the name is out of scope, so the later reference cannot be resolved by the compiler.

Question 15Java Basics

What is printed by the following code? StringBuilder sb = new StringBuilder("ab"); sb.append("c"); System.out.println(sb);

  1. c
  2. abc Correct answer.
  3. ab
  4. compile error

Explanation

StringBuilder is mutable, so append modifies the same object's character sequence and returns that object. Printing it therefore shows the combined content abc.

Question 16Java Basics

What is printed by the following code? String s = "JavaProgramming"; System.out.println(s.substring(4, 8));

  1. rogram
  2. Java
  3. Progr
  4. Prog Correct answer.

Explanation

The two-argument substring takes characters from the begin index up to but not including the end index. Indices 4, 5, 6, and 7 of the string are P, r, o, and g.

Question 17Java Basics

What is printed by the following code? char c = 'A'; c += 2; System.out.println(c);

  1. A2
  2. 67
  3. C Correct answer.
  4. B

Explanation

A char is an unsigned 16-bit numeric value, so 'A' corresponds to 65 and adding 2 gives 67, which is 'C'. Compound assignment such as += applies an implicit narrowing cast back to char.

Question 18Java Basics

What is printed by the following code? int i = 5; int j = i++ + ++i; System.out.println(i + " " + j);

  1. 6 12
  2. 7 11
  3. 6 11
  4. 7 12 Correct answer.

Explanation

The postfix i++ yields the old value 5 and then increments i to 6. The prefix ++i increments first to 7 and yields 7, so j is 5 + 7 = 12 and i ends at 7.

Question 19Java Basics

What is printed by the following code? String s = null; if (s != null && s.length() > 0) System.out.print("yes"); else System.out.print("no");

  1. NullPointerException
  2. yes
  3. compile error
  4. no Correct answer.

Explanation

The && operator short-circuits: when the left operand is false, the right operand is never evaluated. Because s != null is false, s.length() is never called, so the else branch prints no.

Question 20Java Basics

What is printed by the following code? int max = Integer.MAX_VALUE; System.out.println(max + 1);

  1. -2147483648 Correct answer.
  2. 0
  3. 2147483648
  4. ArithmeticException

Explanation

int arithmetic silently wraps around on overflow, following two's-complement representation. Adding 1 to the largest int produces the smallest int value, -2147483648.

Question 21Java Basics

What is printed by the following code? outer: for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (j == 1) break outer; System.out.print("" + i + j + " "); } }

  1. 00 01 02
  2. 00 10 20
  3. 00 01 02 10 11 12 20 21 22
  4. 00 Correct answer.

Explanation

A labeled break terminates the loop named by the label, not merely the innermost loop. The first time j reaches 1 the outer loop is abandoned, so only the single iteration printing 00 completes.

Question 22Java Basics

What is printed by the following code? Integer a = 127, b = 127; Integer c = 200, d = 200; System.out.println((a == b) + " " + (c == d));

  1. false false
  2. false true
  3. true false Correct answer.
  4. true true

Explanation

Autoboxing of values in the range -128 to 127 reuses cached Integer instances, so a and b refer to the same object. Values outside that range create separate objects, so c == d compares distinct references and is false.

Question 23Working with Java Data Types

A field of type boolean is declared in a class and never assigned a value. What value does it hold?

  1. false Correct answer.
  2. 0
  3. true
  4. null

Explanation

Fields (instance and static) are automatically initialized to their type's default value, and the default for boolean is false. Only local variables lack default values and must be assigned before use.

Question 24Working with Java Data Types

Which Java primitive type stores a single 16-bit Unicode character?

  1. byte
  2. String
  3. short
  4. char Correct answer.

Explanation

char is the only primitive type designated for a single 16-bit Unicode code unit. byte is an 8-bit signed integer and short is a 16-bit signed integer, both used for arithmetic values rather than characters.

Question 25Working with Java Data Types

Which declaration compiles successfully?

  1. long d = 1_000_000.0L;
  2. long c = 1_000_000L; Correct answer.
  3. long b = _1000L;
  4. long a = 1_000_000_L;

Explanation

Underscores may appear between digits in a numeric literal but not adjacent to a prefix or suffix. A floating-point literal also cannot carry the L suffix, which is reserved for integral long literals.

Question 26Working with Java Data Types

How many bytes does a Java double occupy?

  1. 4
  2. 2
  3. 8 Correct answer.
  4. 16

Explanation

double is a 64-bit IEEE 754 floating-point type, which is 8 bytes. float is the 32-bit (4-byte) floating-point type, and char/short are 16-bit (2-byte) types.

Question 27Working with Java Data Types

What is the value of the expression 7 / 2 in Java?

  1. 3 Correct answer.
  2. 4
  3. 3.0
  4. 3.5

Explanation

When both operands of / are integers, Java performs integer division and discards the fractional part, so 7 / 2 yields 3. The result is also of type int, so no decimal point appears.

Question 28Working with Java Data Types

Which line declares a variable whose type is a primitive?

  1. String s = "5";
  2. int[] a = {5};
  3. int n = 5; Correct answer.
  4. Integer n = 5;

Explanation

Java has eight primitive types, including int, which store values directly. String, Integer and arrays are all reference types whose variables hold references to objects.

Question 29Working with Java Data Types

Which assignment compiles without an explicit cast?

  1. int a = 10L;
  2. long d = 10; Correct answer.
  3. short b = 100000;
  4. byte c = 200;

Explanation

Assigning an int value to a long is a widening conversion, which the compiler performs automatically. The other options either narrow the value or use a constant outside the target type's range.

Question 30Working with Java Data Types

Consider the code: byte b = 10; b = b + 1; What is the result of compiling it?

  1. It compiles and b becomes 10
  2. Compilation fails because 10 cannot initialize a byte
  3. Compilation fails because b + 1 has type int Correct answer.
  4. It compiles and b becomes 11

Explanation

Binary numeric promotion converts byte operands to int, so b + 1 is an int and cannot be assigned back to a byte without a cast. The initializer 10 is fine because it is a constant that fits in byte.

Question 31Working with Java Data Types

Given byte b = 10; which statement compiles and leaves b equal to 15?

  1. b = 5 + b.byteValue();
  2. b = b + 5;
  3. b = (byte) b + 5;
  4. b += 5; Correct answer.

Explanation

Compound assignment operators such as += include an implicit cast back to the type of the left-hand operand, so b += 5 is legal for a byte. A plain assignment of an int expression to a byte is rejected.

Question 32Working with Java Data Types

Which declaration compiles without error?

  1. float b = 3.14d;
  2. float d = 3.14L;
  3. float c = 3.14f; Correct answer.
  4. float a = 3.14;

Explanation

A literal with a decimal point is a double unless it carries the f suffix, so only the version with f can initialize a float directly. The d suffix explicitly makes the literal a double, which narrows to float.

Question 33Working with Java Data Types

What does System.out.println('a' + 'b'); print?

  1. 97 98
  2. a compile-time error
  3. 195 Correct answer.
  4. ab

Explanation

The + operator applied to two char operands performs binary numeric promotion to int, so it adds the code values 97 and 98. The resulting int 195 is printed numerically rather than as characters.

Question 34Working with Java Data Types

Inside a method the statements int x; System.out.println(x); appear. What happens?

  1. It prints an unpredictable leftover value
  2. It prints 0
  3. It throws NullPointerException at runtime
  4. Compilation fails because x is not initialized Correct answer.

Explanation

Only fields receive default values; local variables have no default and must be definitely assigned before use. The compiler therefore rejects any read of an uninitialized local variable.

Question 35Working with Java Data Types

Given String s = "Java"; s.concat(" Rocks"); System.out.println(s); what is printed?

  1. JavaRocks
  2. A compile-time error
  3. Java Rocks
  4. Java Correct answer.

Explanation

String objects are immutable, so concat returns a new String instead of modifying the original. Because the returned reference is discarded, the variable s still refers to "Java".

Question 36Working with Java Data Types

Given double d = 5 / 2; what is the value of d?

  1. 2
  2. 2.5
  3. 3.0
  4. 2.0 Correct answer.

Explanation

The operands of / are both int, so integer division is performed first and produces 2. That int result is then widened to double for the assignment, giving 2.0.

Question 37Working with Java Data Types

Which statement about the var keyword is true?

  1. var may be used for method parameters
  2. var creates a dynamically typed variable
  3. var requires an initializer on the same statement Correct answer.
  4. var may be used to declare instance fields

Explanation

var is local-variable type inference: the compiler derives a static type from the initializer, so an initializer is mandatory in the same declaration. It cannot be used for fields, method parameters, or return types.

Question 38Working with Java Data Types

Given double d = 9.7; which line compiles successfully?

  1. int a = d;
  2. short b = d;
  3. int c = (int) d; Correct answer.
  4. byte e = d;

Explanation

Converting a double to an integral type is a narrowing conversion and must be explicit. The cast truncates the fractional part, so c receives the value 9.

Question 39Working with Java Data Types

Given StringBuilder sb = new StringBuilder("Java"); sb.append(" SE"); System.out.println(sb); what is printed?

  1. SE
  2. Java
  3. A compile-time error
  4. Java SE Correct answer.

Explanation

StringBuilder is mutable, so append modifies the same object and returns it for chaining. Printing sb therefore shows the accumulated text "Java SE".

Question 40Working with Java Data Types

What happens when the statement long x = 2147483648; is compiled?

  1. x holds 2147483647
  2. x holds 2147483648
  3. x holds -2147483648
  4. Compilation fails because the integer literal is too large Correct answer.

Explanation

An integral literal without a suffix has type int, and 2147483648 exceeds Integer.MAX_VALUE. Adding the L suffix, as in 2147483648L, would make it a valid long literal.

Question 41Working with Java Data Types

Given Integer a = 1000; Integer b = 1000; what does System.out.println(a == b); print?

  1. false Correct answer.
  2. It throws an exception
  3. true
  4. A compile-time error

Explanation

== on two reference variables compares references, and autoboxing caches only values from -128 to 127. Both 1000 values therefore become distinct Integer objects, so the comparison is false and equals must be used for value comparison.

Question 42Working with Java Data Types

Given byte b = (byte) 130; what is the value of b?

  1. -126 Correct answer.
  2. -130
  3. 130
  4. 2

Explanation

A narrowing cast to byte keeps only the low 8 bits, and the result is interpreted as a signed two's-complement value. The bit pattern of 130 becomes 10000010, which is -126.

Question 43Working with Java Data Types

What is the value of total after long total = Integer.MAX_VALUE + 1; executes?

  1. -2147483648 Correct answer.
  2. Compilation fails
  3. 0
  4. 2147483648

Explanation

Both operands of + are int, so the addition is performed in 32-bit int arithmetic and wraps around before the assignment. The overflowed int is then widened to long, preserving the negative value.

Question 44Working with Java Data Types

What is the value of v after int v = 0x10 + 010 + 0b10; executes?

  1. 18
  2. 26 Correct answer.
  3. 22
  4. 28

Explanation

0x10 is hexadecimal 16, 010 is octal 8, and 0b10 is binary 2. Adding them with ordinary int arithmetic gives 26.

Question 45Using Operators and Decision Constructs

What value is stored in the variable by the statement int result = 7 / 2; ?

  1. 4
  2. 3.5
  3. 3 Correct answer.
  4. 0

Explanation

Both operands of the division are int values, so Java performs integer division and discards the fractional part. The exact quotient 3.5 is truncated toward zero, so 3 is stored.

Question 46Using Operators and Decision Constructs

What value is stored in r by the statement int r = 17 % 5; ?

  1. 2 Correct answer.
  2. 3.4
  3. 3
  4. 0

Explanation

The remainder operator returns what is left over after integer division: 17 divided by 5 is 3 with a remainder of 2. Because both operands are int, the result is also an int.

Question 47Using Operators and Decision Constructs

What is the value of the expression 2 + 3 * 4 ?

  1. 20
  2. 9
  3. 24
  4. 14 Correct answer.

Explanation

Multiplication has higher precedence than addition, so 3 * 4 is evaluated first and its result is then added to 2. Parentheses would be required to force the addition to happen first.

Question 48Using Operators and Decision Constructs

Given the statements int x = 5; int y = x++; what are the final values of x and y?

  1. y is 5 and x is 5
  2. y is 5 and x is 6 Correct answer.
  3. y is 6 and x is 6
  4. y is 6 and x is 5

Explanation

The postfix form x++ produces the current value of x and only then increments x. As a result y receives 5 while x becomes 6.

Question 49Using Operators and Decision Constructs

Given int n = 4; what does the expression (n % 2 == 0) ? "even" : "odd" evaluate to?

  1. "odd"
  2. "even" Correct answer.
  3. "true"
  4. A compile-time error occurs

Explanation

The conditional operator tests its boolean condition and yields the second operand when the condition is true. Since 4 % 2 equals 0, the first value is selected.

Question 50Using Operators and Decision Constructs

Which type may be used as the selector expression of a switch statement?

  1. double
  2. boolean
  3. long
  4. String Correct answer.

Explanation

A switch selector may be one of the integral types char, byte, short or int, an enum constant, or a String. Floating-point and boolean values are not permitted.