🎯 OCA Java Practice tests - 7

10 Free Practice Questions for 1Z0-808 Certification
All 10 questions shown below with complete explanations

πŸ“š OCA Language Fundamentals: Data Types, Identifiers & Literals

πŸ“š Topics Covered in Practice Test 7

Question 1 EASY πŸ“Œ Primitive Data Types
What is the size of int in Java?
A. 16 bits
B. 32 bits
C. 64 bits
D. 8 bits

βœ… Correct Answer: B

int is 32 bits (4 bytes) in Java.

πŸ“– Detailed Explanation:

In Java, the 'int' primitive data type is always 32 bits (4 bytes) regardless of the platform, which is part of Java's platform independence guarantee. An int can store values from -2,147,483,648 to 2,147,483,647 (2^31-1 to -2^31). This fixed size ensures that Java programs behave consistently across different operating systems and hardware architectures. Other primitive sizes are: byte (8 bits), short (16 bits), long (64 bits), char (16 bits), float (32 bits), double (64 bits), and boolean (implementation dependent, typically 1 bit). Understanding these sizes is crucial for memory management and choosing appropriate data types.

Question 2 EASY πŸ“Œ Java Identifiers
Which of the following is a valid Java identifier?
A. 2value
B. _total
C. int
D. hello-world

βœ… Correct Answer: B

Identifiers cannot start with a digit, cannot use hyphens, and cannot be Java keywords. _total starts with an underscore and follows the rules.

πŸ“– Detailed Explanation:

Java identifiers must follow specific rules: they cannot start with a digit (eliminates 2value), cannot use hyphens as they are interpreted as operators (eliminates hello-world), and cannot be reserved keywords (eliminates int). Identifiers can start with letters, underscores, or dollar signs. The underscore character is a valid starting character for identifiers, making _total a valid identifier, though starting with underscore is discouraged by naming conventions.

Note: A single underscore "_" became a reserved keyword in Java 9+, but "_total" is still valid.
Question 3 EASY πŸ“Œ Java Identifiers
Which statement about Java identifiers is true?
A. Identifiers can start with a digit
B. Java identifiers are not case-sensitive
C. An identifier can contain $ and _
D. Identifiers must be less than 32 characters

βœ… Correct Answer: C

$ and _ are legal anywhere in an identifier, though their use is discouraged for readability. Java identifiers are case-sensitive and have no formal length limit.

πŸ“– Detailed Explanation:

Java identifier rules: cannot start with digits (only letters, underscore, or dollar sign), are case-sensitive (myVar and MyVar are different), can contain $ and _ anywhere in the identifier, and have no formal length limit in the language specification. The dollar sign and underscore are legal characters in identifiers, though their use is discouraged by Java naming conventions for regular variables and methods.

Question 4 EASY πŸ“Œ Java Identifiers
Which of these is not a valid Java identifier?
A. myVar
B. MAX_VALUE
C. hello world
D. _count

βœ… Correct Answer: C

Spaces are not allowed in identifiers. myVar, MAX_VALUE, and _count are all valid.

πŸ“– Detailed Explanation:

Spaces are not allowed anywhere in Java identifiers. An identifier must be a continuous sequence of valid characters without any whitespace. The other options myVar (camelCase), MAX_VALUE (constant naming), and _count (underscore prefix) are all syntactically valid identifiers, though _count goes against Java naming conventions.

Question 5 MEDIUM πŸ“Œ Data Types and Variables
Which of the following lines generate a compiler error? (Choose all that apply)
10: byte myAnimals = 8;
11: int myScore = 3.8;
12: String theText = "Buddy";
13: myAnimals.length();
14: myScore.length();
15: theText.length();
A. Line 13 generates a compiler error.
B. Line 14 generates a compiler error.
C. Line 11 generates a compiler error.
D. Line 10 generates a compiler error.
E. Line 15 generates a compiler error.

βœ… Correct Answer: A,B,C

Line 11 generates a compiler error because a double literal (3.8) cannot be assigned to an int.
Line 13 generates a compiler error because primitives (byte) do not have methods.
Line 14 generates a compiler error because primitives (int) do not have methods.

Lines 10 and 15 compile successfully.

πŸ“– Detailed Explanation:

Line 11 fails because assigning a double literal (3.8) to an int requires an explicit cast.

Line 13 fails because myAnimals is a primitive byte, and primitives do not have methods like length().

Line 14 fails because myScore is a primitive int, which also has no methods.

Line 10 is valid because the literal 8 fits within the range of byte.

Line 15 is valid since String provides the length() method.

Therefore, the lines that generate compiler errors are 13, 14, and 11 - A, B, C.

Question 6 MEDIUM πŸ“Œ String Comparison and String Pool
Consider the following code:

public class Demo {
public static void main(String[] args) {
String city1 = "Boston";
//insert here
System.out.print(city1.equals(city2)+" ");
System.out.print(city1 == city2);
}
}

Two possible statements for line 6:
Statement 1: String city2 = "Boston";
Statement 2: String city2 = new String("Boston");

Which statement is TRUE?
A. Inserting statement 2 at line 6 will produce the output 'true true'
B. Inserting statement 1 at line 6 will produce the output 'true false'
C. Inserting statement 1 at line 6 will produce the output 'false false'
D. Inserting statement 2 at line 6 will produce the output 'false true'
E. None of the above

βœ… Correct Answer: E

None of the given options correctly describe the outputs. Statement 1 (String literal) produces 'true true' because both variables reference the same string pool object. Statement 2 (new String) produces 'true false' because equals() compares content (true) while == compares references (false for different objects).

πŸ“– Detailed Explanation:

Java stores string literals in the string pool, where identical literals share the same memory reference.

In Statement 1, "Boston" already exists in the pool (from city1), so city2 refers to the same pooled object as city1.

Because of this, both equals() (content check) and == (reference check) return true true.

In Statement 2, new String("Boston") forces creation of a new, distinct object in the heap, bypassing the pool.

The contents remain identical, so equals() returns true, but the references differ, so == returns false β†’ true false.

Since no answer choice matches true true and true false, the only correct choice is E (None of the above).

Question 7 MEDIUM πŸ“Œ Java Identifiers
Which of the following identifiers is invalid in Java?
A. $Amount
B. Ξ±ΟΞΉΞΈΞΌΟŒΟ‚
C. my@value
D. value_123

βœ… Correct Answer: C

The @ symbol is not allowed in identifiers. Unicode characters are allowed, so Greek letters are technically valid, though not recommended.

πŸ“– Detailed Explanation:

Java identifiers:

CAN use: letters (Unicode allowed), digits, _, $

CANNOT use: symbols like @, #, !, %, &, etc.

Cannot start with a digit

Cannot be a keyword

Now check each option:

A β€” $Amount. Valid β€” $ is allowed.

B β€” Ξ±ΟΞΉΞΈΞΌΟŒΟ‚ (Greek letters). Valid β€” Java allows full Unicode in identifiers.

C β€” my@value. Invalid β€” @ is not allowed in identifiers.

D β€” value_123. Valid β€” letters, underscore, digits β†’ all allowed.

Question 8 MEDIUM πŸ“Œ Java Identifiers
What will happen if you declare:
int Class = 5;
System.out.println(Class);
A. Compile-time error
B. Prints 5
C. Runtime error
D. Variable shadowing warning

βœ… Correct Answer: B

Class (uppercase C) is not a keyword. Java is case-sensitive, so this is valid. Only class (lowercase) is reserved.

πŸ“– Detailed Explanation:

Java is case-sensitive, which means Class and class are completely different identifiers. The keyword class (lowercase) is reserved and cannot be used as an identifier, but Class (uppercase C) is not a keyword and can be used as a variable name. This code will compile successfully and print 5. However, using Class as a variable name could be confusing since it resembles the Class type, so it is not recommended for code readability.

Question 9 HARD πŸ“Œ Java Identifiers
What is true about the following identifiers?
Ξ±ΟΞΉΞΈΞΌΟŒΟ‚ (Greek: number)
ε…ƒη΄  (Chinese: element)
student_name
A. All are invalid
B. Only student_name is valid
C. All are valid
D. Only Ξ±ΟΞΉΞΈΞΌΟŒΟ‚ is valid

βœ… Correct Answer: C

Java allows Unicode characters in identifiers, including Greek and Chinese. However, it is best practice to use English for readability.

πŸ“– Detailed Explanation:

Java supports Unicode characters in identifiers, which means characters from various languages including Greek (Ξ±ΟΞΉΞΈΞΌΟŒΟ‚), Chinese (ε…ƒη΄ ), and Latin scripts are syntactically valid. The underscore in student_name is also perfectly valid. However, while Unicode characters are allowed, it is best practice to use ASCII characters (English letters) for code readability, maintainability, and compatibility across different development environments and teams.

Question 10 HARD πŸ“Œ Numeric Literals
Given the following class, which of the following lines of code can replace INSERT CODE HERE to make the code compile? (Choose all that apply)

public class Cost {
public void calculate() {
INSERT CODE HERE
System.out.println(value);
}
}
A. int value = 8L;
B. int value = 0b110;
C. int value = 0xF;
D. double value = 0xF;
E. double value = 1_3_.5_0;
F. int value = 2_5_;
G. None of the above

βœ… Correct Answer: B,C,D

Options B, C, and D compile: binary and hexadecimal literals are valid, and widening conversions (int to double) are allowed. Options A, E, F have syntax or type errors.

πŸ“– Detailed Explanation:

Let's analyze each option: A) int value = 8L; - Does not compile. 8L is a long literal, and assigning a long to an int requires explicit casting due to potential data loss. B) int value = 0b110; - Compiles. 0b110 is a binary literal representing decimal 6, which is a valid int value. C) int value = 0xF; - Compiles. 0xF is a hexadecimal literal representing decimal 15, which is a valid int value. D) double value = 0xF; - Compiles. 0xF is an int literal by default (decimal 15), which is then widened to double. E) double value = 1_3_.5_0; - Does not compile. Underscores cannot be placed adjacent to decimal points (1_3_.5_0 has underscore before decimal point). F) int value = 2_5_; - Does not compile. Underscores cannot be at the end of numeric literals. The valid options use proper numeric literal syntax and compatible type assignments.

Want More Practice Questions?

Access all 1624 more questions covering all OCA topics

What You'll Get:

β†’ 1,624 practice questions

β†’ 29 full certification tests

β†’ Chapter-wise practice exams

β†’ Detailed explanations for every answer

β†’ Progress tracking & certificates

Start Free Trial Now β†’