



































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 C. Compiling the program D. Formatting the program
Correct Answer: C. Compiling the program Expert Rationale: The compile step parses the Java source, flags compile-time syntax/type errors, and generates bytecode if the code is valid.
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 C. Inside the class but outside all methods D. Outside the class entirely
Correct Answer: C. Inside the class but outside all methods Expert Rationale: Instance variables belong to the class definition but are not declared inside methods; this gives them object-wide scope.
Question 12: Which Java keyword is most directly associated with encapsulation by hiding implementation details? A. public B. extends C. private D. static
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.
Question 19: What is stored in a reference variable of type Dog when you write Dog d = new Dog();? A. The entire Dog object B. The address (reference) of a Dog object in memory C. The source code of the Dog class D. A copy of the Dog class’s methods
Correct Answer: B. The address (reference) of a Dog object in memory Expert Rationale: The study guide explains that object variables hold references to objects created on the heap using the new keyword.
Question 20: Which Java loop is most naturally used when the number of iterations is known before the loop begins? A. while loop B. do-while loop C. for loop D. Enhanced for loop
Correct Answer: C. for loop Expert Rationale: The classic for loop is typically described as a definite loop used when the number of repetitions is known in advance.
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.
D. A compile-time error
Correct Answer: A. 2. Expert Rationale: a / b performs integer division yielding 2; this int is then widened to double, so result holds 2.0.
Question 27: Given the code:
String text = "programming";
int idx = text.indexOf("g");
What is stored in idx? A. - B. 0 C. 3 D. 5
Correct Answer: D. 5 Expert Rationale: "programming" is indexed from 0; the first 'g' occurs at position 5 (p=0, r=1, o=2, g=3 is wrong—careful; there are two g's; let’s recompute). Oops need to compute carefully: "p r o g r a m m i n g" indexes: 0 p,1 r,2 o,3 g,4 r,5 a,6 m,7 m,8 i, n,10 g -> first g index=3. I must fix answer & explanation.
Let me correct question 27: I'll recompute and adjust. Need to ensure final answer matches. Let's rewrite Q27 fully:
I'll replace it with new code so index is clear. I'll choose "Student" and indexOf("d") etc. Let's restart Q27.
I must not leave inconsistent text.
I'll re-do Question 27 from scratch.
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
Correct Answer: A. true Expert Rationale: Both s1 and s2 refer to the same interned string "Java" built at compile time, so the references are identical and == yields true.
Question 31: What will this code print?
String s = "computer";
System.out.println(s.substring(1, 4));
A. com B. omp C. omp plus an extra space D. comp
Correct Answer: B. omp Expert Rationale: substring(1, 4) returns characters at indices 1, 2, and 3, which are "omp".
Question 32: Given the following code:
int i = 0;
while (i < 3) {
System.out.print(i + " "); i++;
}
What output is produced? A. 0 1 2 B. 1 2 3 C. 0 1 2 3 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 ".
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");
}
B.
switch(day) {
case 1,2,3,4,5: System.out.println("Weekday"); break; case 6,7: System.out.println("Weekend");
break;
}
C.
switch(day) {
case 1: case 2: case 3: case 4: case 5: System.out.println("Weekday"); break; case 6: case 7: System.out.println("Weekend"); break;
}
D.
switch(day) {
default: 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;
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.
Question 40: What is the effect of the following code?
String word = "hello";
String upper = word.toUpperCase();
A. It changes word to "HELLO" and leaves upper empty. B. It creates a new string "HELLO" in upper and word remains "hello". C. It deletes the original string and only keeps "HELLO". D. It throws a runtime exception because strings are immutable.
Correct Answer: B. It creates a new string "HELLO" in upper and word remains "hello". Expert Rationale: Strings are immutable; methods like toUpperCase() return a new String object without modifying the original.
Question 41: Suppose you have int[] data = new int[4]; and you do not assign any values. What is the value of data[0]? A. 0
C. null D. It is undefined and causes an error if accessed.
Correct Answer: A. 0 Expert Rationale: The guide notes that numeric instance variables and array elements of primitive types get default values; for int, that default is 0.
Question 42: What is the output of the following code?
int i = 1;
do {
System.out.print(i + " "); i++;
} while (i <= 3);
A. 1 2 B. 1 2 3 C. 2 3 D. No output
Correct Answer: B. 1 2 3 Expert Rationale: A do-while loop executes its body at least once; here it prints 1, 2, and 3 while i <= 3.
Question 43: Consider the method:
public void change(int x) {
x = x + 5;
}