Chapter 2 Practice Questions Flashcards
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.
What would happen if you run the following lines of code?
System.out.println(“This is being printed out”);
System.out.println(“on two separate lines.”);
A) Both lines will be printed on the same line.
B) The first line will be printed, but the second line will be ignored.
C) Both lines will be printed on separate lines.
D) The code will cause a runtime error.
C) Both lines will be printed on separate lines.
Explanation: println sends the output to a new line each time it is used.
How does the print method differ from the println method in Java?
A) print adds a newline character, while println does not.
B) println adds a newline character, while print does not.
C) println prints strings, while print only handles numbers.
D) There is no difference between print and println.
B) println adds a newline character, while print does not.
Explanation: print does not move to a new line, while println does.
What will the following code output?
System.out.print(“These lines will be”);
System.out.print(“printed on”);
System.out.println(“the same line.”);
A) These lines will be printed on separate lines.
B) These lines will be printed on the same line with proper spacing.
C) These lines will be printed on the same line without spaces.
D) The code will cause an error because print does not work.
C) These lines will be printed on the same line without spaces.
Explanation: The print method does not add a space or newline, causing the words to run together.
What does the escape sequence \n represent in Java?
A) A space between words
B) A new line
C) A tab space
D) A string separator
B) A new line
Explanation: \n is an escape sequence used to represent a newline character in a string.
What will the following code output?
System.out.print(“This line will have a newline at the end.\n”);
A) This line will be printed with no new line.
B) The text will print on the same line.
C) The text will be followed by a new line.
D) The code will produce an error.
C) The text will be followed by a new line.
Explanation: \n at the end of the string will move the cursor to the next line.
Why are escape sequences useful in Java?
A) They allow the programmer to print characters that would otherwise be unprintable.
B) They speed up the compilation process.
C) They remove unnecessary white spaces from the output.
D) They are used to create variables.
A) They allow the programmer to print characters that would otherwise be unprintable.
Explanation: Escape sequences like \n and \t help in printing special characters that can’t be typed directly.
Even though escape sequences are comprised of two characters, how are they treated by the compiler?
a) They are treated as two separate characters.
b) They are treated as a single character.
c) They are ignored by the compiler.
d) They cause a syntax error.
b) They are treated as a single character.
Explanation: Escape sequences like \n (newline) or \t (tab) are treated as a single character by the Java compiler, even though they are made up of two characters. This is why they can perform special formatting tasks in strings.
What does the + operator do in Java?
a) Adds two numbers only.
b) Concatenates strings only.
c) Can be used as both an addition operator and a concatenation operator.
d) None of the above.
c) Can be used as both an addition operator and a concatenation operator.
Explanation: The + operator can either perform addition (for numbers) or string concatenation (for strings). For example, “Hello” + “World” results in “HelloWorld”, and 5 + 3 results in 8.
What will this line of code output?
System.out.println(“Hello “ + “World”);
a) HelloWorld
b) Hello World
c) “Hello “ + “World”
d) It will cause a compilation error.
b) Hello World
Explanation: The + operator concatenates the two strings “Hello “ and “World”, resulting in “Hello World” being printed out.
How can you concatenate strings and variables in Java?
a) By using the & operator.
b) By using the + operator.
c) By using the concat() method.
d) None of the above.
b) By using the + operator.
Explanation: The + operator is used to concatenate strings and other variables (e.g., numbers, booleans) in Java.
How do you handle long string literals that span multiple lines in Java?
a) By breaking them into separate lines without any operator.
b) By using string concatenation with the + operator.
c) By using \n to break the line.
d) It is not allowed in Java.
b) By using string concatenation with the + operator.
Explanation: In Java, string literals cannot span multiple lines unless they are concatenated using the + operator. For example:
System.out.println(“This is a long string “ +
“that spans multiple lines.”);