





























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!






























Prepare for the WGU C169 Objective Assessment – Java Programming with this comprehensive practice resource designed to strengthen your programming skills and exam readiness. It features exam-style questions covering Java syntax, object- oriented programming, control structures, methods, classes, arrays, exception handling, and debugging concepts. Use it to reinforce key programming principles, assess your understanding, and build confidence before your objective assessment. An excellent study aid for WGU students preparing for the C169 Java Programming course. MULTIPLE CHOICE. Question 1: Where is a program typically stored when it is not running? A. In the core processor B. In primary storage C. In the CPU D. In secondary storage Correct answer: D. In secondary storage Rationale: Programs are stored on secondary storage devices (like hard drives or SSDs) when not in use. They are loaded into primary storage (RAM) to be executed by the CPU. Question 2: What is the relationship between the Java Development Kit (JDK), the Java Runtime Environment (JRE), and the Java Virtual Machine (JVM)? A. The JRE contains the JDK and JVM B. The JDK contains the JVM but not the JRE C. The JDK contains the JRE but not the JVM D. The JRE contains the JVM but not the JDK
Correct answer: D. The JRE contains the JVM but not the JDK Rationale: The JRE (Java Runtime Environment) includes the JVM (Java Virtual Machine) and the core libraries needed to run Java programs. The JDK (Java Development Kit) is a superset of the JRE, including development tools like compilers. Question 3: What is an applet in Java? A. An operating system plug-in that supports Java-enabled browsers B. Java code that can be located anywhere on the internet C. Java code that resides on a client browser D. A browser plug-in that allows Java code to run Correct answer: B. Java code that can be located anywhere on the internet Rationale: An applet is a Java program that can be embedded in a web page and run within a browser. It is typically downloaded from the internet and executed locally. Question 4: What happens when the compile option is selected in a Java integrated development environment (IDE)? A. The IDE scans the code and checks for syntax errors without translating or executing. B. The IDE interprets and executes Java code directly C. The IDE translates the Java code into machine-specific instructions. D. The IDE enters a context-sensitive mode that flags coding errors as information is entered Correct answer: B. The IDE interprets and executes Java code directly Rationale: When the compile option is selected, the Java compiler translates the source code into bytecode, which is a platform- independent intermediate code. This bytecode is then executed by the JVM.
Question 8: Which reserved word, when added to a variable declaration, makes its value unchangeable? A. Final B. Const C. Super D. Reserve Correct answer: A. Final Rationale: The final keyword in Java makes a variable constant, meaning its value cannot be changed after initialization. const is a reserved word but is not used in Java. Question 9: Which statement would allow an administrator to initialize a variable in Java? A. double itemPrice=0; B. go itemPrice==0; C. integer itemPrice=0; D. the itemPrice==0; Correct answer: A. double itemPrice=0; Rationale: This statement declares a variable of type double named itemPrice and initializes it to 0. The syntax is correct and follows the standard Java format. Question 10: What is the expected output of the following code? java System.out.println(1 / 0); A. Fail during compile B. Compile but fail to run C. Run and print "1 / 0" D. Run but report a run-time error
Correct answer: D. Run but report a run-time error Rationale: The code will compile successfully but will throw an ArithmeticException at runtime because division by zero is not allowed in integer arithmetic. Question 11: Which case should be used for Java variable names, assuming standard Java naming conventions? A. Lower case B. Upper camel case C. Lower camel case D. Upper case Correct answer: C. Lower camel case Rationale: Java variable names should start with a lowercase letter and then use uppercase letters for each subsequent word (e.g., myVariableName ). This is the standard Java naming convention. Question 12: What is the difference between objects and classes? A. Objects are instances of a class B. Objects are primitive types in a class C. Objects do not have to belong to a class D. Objects do not have lifespans but classes do. Correct answer: A. Objects are instances of a class Rationale: A class is a blueprint or template, while an object is an instance of that class. Objects have a state and behavior defined by their class. Question 13: What are objects, in the realm of Java? A. Entities that share a common location in memory B. Variables with common names C. Entities that have certain basic properties D. Variables with no functionality
Correct answer: A. To automatically generate API documentation Rationale: Javadoc is a tool that parses special comments in Java source code and generates HTML documentation for the API. Question 17: In Java, what happens when an array variable is declared? A. The number of array entries does not need to be declared. B. The array is automatically initialized C. The types of the index must be declared D. The type of the array entries must be declared. Correct answer: D. The type of the array entries must be declared. Rationale: When declaring an array variable, the type of elements the array will hold must be specified (e.g., int[] or String[] ). Question 18: What is the advantage of a Java array list over an array? A. Array list elements can be of different types. B. Array list can use primitive types as the type parameter C. Array list elements are automatically initialized D. Array list can grow or shrink as needed. Correct answer: D. Array list can grow or shrink as needed. Rationale: Unlike fixed-size arrays, ArrayLists in Java are dynamic and can resize automatically as elements are added or removed. This gives them more flexibility than standard arrays. Question 19: A programmer wants to access the last element in the ArrayList phoneNumbers but does not know how many elements are in the array list. Which method should the programmer use to determine the length of this array list? A. phoneNumbers.index () B. phoneNumbers.get () C. phoneNumbers.size () D. phoneNumbers.length ()
Correct answer: C. phoneNumbers.size () Rationale: The size() method returns the number of elements currently in the ArrayList. The length property is used for arrays, not ArrayLists. Question 20: Which characteristic is true if the subclass is derived from a class? A. A subclass can override inherited methods B. The inherited method can never be used directly. C. A subclass can access the private members of its parent class D. A subclass constructor cannot invoke the constructor of the superclass Correct answer: A. A subclass can override inherited methods Rationale: Subclasses in Java can override inherited methods to provide their own implementation. However, they cannot access private members of the superclass. Question 21: Which Java keyword denotes inheritance? A. inherits B. extends C. implements D. derives Correct answer: B. extends Rationale: The extends keyword is used in Java to indicate that a class is inheriting from another class (superclass). Question 22: What is the purpose of the super keyword in Java? A. To refer to the current object B. To refer to the superclass constructor or members C. To refer to a static method D. To refer to a final variable Correct answer: B. To refer to the superclass constructor or members Rationale: super is used to call a superclass constructor or to access
Question 25: What is one reason to use an array list? A. To avoid specifying type during creation B. To collect objects of different types C. To create a collection of elements D. To automatically initialized elements Correct answer: C. To create a collection of elements Rationale: ArrayLists are used to create a collection of elements that can be dynamically resized. They provide a flexible way to store and manage groups of objects. Question 26: In Java, what does the final keyword do when applied to a variable? A. It makes the variable a constant B. It makes the variable a class variable C. It makes the variable accessible from anywhere D. It makes the variable the same for all objects Correct answer: A. It makes the variable a constant Rationale: The final keyword makes a variable a constant, meaning its value cannot be changed after initialization. Question 27: What is the purpose of the this keyword in Java? A. To refer to the current object B. To refer to the superclass C. To refer to a static method D. To refer to a final variable Correct answer: A. To refer to the current object Rationale: this is used to refer to the current object within an instance method or constructor. It is often used to distinguish instance variables from local variables.
Question 28: Which data type in Java is immutable? A. String B. StringBuilder C. StringBuffer D. All of the above Correct answer: A. String Rationale: The String class in Java is immutable, meaning that once a String object is created, its value cannot be changed. StringBuilder and StringBuffer are mutable. Question 29: What is a primitive data type in Java? A. A data type that is not an object B. A data type that is a class C. A data type that can be null D. A data type that is immutable Correct answer: A. A data type that is not an object Rationale: Primitive data types (e.g., int, double, boolean) are the basic data types in Java that are not objects. They store values directly in memory. Question 30: Which of the following is NOT a primitive data type in Java? A. int B. double C. String D. boolean Correct answer: C. String Rationale: String is a class in Java, not a primitive data type. The primitive types are byte , short , int , long , float , double , char , and boolean. Question 31: What is the correct syntax for an if-else statement in Java? A. if (condition) { // code } else { // code }
Question 33: Which of the following is a logical operator in Java? A. != B. < C. && D. > Correct answer: C. && Rationale: && is the logical AND operator in Java. Logical operators are used to combine boolean expressions.
Question 34: Given the following code:
int x = 5; if (x > 10) { System.out.println("A"); } else if (x > 5) { System.out.println("B"); } else { System.out.println("C"); } What will be printed? A. A B. B C. C D. Nothing **Correct answer: C. C Rationale: The condition** x > 10 **is false, and** x > 5 **is also false (since** x **is 5). Therefore, the** else **block executes, printing "C". Question 35:** What is the difference between && and & in Java? A. && is a short-circuit logical AND, while & is a bitwise AND B. && is a bitwise AND, while & is a logical AND C. There is no difference D. && is used for integers, & is used for booleans **Correct answer: A.** && **is a short-circuit logical AND, while** & **is a bitwise AND Rationale:** && **is the short-circuit logical AND operator. It evaluates the right-hand expression only if the left-hand expression is** true**.** & **is a bitwise AND operator. Question 36:** What is the difference between || and | in Java? A. || is a short-circuit logical OR, while | is a bitwise OR B. || is a bitwise OR, while | is a logical OR C. There is no difference D. || is used for integers, | is used for booleans **Correct answer: A.** || **is a short-circuit logical OR, while** | **is a bitwise OR Rationale:** || **is the short-circuit logical OR operator. It evaluates the right- hand expression only if the left-hand expression is** false**.** | **is a bitwise OR operator.** **Question 40:** Which of the following is the correct way to write an if statement that executes when x is between 1 and 10 inclusive? A. if (1 < x < 10) B. if (x >= 1 && x <= 10) C. if (1 <= x <= 10) D. if (x >= 1 || x <= 10) **Correct answer: B. if (x >= 1 && x <= 10) Rationale: The correct way to check if** x **is between 1 and 10 inclusive is** if (x >= 1 && x <= 10)**. Java does not support chained comparisons like** 1 < x < 10**. Question 41:** What is the purpose of a loop in Java? A. To create a new object B. To execute a block of code repeatedly C. To declare a variable D. To handle exceptions **Correct answer: B. To execute a block of code repeatedly Rationale: Loops are used to execute a block of code multiple times, based on a condition. This is a fundamental control structure in programming. Question 42:** Which of the following is a valid loop construct in Java? A. for B. while C. do-while D. All of the above **Correct answer: D. All of the above Rationale: Java provides three loop constructs:** for **,** while **, and** do-while**. Each is used for different scenarios based on the number of iterations and condition evaluation time.** **Question 43:** What is the difference between a while loop and a do- while loop? A. while executes at least once; do-while may not execute B. do-while executes at least once; while may not execute C. There is no difference D. while is used for infinite loops; do-while is not **Correct answer: B.** do-while **executes at least once;** while **may not execute Rationale: In a** do-while **loop, the condition is evaluated after the code block, so the code always executes at least once. In a** while **loop, the condition is evaluated first, so the code might not execute at all. Question 44:** Given the following code: java for (int i = 0; i < 5; i++) { System.out.print(i); } What will be printed? A. 01234 B. 12345 C. 012345 D. 1234 **Correct answer: A. 01234 Rationale: The loop starts at** i = 0 **and runs while** i < 5**. The loop body prints the value of** i **, then increments** i**. The output is** 01234**. Question 45:** Given the following code: java for (int i = 1; i <= 5; i++) { **Question 48:** How many times does the following loop execute? java for (int i = 0; i < 10; i++) { if (i == 5) break; } A. 4 times B. 5 times C. 6 times D. 10 times **Correct answer: B. 5 times Rationale: The loop breaks when** i **equals 5. The loop executes for** i = 0, 1, 2, 3, 4 **, which is 5 times. Question 49:** What is the purpose of the break statement in a loop? A. To skip the current iteration B. To exit the loop completely C. To restart the loop D. To throw an exception **Correct answer: B. To exit the loop completely Rationale: The** break **statement is used to exit a loop prematurely. When encountered, the loop terminates immediately. Question 50:** What is the purpose of the continue statement in a loop? A. To exit the loop completely B. To skip the current iteration and move to the next C. To restart the loop D. To throw an exception **Correct answer: B. To skip the current iteration and move to the next Rationale: The** continue **statement is used to skip the rest of the current iteration of a loop and jump to the next iteration. Question 51:** Which of the following correctly declares and initializes an array of 5 integers? A. int[] arr = new int[5]; B. int arr = new int[5]; C. int[] arr = new int[]; D. int arr = new int[]; **Correct answer: A.** int[] arr = new int[5]; **Rationale: The correct syntax for declaring an array of 5 integers is** int[] arr = new int[5];**. Question 52:** Which of the following correctly initializes a String array? A. String[] arr = {"a", "b", "c"}; B. String arr = {"a", "b", "c"}; C. String[] arr = new String[3]; arr = {"a", "b", "c"}; D. String arr = new String[] {"a", "b", "c"}; **Correct answer: A.** String[] arr = {"a", "b", "c"}; **Rationale: The correct syntax for initializing a String array is** String[] arr = {"a", "b", "c"};**. Question 53:** What is the default value of an element in an int array? A. null B. 0 C. undefined D. 1 **Correct answer: B. 0 Rationale: In Java, elements of an** int **array are automatically initialized to** 0 **by default.**