Chapter 2 Practice Questions Flashcards
(89 cards)
A Java source code file can have more than one public class.
A) True
B) False
False
Explanation: A Java source file can only contain one public class.
What must the filename of a Java source file match?
A) The name of the main method
B) The name of the public class
C) The name of the first class in the file
D) The name of the method within the main class
B) The name of the public class
Explanation: The filename must match the name of the public class inside the file.
What will the following code do?
public class Example {
public static void main(String[] args) {
System.out.println(“Hello World!”);
}
}
A) The code will run and print “Hello World!” to the console.
B) The code will cause a compile-time error because the filename doesn’t match the class name.
C) The code will cause a runtime error due to the missing semicolon.
D) The code will not compile because the main method is missing a parameter.
A) The code will run and print “Hello World!” to the console.
Explanation: The code is correct and will print “Hello World!” to the console.
The Java programming language is case-sensitive, meaning Variable and variable are treated as different identifiers.
A) True
B) False
A) True
Explanation: Java is case-sensitive, meaning Variable and variable are considered different variables.
What is the correct syntax for defining the main method in Java?
A) public void main(String args)
B) public static void main(String[] args)
C) public static main(String args)
D) public void static main(String[] args)
public static void main(String[] args)
Explanation: This is the correct syntax for defining the main method in Java.
Which of the following does not require a semicolon in Java?
A) A statement inside the main method
B) A class header
C) A method header
D) Both B and C
D) Both B and C
Explanation: Class headers and method headers do not require semicolons.
What will happen when the following code is executed?
public class Test {
public static void main(String[] args) {
System.out.println(“Hello”);
// System.out.println(“World”);
}
}
A) The code will print “Hello World” to the console.
B) The code will print “Hello” only to the console.
C) The code will not compile because of the comment.
D) The code will throw a runtime exception.
B) The code will print “Hello” only to the console.
Explanation: The System.out.println(“World”); is commented out, so only “Hello” will be printed.
Every Java program must have a main method.
A) True
B) False
A) True
Explanation: Every Java application requires a main method as the entry point for execution.
What is the correct file extension for a Java source file?
A) .java
B) .class
C) .txt
D) .js
A) .java
Explanation: Java source files always use the .java extension.
What will this code do?
public class Example {
public static void main(String[] args) {
System.out.println(“Hello World!”);
System.out.println(“Goodbye World!”);
}
}
A) The code will print “Hello World!” followed by “Goodbye World!” to the console.
B) The code will print “Hello World!” followed by a compile-time error.
C) The code will cause a runtime error due to missing semicolons.
D) The code will cause a compile-time error because the class name doesn’t match the filename.
A) The code will print “Hello World!” followed by “Goodbye World!” to the console.
Explanation: The code is correct and will print both messages to the console.
What is the purpose of curly braces {} in Java?
A) To mark the beginning and end of the program.
B) To define the scope of methods and classes.
C) To separate variables within a method.
D) To define a loop or conditional block.
B) To define the scope of methods and classes.
Explanation: Curly braces {} define the boundaries or scope of classes and methods.
What will happen when the following code is executed?
public class Example {
public static void main(String[] args) {
int number = 5;
if (number > 0) {
System.out.println(“Positive”);
} else {
System.out.println(“Negative”);
}
}
}
A) The code will print “Positive” to the console.
B) The code will print “Negative” to the console.
C) The code will cause a runtime error because the variable is not initialized.
D) The code will not compile due to a missing semicolon.
A) The code will print “Positive” to the console.
Explanation: Since number is greater than 0, the “Positive” message will be printed.
For every opening curly brace {, there must be a corresponding closing curly brace }.
A) True
B) False
A) True
Explanation: Every opening curly brace { must have a matching closing curly brace }.
Which of the following is a valid Java comment?
A) /* This is a comment */
B) // This is a comment
C) Both A and B
D) None of the above
C) Both A and B
Explanation: Both /* */ and // are valid ways to add comments in Java.
What will happen when this code is run?
public class Simple {
public static void main(String[] args) {
System.out.println(“Hello World!”);
}
}
A) The code will run and print “Hello World!” to the console.
B) The code will throw an exception because the filename doesn’t match the class name.
C) The code will fail to compile due to a missing class name.
D) The code will run but print nothing to the console.
A) The code will run and print “Hello World!” to the console.
Explanation: The class name matches the filename, and the program is correct.
What is the console window that starts a Java application typically known as?
A) Standard Input Device
B) Standard Output Device
C) Java API
D) System Class
B) Standard Output Device
Explanation: The console window that starts a Java application is the standard output device, where information is sent to be displayed.
What is the standard input device typically in Java?
A) Mouse
B) Keyboard
C) Printer
D) Monitor
B) Keyboard
Explanation: The standard input device is typically the keyboard, where the user provides input to the program.
How does Java send information to the standard output device?
A) Through the Java Operating System
B) By using the System class stored in the Java API
C) By using the standard output class
D) By writing directly to the screen
B) By using the System class stored in the Java API
Explanation: Java sends information to the standard output device using the System class from the Java API.
What is the standard Java library commonly referred to as?
A) Java Compiler
B) Java API
C) Java Syntax
D) Java Developer Tools
B) Java API
Explanation: The standard Java library is commonly referred to as the Java API (Application Programming Interface).
What does the line System.out.println(“Programming is great fun!”); do?
A) It prints a statement on the console and then moves to a new line.
B) It prints the statement but does not move to a new line.
C) It prints the statement in uppercase.
D) It runs a loop to print the statement multiple times.
A) It prints a statement on the console and then moves to a new line.
Explanation: println prints the text and automatically moves to the next line.
What is the System class used for in Java?
A) It provides methods to manipulate strings.
B) It contains methods and objects for system-level tasks.
C) It manages Java collections.
D) It handles exception handling.
B) It contains methods and objects for system-level tasks.
Explanation: The System class provides system-level functionality like output and input handling.
What does the out object in the System class represent?
A) It represents the standard input device.
B) It represents the output device used for printing messages.
C) It represents a method for string concatenation.
D) It represents an error message output.
B) It represents the output device used for printing messages.
Explanation: out is an object of the System class that handles printing output.
How is the line System.out.println(“Programming is great fun!”); pronounced?
A) System dot out dot print
B) System dot println
C) System dot out dot println
D) Print line system
C) System dot out dot println
Explanation: This is how you pronounce the command to print a message to the console with a new line.
What does the println method do after printing the output?
A) It adds a space between the printed strings.
B) It prints the output on the same line.
C) It moves the cursor to the next line after printing the output.
D) It repeats the output multiple times.
C) It moves the cursor to the next line after printing the output.
Explanation: println adds a newline character after printing.