






































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







































Western Governors University
Question 1: Which Java component is responsible for actually executing compiled bytecode on a specific machine? A. Java Development Kit (JDK) B. Java Runtime Environment (JRE) C. Java Virtual Machine (JVM) D. Java Application Programming Interface (API)
Correct Answer: C. Java Virtual Machine (JVM) Expert Rationale: The JVM is the part of the Java platform that interprets and executes the bytecode instructions produced by the compiler on a particular device.
Question 2: Which statement best describes the relationship between the JDK and the JRE? A. The JRE includes the JDK so programs can be compiled and run. B. The JDK includes the JRE so programs can be compiled and run. C. The JVM includes both the JDK and the JRE. D. The JDK and JRE are unrelated and installed separately by design.
Correct Answer: B. The JDK includes the JRE so programs can be compiled and run. Expert Rationale: The JDK is the full development kit that contains tools such as the compiler and also bundles the JRE so compiled programs can be executed.
Question 3: In a Java IDE, which action most directly checks for syntax errors and translates source code to bytecode? A. Running the program B. Debugging the program
Question 6: Which of the following is a valid Java variable declaration and initialization for an immutable value? A. final int DAYS_IN_WEEK = 7; B. constant int DAYS_IN_WEEK = 7; C. static DAYS_IN_WEEK = 7; D. immutable int DAYS_IN_WEEK = 7;
Correct Answer: A. final int DAYS_IN_WEEK = 7; Expert Rationale: The final keyword marks a variable so its value cannot be changed once initialized, matching the concept of an unchangeable constant.
Question 7: Which of these is a primitive data type in Java? A. String B. Integer C. boolean D. Scanner
Correct Answer: C. boolean Expert Rationale: The study guide lists primitive types such as int, double, char, boolean, etc., whereas String, Integer, and Scanner are objects.
Question 8: Which Java literal represents a char value? A. "A" B. 'A' C. true D. 3.
Correct Answer: B. 'A' Expert Rationale: A char literal uses single quotes around a single character, while double quotes denote a String literal.
Question 9: Which best describes a Java class? A. A running instance in memory B. A blueprint defining fields and methods for objects C. A single variable containing multiple values D. A special method that initializes data
Correct Answer: B. A blueprint defining fields and methods for objects Expert Rationale: The guide explains that a class describes common state and behavior, serving as the template from which objects (instances) are created.
Question 10: What is the primary purpose of a constructor? A. To destroy objects when they are no longer needed B. To allocate memory and initialize an object's fields C. To compare two objects for equality D. To perform input/output operations
Correct Answer: B. To allocate memory and initialize an object's fields Expert Rationale: Constructors run when new is used and typically set up instance variables with initial values for the newly created object.
Question 11: In Java, where are instance variables normally declared? A. Inside the main method only B. Inside any method that uses them
Question 14: Which of the following is a valid Java array declaration that reserves space for 5 int values? A. int[5] nums; B. int nums = new int[5]; C. int[] nums = new int[5]; D. array int nums[5];
Correct Answer: C. int[] nums = new int[5]; Expert Rationale: The correct syntax declares a variable of type int[] and uses new int[5] to allocate memory, matching the array examples in the guide.
Question 15: Which method would you use to find the number of elements currently stored in an ArrayList names? A. names.length() B. names.size() C. names.count() D. names.capacity()
Correct Answer: B. names.size() Expert Rationale: The guide notes that ArrayList uses the size() method, not length, to report how many elements are stored.
Question 16: Which statement about ArrayList compared to an array is correct? A. Arrays grow automatically as items are added. B. ArrayList can change its size as elements are added or removed. C. ArrayList can store primitive types without wrappers. D. Arrays have built-in methods for adding and removing elements.
Correct Answer: B. ArrayList can change its size as elements are added or removed. Expert Rationale: The study guide highlights that an ArrayList can grow or shrink as needed, while arrays have a fixed length.
Question 17: What is the correct way to read an int from the keyboard using a Scanner named in? A. int value = in.readInt(); B. int value = in.nextInt(); C. int value = in.getInt(); D. int value = in.scanInt();
Correct Answer: B. int value = in.nextInt(); Expert Rationale: The guide repeatedly uses Scanner’s nextInt() method to read integer input from System.in.
Question 18: Which Java statement correctly compares two String variables s1 and s2 for the same sequence of characters? A. if (s1 == s2) B. if (s1 != s2) C. if (s1.equals(s2)) D. if (equals(s1, s2))
Correct Answer: C. if (s1.equals(s2)) Expert Rationale: The guide emphasizes that equals compares string contents, whereas == checks whether two references point to the same object.
Correct Answer: B. a && b Expert Rationale: The guide identifies && as the short-circuit AND operator, which may skip evaluating the second operand if the first is false.
Question 22: What is the result of evaluating the expression 5 / 2 in Java when both operands are int? A. 2. B. 3 C. 2 D. 0
Correct Answer: C. 2 Expert Rationale: When both operands are int, Java performs integer division and discards the fractional part, leaving only the whole number.
Question 23: Which of the following declares a method that does not return a value and receives a single double parameter? A. void update(double amount) B. double update(void amount) C. update(double amount) D. public double update(double)
Correct Answer: A. void update(double amount) Expert Rationale: A void return type indicates no value is returned, and the parameter list (double amount) matches a single double parameter.
Question 24: Which best describes a while loop that never changes the variable in its condition? A. It will execute exactly once.
B. It will cause a compile-time error. C. It will likely create an infinite loop. D. It will be optimized away by the compiler.
Correct Answer: C. It will likely create an infinite loop. Expert Rationale: If the condition of a while loop never changes from true to false, control never exits the loop, which matches the infinite loop example in the guide.
Question 25: What does the this keyword refer to inside an instance method? A. The superclass of the current class B. A new temporary object C. The current object on which the method was called D. Any object passed as a parameter
Correct Answer: C. The current object on which the method was called Expert Rationale: The guide explains that this is a reference to the current instantiated object, typically used to distinguish fields from parameters.
Moderate (Questions 26–60)
Question 26: Consider the code snippet:
int a = 7;
int b = 3;
double result = a / b;
What is the value stored in result? A. 2. B. 2.
Question 27: Given the code:
String text = "student";
int idx = text.indexOf("d");
What is stored in idx? A. - B. 2 C. 3 D. 4
Correct Answer: C. 3 Expert Rationale: Characters are indexed from 0, so in "student" the 'd' is the fourth character, giving an index of 3.
Question 28: What is printed by the following code?
int x = 4;
int y = 6;
boolean b = (x < 5) && (y > 10);
System.out.println(b);
A. true B. false C. 0 D. It causes a compile-time error
Correct Answer: B. false Expert Rationale: The first comparison x < 5 is true, but y > 10 is false, so true && false evaluates to false.
Question 29: What is printed by this code fragment?
int count = 0;
for (int i = 1; i <= 3; i++) {
count += i;
}
System.out.println(count);
A. 3 B. 4 C. 6 D. 7
Correct Answer: C. 6 Expert Rationale: The loop adds 1 + 2 + 3 to count, resulting in 6.
Question 30: Consider this code:
String s1 = "Java";
String s2 = "Ja" + "va";
boolean same = (s1 == s2);
System.out.println(same);
What will be printed? A. true B. false C. 0 D. It throws a runtime exception
D. No output because the loop never executes
Correct Answer: A. 0 1 2 Expert Rationale: i starts at 0 and increments up to 2 while i < 3, so three iterations print "0 ", "1 ", and "2 ".
Question 33: What is the value of total after this loop executes?
int total = 1;
for (int k = 0; k < 3; k++) {
total *= 2;
}
A. 2 B. 4 C. 6 D. 8
Correct Answer: D. 8 Expert Rationale: The body doubles total three times: 1 → 2 → 4 → 8.
Question 34: A method header is written as public int calc(int a, double b). Which statement correctly calls this method and stores the result? A. calc(3, 2.0); B. int x = calc(3, 2.0); C. double x = calc(3, 2.0); D. int x = calc();
Correct Answer: B. int x = calc(3, 2.0); Expert Rationale: The method returns an int and expects one int and one double argument, so the call with those arguments and an int variable to receive the result is appropriate.
Question 35: Consider the code:
int[] nums = { 4, 8, 12 };
System.out.println(nums[1]);
What is printed? A. 4 B. 8 C. 12 D. An index-out-of-bounds exception
Correct Answer: B. 8 Expert Rationale: Array indices start at 0, so nums[1] accesses the second element, which is 8.
Question 36: Which switch statement fragment will correctly print "Weekend" for the values 6 and 7 and "Weekday" for 1 through 5? A.
switch(day) {
case 1: case 2: case 3: case 4: case 5: System.out.println("Weekday"); case 6: case 7: System.out.println("Weekend");
System.out.println("Weekday"); break;
}
Correct Answer: C. Expert Rationale: Option C groups the case labels correctly and uses break to prevent fall-through, producing exactly one line of output for each value.
Question 37: What is printed by the following code?
int a = 5;
int b = 10;
int result = 0;
if (a > b && b > 0) {
result = 1;
} else {
result = 2;
}
System.out.println(result);
A. 0 B. 1 C. 2 D. It causes a runtime error
Correct Answer: C. 2 Expert Rationale: a > b is false, so the if condition fails, leading to the else branch and setting result to 2.
Question 38: Which of the following best describes method overloading in Java? A. A subclass providing a new implementation of a superclass method B. Two methods with the same name but different parameter lists in the same class C. A method calling itself recursively D. A static method replacing an instance method
Correct Answer: B. Two methods with the same name but different parameter lists in the same class Expert Rationale: Overloading occurs when a class defines multiple methods with the same name but different parameter types or counts, as noted in the study guide.
Question 39: Which line correctly creates a new ArrayList to store Integer objects? A. ArrayList nums = new ArrayList(); B. ArrayList nums = new ArrayList<>(); C. ArrayList nums = new ArrayList(); D. ArrayList nums = new ArrayList();
Correct Answer: B. ArrayList nums = new ArrayList<>(); Expert Rationale: The guide stresses that ArrayList requires an object type parameter like Integer, and the diamond operator <> can infer the type on the right-hand side.