Docsity

STARTING OUT WITH JAVA: CONTROL STRUCTURES THROUGH OBJECTS, 8TH EDITION (TONY GADDIS), Exams of Nursing

Toggle sidebar
bg1
Page 1 of 32
STARTING OUT WITH JAVA: CONTROL STRUCTURES THROUGH OBJECTS,
8TH EDITION (TONY GADDIS) EXAM QUESTIONS LATEST EDITION 2026 –
2027 VERSION SOLVED QUESTIONS & ANSWERS
STARTING OUT WITH JAVA: CONTROL STRUCTURES THROUGH OBJECTS, 8th Edition
(Tony Gaddis) — QUESTIONS WITH RATIONALES
Question 1
Which of the following best describes the role of the Java Virtual Machine (JVM) in
the execution of a Java program?
A) The JVM translates Java source code directly into machine language for the specific
computer it is running on.
B) The JVM is a hardware component that stores Java bytecode for execution.
C) The JVM is a software program that reads Java bytecode and executes it on the host
computer, providing platform independence.
D) The JVM is a compiler that converts Java source code into bytecode.
Answer: C) The JVM is a software program that reads Java bytecode and executes it
on the host computer, providing platform independence.
Rationale: The Java Virtual Machine (JVM) is a software program that reads Java
bytecode and executes it on the host computer. This allows Java programs to be
platform-independent because the same bytecode can run on any computer that has a
JVM installed. The compiler (D) converts source code into bytecode, not the JVM.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download STARTING OUT WITH JAVA: CONTROL STRUCTURES THROUGH OBJECTS, 8TH EDITION (TONY GADDIS) and more Exams Nursing in PDF only on Docsity!

STARTING OUT WITH JAVA: CONTROL STRUCTURES THROUGH OBJECTS,

8TH EDITION (TONY GADDIS) EXAM QUESTIONS LATEST EDITION 2026 –

2027 VERSION SOLVED QUESTIONS & ANSWERS

STARTING OUT WITH JAVA: CONTROL STRUCTURES THROUGH OBJECTS, 8th Edition (Tony Gaddis) — QUESTIONS WITH RATIONALES Question 1 Which of the following best describes the role of the Java Virtual Machine (JVM) in the execution of a Java program? A) The JVM translates Java source code directly into machine language for the specific computer it is running on. B) The JVM is a hardware component that stores Java bytecode for execution. C) The JVM is a software program that reads Java bytecode and executes it on the host computer, providing platform independence. D) The JVM is a compiler that converts Java source code into bytecode. Answer: C) The JVM is a software program that reads Java bytecode and executes it on the host computer, providing platform independence. Rationale: The Java Virtual Machine (JVM) is a software program that reads Java bytecode and executes it on the host computer. This allows Java programs to be platform-independent because the same bytecode can run on any computer that has a JVM installed. The compiler (D) converts source code into bytecode, not the JVM.

Question 2 A programmer writes a Java program and saves it with the file extension .java. Which of the following correctly describes the next step in the process of running this program? A) The .java file is directly executed by the operating system. B) The .java file is compiled by the javac compiler into a .class file containing bytecode. C) The .java file is interpreted by the JVM without any compilation. D) The .java file is converted into a .exe file for Windows. Answer: B) The .java file is compiled by the javac compiler into a .class file containing bytecode. Rationale: Java source code is written in .java files. These files must be compiled by the javac compiler into .class files, which contain bytecode. The JVM then executes the bytecode. Java files are not directly executed (A) or interpreted without compilation (C), and they are not converted to .exe files (D). Question 3 Which type of programming error causes a program to produce incorrect results but does not prevent the program from compiling or running? A) Syntax error B) Runtime error C) Logic error D) Compilation error Answer: C) Logic error Rationale: A logic error is a mistake that causes a program to produce erroneous results but does not prevent the program from compiling or running. Syntax errors (A) and compilation errors (D) prevent the program from compiling. Runtime errors (B) occur during execution and cause the program to terminate abnormally.

Question 6 Which of the following is an example of a runtime error in a Java program? A) Forgetting a semicolon at the end of a statement B) Dividing a number by zero C) Using a variable before it is declared D) Misspelling a class name Answer: B) Dividing a number by zero Rationale: A runtime error occurs during the execution of a program. Dividing a number by zero causes an ArithmeticException at runtime. Forgetting a semicolon (A), using an undeclared variable (C), and misspelling a class name (D) are compile-time errors. SECTION 2: JAVA FUNDAMENTALS (Questions 7–14) Question 7 Which of the following is the correct way to declare a named constant in Java? A) const double PI = 3.14159; B) final double PI = 3.14159; C) static double PI = 3.14159; D) constant double PI = 3.14159; Answer: B) final double PI = 3.14159; Rationale: In Java, a named constant is created using the final keyword. Once a final variable is assigned a value, it cannot be changed. The const keyword (A) is reserved but not used in Java. The static keyword (C) makes a variable belong to the class rather than instances, but does not make it constant. Question 8 What is the result of the following expression in Java: (int) 7.9?

A) 7.

B) 8

C) 7

D) A compile-time error Answer: C) 7 Rationale: Casting a double to an int truncates the decimal portion, not rounds it. Therefore, (int) 7.9 results in 7_. Option B (8) would be the result of rounding, not truncation._ Question 9 Which of the following correctly creates a Scanner object to read input from the keyboard? A) Scanner keyboard = new Scanner(); B) Scanner keyboard = new Scanner(System.in); C) Scanner keyboard = Scanner(System.in); D) Scanner keyboard = new Scanner(System); Answer: B) Scanner keyboard = new Scanner(System.in); Rationale: The Scanner class is used to read input. To read from the keyboard, you pass System.in to the Scanner constructor. Option A is missing the argument. Option C is missing the new keyword. Option D passes System instead of System.in_._ Question 10 What is the value of x after the following code executes: int x = 10; x += 5; x - = 3;? A) 10 B) 12 C) 15 D) 18 Answer: B) 12

Question 13 Which of the following data types is used to store a single character in Java? A) String B) char C) int D) boolean Answer: B) char Rationale: The char data type is used to store a single character, such as 'A' or '7'. The String type (A) stores a sequence of characters. The int type (C) stores integers. The boolean type (D) stores true/false values. Question 14 What is the result of 10 / 3 in Java when both operands are integers? A) 3. B) 3. C) 3 D) 4 Answer: C) 3 Rationale: When both operands of the division operator are integers, Java performs integer division, which truncates the decimal portion. Therefore, 10 / 3 results in 3_. To get a floating-point result, at least one operand must be a_ double or float_._ SECTION 3: DECISION STRUCTURES (Questions 15–22) Question 15 Which of the following correctly uses the if-else statement to assign a value to a variable?

A) if (x > 0) y = 1; else y = - 1; B) if x > 0 then y = 1; else y = - 1; C) if (x > 0) { y = 1; } else { y = - 1; } D) Both A and C are correct. Answer: D) Both A and C are correct. Rationale: Both A and C are valid Java syntax. A single statement can follow if or else without braces, but using braces (C) is recommended for clarity and to avoid logic errors. Option B is not valid Java syntax. Question 16 In a switch statement, which keyword is used to specify the code that executes if no case matches? A) else B) default C) case D) otherwise Answer: B) default Rationale: The default section of a switch statement is branched to if none of the case expressions match the switch expression. The else keyword (A) is used with if statements. The case keyword (C) is used to specify matching values. The otherwise keyword (D) is not used in Java. Question 17 What is the output of the following code: int x = 5; if (x > 3) { if (x < 10) { System.out.println("A"); } else { System.out.println("B"); } }? A) A B) B

Rationale: The expression (x > 5) is true , but (y < 15) is false_. The_ && operator requires both operands to be true for the result to be true_. Therefore,_ result is false_._ Question 20 Which of the following correctly compares two strings for equality in Java? A) if (str1 == str2) B) if (str1.equals(str2)) C) if (str1 = str2) D) if (str1.compareTo(str2)) Answer: B) if (str1.equals(str2)) Rationale: The equals() method is used to compare the contents of two strings. The == operator (A) compares object references, not contents. The = operator (C) is an assignment, not a comparison. The compareTo() method (D) returns an integer, not a boolean, and is used to determine the lexicographic order of strings. Question 21 What is the purpose of the conditional operator ( ?: ) in Java? A) To execute a block of code repeatedly B) To evaluate a boolean expression and return one of two values based on the result C) To compare two strings D) To declare a constant Answer: B) To evaluate a boolean expression and return one of two values based on the result Rationale: The conditional operator (also called the ternary operator) evaluates a boolean expression and returns one of two values depending on whether the expression is true or false. It is a shorthand for a simple if-else statement.

Question 22 Which of the following if statements correctly checks if a variable x is between 1 and 10, inclusive? A) if (1 < x < 10) B) if (x > 1 && x < 10) C) if (x >= 1 && x <= 10) D) if (x >= 1 || x <= 10) Answer: C) if (x >= 1 && x <= 10) Rationale: To check if x is between 1 and 10 inclusive, both conditions must be true: x >= 1 and x <= 10_. The_ && operator ensures both are true. Option A is not valid Java syntax. Option B excludes 1 and 10. Option D uses || , which would be true for any value. SECTION 4: LOOPS AND FILES (Questions 23–30) Question 23 Which type of loop is guaranteed to execute at least once? A) while loop B) do-while loop C) for loop D) Enhanced for loop Answer: B) do-while loop Rationale: A do-while loop executes the loop body first and then checks the condition. Therefore, the body is guaranteed to execute at least once. The while loop (A) and for loop (C) check the condition before executing the body, so they may not execute at all. Question 24 What is the output of the following code: for (int i = 0; i < 5; i++) { System.out.print(i + " "); }?

Rationale: The break statement terminates the loop immediately, and control passes to the statement following the loop. The continue statement (A) skips the current iteration and proceeds to the next one. Question 27 Which of the following is the correct way to generate a random integer between 1 and 100 in Java? A) int num = (int) (Math.random() * 100); B) int num = (int) (Math.random() * 100) + 1; C) int num = (int) (Math.random() * 101); D) int num = (int) (Math.random() * 100) - 1; Answer: B) int num = (int) (Math.random() * 100) + 1; Rationale: Math.random() returns a double between 0.0 (inclusive) and 1.0 (exclusive). Multiplying by 100 gives a value between 0.0 and 99.999, which when cast to int gives 0 to 99. Adding 1 shifts the range to 1 to 100. Option A gives 0 to 99. Option C gives 0 to

100. Option D gives - 1 to 98. Question 28 Which of the following correctly uses a PrintWriter object to write to a file? A) PrintWriter outputFile = new PrintWriter("data.txt"); B) PrintWriter outputFile = new PrintWriter(new File("data.txt")); C) PrintWriter outputFile = new PrintWriter(File("data.txt")); D) Both A and B are correct. Answer: D) Both A and B are correct. Rationale: The PrintWriter class can be constructed with either a String filename or a File object. Both A and B are valid. Option C is missing the new keyword.

Question 29 What is the value of x after the following code: int x = 0; for (int i = 0; i < 5; i++) { if (i ==

  1. break; x += i; }? A) 0 B) 3 C) 6 D) 10 Answer: B) 3 Rationale: The loop runs for i = 0, 1, 2, 3, .... When i == 3 , the break statement exits the loop before x += i is executed. Therefore, x accumulates 0 + 1 + 2 = 3_. The other options represent incorrect calculations._ Question 30 Which of the following is a correct while loop that prints the numbers 1 through 5? A) int i = 1; while (i <= 5) { System.out.print(i + " "); i++; } B) int i = 0; while (i < 5) { System.out.print(i + " "); i++; } C) int i = 1; while (i < 5) { System.out.print(i + " "); i++; } D) int i = 0; while (i <= 5) { System.out.print(i + " "); i++; } Answer: A) int i = 1; while (i <= 5) { System.out.print(i + " "); i++; } Rationale: Starting at i = 1 and continuing while i <= 5 prints 1, 2, 3, 4, 5. Option B prints 0 – 4. Option C prints 1–4. Option D prints 0–5. SECTION 5: METHODS (Questions 31–38) Question 31 Which of the following correctly defines a method that returns the sum of two integers?

C) public static calculate(double a, double b) D) public static double calculate(double a; double b) Answer: A) public static double calculate(double a, double b) Rationale: The method header includes the access modifier ( public ), the static keyword, the return type ( double ), the method name ( calculate ), and the parameter list with types and names. Option B uses void , which does not return a value. Option C is missing the return type. Option D uses a semicolon instead of a comma. Question 34 What is method overloading in Java? A) Defining multiple methods with the same name but different parameter lists B) Defining multiple methods with the same name and same parameter lists C) Defining a method that calls itself D) Defining a method inside another method Answer: A) Defining multiple methods with the same name but different parameter lists Rationale: Method overloading allows multiple methods with the same name as long as their parameter lists differ (by number, type, or order of parameters). This is different from overriding (C), which involves redefining a superclass method in a subclass. Question 35 Which of the following is a correct way to call a method named display that takes no arguments and returns nothing? A) display(); B) display; C) void display(); D) display(void); Answer: A) display();

Rationale: To call a method, you use its name followed by parentheses. If it takes no arguments, the parentheses are empty. Option B is missing parentheses. Options C and D are not valid method calls. Question 36 What is the scope of a local variable declared inside a method? A) The entire program B) The entire class C) The method in which it is declared D) The block in which it is declared Answer: C) The method in which it is declared Rationale: A local variable is declared inside a method and its scope is limited to that method. It cannot be accessed from other methods. If declared inside a block (e.g., inside an if statement), its scope is limited to that block (D). Question 37 Which of the following is a correct return statement for a method that returns a boolean value? A) return true; B) return 1; C) return "true"; D) return; Answer: A) return true; Rationale: A method that returns a boolean must return a boolean value ( true or false ). Option B returns an int , which is a type mismatch. Option C returns a String_. Option D returns nothing, which is only valid for_ void methods.

A) A method that is called automatically when an object is created and is used to initialize the object's fields B) A method that is used to destroy an object C) A method that returns the value of an instance field D) A method that is used to compare two objects Answer: A) A method that is called automatically when an object is created and is used to initialize the object's fields Rationale: A constructor is a special method with the same name as the class. It is called when an object is created using the new keyword and is used to initialize the object's instance fields. It has no return type. Question 41 Which of the following correctly creates an object of the Rectangle class and calls its setLength method? A) Rectangle rect = new Rectangle(); rect.setLength(10); B) Rectangle rect = Rectangle(); rect.setLength(10); C) Rectangle rect = new Rectangle; rect.setLength(10); D) new Rectangle rect = Rectangle(); rect.setLength(10); Answer: A) Rectangle rect = new Rectangle(); rect.setLength(10); Rationale: To create an object, you use the new keyword followed by the class constructor. Option B is missing the new keyword. Option C is missing parentheses after the constructor. Option D has incorrect syntax. Question 42 What is the purpose of the toString() method in a class? A) To convert an object's data to a string representation B) To destroy the object

C) To compare two objects D) To create a copy of the object Answer: A) To convert an object's data to a string representation Rationale: The toString() method returns a string representation of an object. It is automatically called when an object is passed to System.out.println() or when concatenated with a string. Overriding it allows you to provide a meaningful description of the object. Question 43 What is the difference between public and private access modifiers in Java? A) public members are accessible from any class, while private members are accessible only within the same class. B) public members are accessible only within the same class, while private members are accessible from any class. C) Both public and private members are accessible from any class. D) Both public and private members are accessible only within the same class. Answer: A) public members are accessible from any class, while private members are accessible only within the same class. Rationale: The public access modifier allows a class, method, or field to be accessed from any other class. The private access modifier restricts access to the same class only. This is a fundamental aspect of encapsulation. Question 44 What is a UML diagram used for in object-oriented programming? A) To write Java code B) To visually represent the structure of classes and their relationships C) To compile Java programs D) To debug runtime errors