Part 1 practice questions: Java fundamentals¶
☕ Java 17 · 🎓 OCP 1Z0-829 · Part 1 · Java Fundamentals / Core Syntax
Optional page
Ten exam-style questions covering all of Part 1. Try each one before opening its answer. To review a topic, go back to the Part 1 recap.
1. Which are valid entry points for a program run from the command line? (Choose all that apply.)
A. public static void main(String[] args)
B. public static final main(String[] args)
C. public void main(String[] args)
D. public static void main(String... args)
E. static public void main(String[] args)
Answer
A, D and E. B has no return type at all, so it doesn't even compile. C misses static. Varargs and swapping public/static are both fine.
2. In which order must these lines appear for the file to compile?
A. X, Y, Z
B. Y, Z, X
C. Z, Y, X
D. Z, X, Y
Answer
C. Package, then imports, then the class.
3. Which are legal identifiers? (Choose all that apply.)
A. _streamingService
B. 4kResolution
C. $contentId
D. true
E. MAX_EPISODES
F. java.lang
Answer
A, C and E. B starts with a digit, true is a literal and can't be used as a name, and a dot isn't allowed in an identifier.
4. Which statements about this class are true? (Choose all that apply.)
public class Movie {
private String title;
public Movie() { title = "Unknown"; }
public String getTitle() { return title; }
}
A. Movie is a class
B. title is a class
C. getTitle() is a constructor
D. Movie() is a constructor
Answer
A and D. Movie() matches the class name and has no return type. title is a field, and getTitle() is a method: it returns a String.
5. Which lines compile? (Choose all that apply. Each line is a local variable.)
Answer
A and B (String and int). C has no value to infer from, and D gives null, which has no type.
6. What is the output?
public class Subscription {
static int totalUsers = 0;
int userId;
{ userId = ++totalUsers; }
public static void main(String[] args) {
Subscription s1 = new Subscription();
Subscription s2 = new Subscription();
System.out.println(s1.userId + " " + s2.userId);
}
}
A. 0 0
B. 1 1
C. 1 2
D. Compilation error
Answer
C. The instance initializer runs for each new object: totalUsers becomes 1 for s1, then 2 for s2.
7. Which statements about garbage collection are true? (Choose all that apply.)
A. System.gc() runs garbage collection immediately
B. An object is eligible when no reference can reach it
C. The collector frees unused heap memory automatically
D. A final variable prevents its object from being collected
Answer
B and C. System.gc() is only a hint. final prevents reassignment, nothing more: the object can still become unreachable.
8. What happens?
public class Episode {
public static void main(String[] args) {
int season = 1;
{
int episode = 5;
season++;
}
System.out.println(season);
System.out.println(episode); // line 9
}
}
A. Prints 2, then 5
B. Prints 2, then throws an exception
C. Compilation error on line 9
D. Compilation error on line 5
Answer
C. episode belongs to the inner block and is out of scope on line 9. It's a compile-time error, so nothing prints.
9. Which imports let this class compile? (Choose all that apply.)
package com.netflix;
// INSERT IMPORT HERE
public class ContentLibrary {
public void load() {
List<String> titles = new ArrayList<>();
}
}
A. import java.util.*;
B. import java.util.List; import java.util.ArrayList;
C. import java.util.List.*;
D. import java.util.concurrent.*;
Answer
A and B. C is a legal line, but it imports the types nested inside List, not List or ArrayList. D imports a sub-package that contains neither.
10. What is the output?
public class StreamingApp {
static int count;
{ System.out.print(count + "-"); }
static { count = 5; }
public static void main(String[] args) {
StreamingApp a = new StreamingApp();
StreamingApp b = new StreamingApp();
System.out.print(count);
}
}
A. 0-0-5
B. 5-5-5
C. 0-5-5
D. Compilation error
Answer
B. The static block runs once, when the class is set up, before main: count is 5 before any object exists. Each instance initializer then prints 5-.