Chapter 2 Practice Questions Flashcards

1
Q

A Java source code file can have more than one public class.

A) True
B) False

A

False
Explanation: A Java source file can only contain one public class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A

B) The name of the public class
Explanation: The filename must match the name of the public class inside the file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The Java programming language is case-sensitive, meaning Variable and variable are treated as different identifiers.

A) True
B) False

A

A) True
Explanation: Java is case-sensitive, meaning Variable and variable are considered different variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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)

A

public static void main(String[] args)
Explanation: This is the correct syntax for defining the main method in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

D) Both B and C
Explanation: Class headers and method headers do not require semicolons.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Every Java program must have a main method.

A) True
B) False

A

A) True
Explanation: Every Java application requires a main method as the entry point for execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the correct file extension for a Java source file?

A) .java
B) .class
C) .txt
D) .js

A

A) .java
Explanation: Java source files always use the .java extension.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

B) To define the scope of methods and classes.
Explanation: Curly braces {} define the boundaries or scope of classes and methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A) The code will print “Positive” to the console.
Explanation: Since number is greater than 0, the “Positive” message will be printed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

For every opening curly brace {, there must be a corresponding closing curly brace }.

A) True
B) False

A

A) True
Explanation: Every opening curly brace { must have a matching closing curly brace }.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

C) Both A and B
Explanation: Both /* */ and // are valid ways to add comments in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A) The code will run and print “Hello World!” to the console.
Explanation: The class name matches the filename, and the program is correct.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the standard input device typically in Java?

A) Mouse
B) Keyboard
C) Printer
D) Monitor

A

B) Keyboard
Explanation: The standard input device is typically the keyboard, where the user provides input to the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the standard Java library commonly referred to as?

A) Java Compiler
B) Java API
C) Java Syntax
D) Java Developer Tools

A

B) Java API
Explanation: The standard Java library is commonly referred to as the Java API (Application Programming Interface).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

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.

A

B) It contains methods and objects for system-level tasks.
Explanation: The System class provides system-level functionality like output and input handling.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

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.

A

B) It represents the output device used for printing messages.
Explanation: out is an object of the System class that handles printing output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

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.

A

C) It moves the cursor to the next line after printing the output.
Explanation: println adds a newline character after printing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

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.

A

C) Both lines will be printed on separate lines.
Explanation: println sends the output to a new line each time it is used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

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.

A

B) println adds a newline character, while print does not.
Explanation: print does not move to a new line, while println does.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

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

A

B) A new line
Explanation: \n is an escape sequence used to represent a newline character in a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

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.

A

b) Hello World
Explanation: The + operator concatenates the two strings “Hello “ and “World”, resulting in “Hello World” being printed out.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

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.

A

b) By using the + operator.
Explanation: The + operator is used to concatenate strings and other variables (e.g., numbers, booleans) in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

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.

A

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.”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

What is the rule for variable naming conventions in Java?
a) Variable names should start with an uppercase letter.
b) Variable names should start with a lowercase letter and use camelCase.
c) Variable names should be all lowercase.
d) Variable names can be any combination of letters and numbers.

A

b) Variable names should start with a lowercase letter and use camelCase.
Explanation: Java follows camelCase naming convention for variables. This means variable names should start with a lowercase letter and subsequent words should start with an uppercase letter (e.g., totalAmount, userName).

37
Q

What is the naming convention for class names in Java?
a) Class names should be in camelCase.
b) Class names should be all uppercase.
c) Class names should use Title Case, with each word capitalized.
d) Class names should use snake_case.

A

c) Class names should use Title Case, with each word capitalized.
Explanation: In Java, class names are typically written in Title Case, meaning the first letter of each word is capitalized (e.g., MyClass, EmployeeDetails).

38
Q

What should Java variable names be like?
a) Variable names should be short and cryptic.
b) Variable names should be descriptive to make code more readable.
c) Variable names should start with numbers.
d) Variable names can only be one character long.

A

b) Variable names should be descriptive to make code more readable.
Explanation: Descriptive variable names improve code readability and maintainability. For example, salesTaxRate is better than tr.

39
Q

Which of the following is an example of a primitive data type in Java?
a) String
b) Object
c) int
d) Array

A

c) int
Explanation: int is a primitive data type used to represent integer values. String and Object are not primitive data types; String is an object type, and Object is the root class of all Java classes.

40
Q

What is the difference between integer and floating-point data types in Java?
a) Integer data types hold whole numbers, while floating-point data types hold numbers with decimals.
b) Floating-point data types hold whole numbers, while integer data types hold numbers with decimals.
c) There is no difference.
d) Floating-point data types cannot hold negative numbers.

A

a) Integer data types hold whole numbers, while floating-point data types hold numbers with decimals.
Explanation: Integer types (like int and long) hold whole numbers, while floating-point types (like float and double) hold numbers that can have a fractional part.

41
Q

What is the default type for floating-point literals in Java?
a) float
b) double
c) int
d) long

A

b) double
Explanation: By default, floating-point literals (e.g., 3.14) are treated as double unless explicitly specified as float by appending an F or f (e.g., 3.14F).

42
Q

How can you assign a floating-point value to a float variable in Java?
a) By using the F or f suffix.
b) By using the float() method.
c) By converting the double to float.
d) It is not possible to assign a floating-point value to a float variable.

A

a) By using the F or f suffix.
Explanation: When assigning a floating-point value to a float variable, you must append the value with an F or f to indicate that it is a float and not a double (e.g., 23.5F).

43
Q

What will happen if you try to assign a double value to a float variable without casting?
a) The program will work without any issues.
b) You will get an error because a double has more precision than a float.
c) The double value will be automatically converted to float.
d) The program will crash.

A

b) You will get an error because a double has more precision than a float.
Explanation: A float variable cannot store a double value directly because double has higher precision. You must explicitly cast it or use the F suffix.

44
Q

Can a string literal span multiple lines in Java?
a) Yes, it can.
b) No, it cannot without causing a syntax error.
c) Yes, but only if it is concatenated with the + operator.
d) No, unless you use a special escape sequence.

A

b) No, it cannot without causing a syntax error.
Explanation: A string literal cannot span multiple lines in Java without causing a syntax error unless it is concatenated with the + operator.

45
Q

What will the following code output?

System.out.println(“Hello\nWorld!”);
a) HelloWorld!
b) Hello\nWorld!
c) Hello
World!
d) Hello World!

A

c) Hello
World!
Explanation: The \n escape sequence creates a new line between “Hello” and “World!”.

46
Q

Which of the following is NOT an escape sequence in Java?
a) \n
b) \t
c) \b
d) \z

A

d) \z
Explanation: \z is not a valid escape sequence. The common escape sequences include \n (new line), \t (tab), and \b (backspace).

47
Q

What will this code print?

String str = “Welcome to the world of "Java"!”;
System.out.println(str);
a) Welcome to the world of “Java”!
b) Welcome to the world of “Java”! with no quotes
c) Welcome to the world of Java!
d) It will cause a syntax error.

A

a) Welcome to the world of “Java”!
Explanation: The escape sequence " is used to print the double quotes around “Java”.

48
Q

Which of the following correctly concatenates the strings “Java” and “Programming” with a space between them?
a) “Java” + “ “ + “Programming”
b) “Java”.concat(“Programming”)
c) “Java” “Programming”
d) “Java “ + “Programming”

A

a) “Java” + “ “ + “Programming”
Explanation: You can concatenate strings in Java using the + operator. To add a space, it must be explicitly included as “ “.

49
Q

Which of these is true about variable names in Java?
a) They must start with a number.
b) They can contain spaces.
c) They can use special characters like @ or $.
d) They must be declared as final to be used.

A

c) They can use special characters like @ or $.
Explanation: In Java, variable names can contain letters, digits, underscores (_), and dollar signs ($). They cannot start with a number or contain spaces.

50
Q

What does the following Java code do?

int a = 5;
float b = 3.2;
System.out.println(a + b);
a) It will print 8.2.
b) It will print 8.
c) It will throw a compilation error because b is a float.
d) It will cause a runtime exception.

A

a) It will print 8.2.
Explanation: Java automatically promotes the int to a float when performing arithmetic with a float, so the result is a floating-point number (8.2).

51
Q

Which of the following statements is true about Java escape sequences?
a) Escape sequences are always treated as separate characters.
b) Escape sequences are treated as special characters that can affect output formatting.
c) Escape sequences can only be used within strings.
d) Escape sequences are used for variable declarations.

A

b) Escape sequences are treated as special characters that can affect output formatting.
Explanation: Escape sequences like \n (new line) or \t (tab) are used to format text output, but they are treated as single characters by the compiler.

52
Q

The + operator in Java can be used for both addition of numbers and concatenation of strings.
A) True
B) False

A

A) True
Explanation: In Java, the + operator serves dual purposes: adding numbers (e.g., 5 + 3 = 8) and concatenating strings (e.g., “Hello” + “ World” = “Hello World”).

53
Q

A float literal can be assigned to a variable of type double without any explicit casting.
A) True
B) False

A

A) True
Explanation: A float value can be assigned to a double variable without casting because double has a larger range and precision than float

54
Q

What will happen if the following code is executed?

System.out.println(“Hello “ + 3 + 2);
a) It will print 5.
b) It will print 32.
c) It will print Hello 32.
d) It will print Hello 5.

A

c) It will print Hello 32.
Explanation: In this case, the + operator first concatenates the string “Hello “ with the number 3, resulting in “Hello 3”. Then, it concatenates that with 2, resulting in “Hello 32”.

55
Q

What will this code print?

System.out.println(“I have “ + 5 + “ apples.”);
a) I have 5 apples.
b) I have apples.
c) I have 5 apples
d) I have 5 apples.0

A

a) I have 5 apples.
Explanation: The + operator concatenates the string “I have “, the number 5, and the string “ apples.”, resulting in “I have 5 apples.”.

56
Q

What is the correct way to represent the number 1000 in float type in Java?
a) 1000f
b) 1000.0f
c) 1000F
d) All of the above

A

d) All of the above
Explanation: float literals in Java are represented by appending either f or F to the number, so 1000f, 1000.0f, and 1000F are all valid representations

57
Q

What will happen if the following code is executed?

double d = 3.14;
float f = d;
a) The code will compile and run correctly.
b) It will compile but cause a runtime exception.
c) It will throw a compilation error because double cannot be assigned to a float directly.
d) The code will not compile due to a syntax error.

A

c) It will throw a compilation error because double cannot be assigned to a float directly.
Explanation: A double has more precision than a float, so you must explicitly cast it to a float (e.g., float f = (float) d;).

58
Q

What will this code print?

String str = “Java”;
str = str + “ Programming”;
System.out.println(str);
a) Java Programming
b) JavaProgramming
c) Java
d) It will cause a compilation error.

A

a) Java Programming
Explanation: The code concatenates “Java” and “ Programming”, and the resulting string “Java Programming” is printed.

59
Q

What will happen if the following code is executed?

System.out.println(3 + 2 + “ is the result”);
a) It will print 5 is the result.
b) It will print 32 is the result.
c) It will print 3 is the result.
d) It will throw a syntax error.

A

Answer: b) It will print 32 is the result.
Explanation: The + operator first adds 3 and 2, resulting in 5, but then concatenates the result with the string “ is the result”, yielding the output “32 is the result”.

60
Q

Which of the following is the correct way to declare a char variable in Java?

a) char letter = “A”;
b) char letter = ‘A’;
c) char letter = A;
d) char letter = “a”;

A

char letter = ‘A’;

Explanation: char literals must be enclosed in single quotes, such as ‘A’, not double quotes or unquoted values.

61
Q

What two values can a boolean variable hold in Java?

a) true and false
b) 1 and 0
c) true and false or yes and no
d) yes and no

A

a) true and false

Explanation: The boolean data type in Java only holds true or false values.

62
Q

How are characters stored in Java internally?

a) As integers using ASCII encoding.
b) As Unicode characters, taking 2 bytes each.
c) As decimal numbers.
d) As hexadecimal values.

A

b) As Unicode characters, taking 2 bytes each.

Explanation: Java uses Unicode to store characters, allowing it to represent 65,536 characters, with each character taking 2 bytes.

63
Q

What will happen if you try to use an uninitialized local variable in Java?

a) The code will run but print an error.
b) The code will throw a runtime exception.
c) The code will compile without issues.
d) The code will generate a syntax error during compilation.

A

d) The code will generate a syntax error during compilation.

Explanation: In Java, local variables must be initialized before use. Failing to do so results in a compile-time error.

64
Q

Which of the following is a valid binary operator in Java?

a) ++
b) -
c) = d) ?

A

b) -

Explanation: A binary operator is one that requires two operands, such as the subtraction operator (-). The ++ operator is unary, while = is an assignment operator and ? is a ternary operator.

65
Q

What is the output of the following code?

int x = 5;
int y = 2;
System.out.println(x / y);
a) 2.5
b) 2
c) 3
d) Compilation error

A

b) 2

Explanation: Since both x and y are integers, Java performs integer division, truncating the decimal part. The result is 2.

66
Q

What will this code output?

int a = 10;
a += 5;
System.out.println(a);
a) 15
b) 105
c) 10
d) Syntax Error

A

a) 15

Explanation: The += operator is a combined assignment operator. It adds 5 to a and assigns the result back to a, which is 15.

67
Q

Which of the following is the correct way to declare a constant in Java?

a) int final TAX_RATE = 0.05;
b) final int TAX_RATE = 0.05;
c) constant int TAX_RATE = 0.05;
d) int TAX_RATE = 0.05; final

A

b) final int TAX_RATE = 0.05;

Explanation: Constants in Java are declared using the final keyword. By convention, constants are named in uppercase letters.

68
Q

What will be the result of the following Java code?

String greeting = “Hello”;
greeting = new String(“Hello”);
System.out.println(greeting);
a) Hello
b) Compilation error
c) null
d) new String(“Hello”)

A

a) Hello

Explanation: The new String(“Hello”) creates a new String object, but the value remains “Hello”. Java automatically uses the same value for string literals, so the output is “Hello”.

69
Q

What is the main difference between a char and a String in Java?

a) char represents a sequence of characters, while String represents a single character.
b) char is a primitive type and String is an object.
c) char is an object, and String is a primitive type.
d) There is no difference; they are both the same.

A

b) char is a primitive type and String is an object.

Explanation: char is a primitive type that holds a single character, while String is an object in Java used to represent a sequence of characters.

70
Q

What is the scope of a local variable in Java?

a) From the method’s declaration to the method’s return statement.
b) From the class declaration to the end of the method.
c) From its declaration to the end of the method.
d) The scope is global.

A

c) From its declaration to the end of the method.

Explanation: The scope of a local variable is limited to the method in which it is declared. It can only be accessed within that method.

71
Q

Which of the following code will result in a compilation error?

a) final int maxSpeed = 120;
b) final int maxSpeed; maxSpeed = 120;
c) final int maxSpeed; maxSpeed = 120; maxSpeed = 150;
d) int maxSpeed = 120;

A

c) final int maxSpeed; maxSpeed = 120; maxSpeed = 150;

Explanation: Once a final variable is initialized, its value cannot be changed. The code maxSpeed = 150; will cause a compilation error.

72
Q

How can a String object be created in Java?

a) Using the new keyword or by assigning a string literal.
b) Only by using the new keyword.
c) By declaring it as a char variable.
d) By using the final keyword.

A

a) Using the new keyword or by assigning a string literal.

Explanation: You can create a String object either by assigning a string literal (e.g., String str = “Hello”;) or by using the new keyword (e.g., String str = new String(“Hello”);).

73
Q

What is the result of this code snippet?

String value = “Hello”;
int stringSize = value.length();
System.out.println(stringSize);
a) 5
b) Hello
c) 6
d) Compilation error

A

a) 5

Explanation: The length() method of a String object returns the number of characters in the string. The string “Hello” has 5 characters, so the output is 5.

74
Q

How do you declare a variable that holds a reference to a String object in Java?

a) String myString = “Hello”;
b) String myString = new String(“Hello”);
c) String myString;
d) All of the above

A

d) All of the above

Explanation: You can declare and initialize a String variable either using a string literal (e.g., “Hello”) or by using the new keyword (e.g., new String(“Hello”)). Both ways are valid in Java.

75
Q

What is the purpose of Javadoc comments in Java?
a) To format code
b) To generate HTML documentation for your code
c) To provide comments in the code only for other developers
d) To execute specific tasks in Java programs

A

b) To generate HTML documentation for your code
Explanation: Javadoc comments are used to generate documentation that explains the code in HTML format.

76
Q

Which of the following commands will generate Javadoc documentation for the Comment3.java file?
a) javac Comment3.java
b) javadoc Comment3.java
c) java Comment3.java
d) document Comment3.java

A

b) javadoc Comment3.java
Explanation: The javadoc command generates HTML documentation for the specified source file.

77
Q

What are the whitespace characters in Java that are ignored by the compiler?
a) Only spaces
b) Space, tab, newline, carriage return, and form feed
c) Tab and newline
d) Only tab

A

b) Space, tab, newline, carriage return, and form feed
Explanation: All mentioned whitespace characters are ignored by the compiler but help with the readability and formatting of code.

78
Q

True or False: It is best practice to use tab characters for indentation in Java code.
a) True
b) False

A

b) False
Explanation: It’s best to use spaces, not tabs, for indentation because tabs can vary in size across different editors or environments.

79
Q

How do you read input from the user in Java using the Scanner class?
a) Scanner read = new Scanner(System.in);
b) Scanner input = new Scanner(userInput);
c) Scanner keyboard = new Scanner(System.in);
d) Scanner scan = new Scanner(reader);

A

c) Scanner keyboard = new Scanner(System.in);
Explanation: You use Scanner keyboard = new Scanner(System.in); to read input from the standard input stream.

80
Q

Which method in the JOptionPane class is used to display an input dialog box to the user?
a) showMessageDialog()
b) showInputDialog()
c) inputMessageDialog()
d) displayDialog()

A

b) showInputDialog()
Explanation: showInputDialog() displays an input dialog box where the user can type a response.

81
Q

What will the following code do?

String name = JOptionPane.showInputDialog(“Enter your name:”);
a) Display a message to the user
b) Show an input dialog box asking for the user’s name
c) Display the name the user inputs in a message box
d) The code will not run because JOptionPane is not imported

A

b) Show an input dialog box asking for the user’s name
Explanation: This code will display an input dialog box where the user can input their name. If the user clicks OK, the name is stored in the name variable.

82
Q

What happens if the user clicks the “Cancel” button in the JOptionPane input dialog?
a) The program exits
b) The method returns null
c) The method returns an empty string
d) The program displays a default name

A

b) The method returns null
Explanation: Clicking “Cancel” in a JOptionPane input dialog returns null.

83
Q

True or False: The System.exit(0); command stops the execution of a Java program immediately.
a) True
b) False

A

a) True
Explanation: System.exit(0) stops the program, where 0 indicates successful termination. If the exit code is non-zero, it typically signals an error.

84
Q

What will be the result of this code?

String number = “100”;
int num = Integer.parseInt(number);
a) The value of num will be 100
b) The code will throw an error
c) The value of num will be “100”
d) The code will compile but the program will not run

A

a) The value of num will be 100
Explanation: Integer.parseInt() converts the string “100” to the integer value 100.

85
Q

Which of the following is the correct method to convert a string into a double in Java?
a) String.toDouble()
b) Double.parseInt()
c) Double.parseDouble()
d) String.convertToDouble()

A

c) Double.parseDouble()
Explanation: Double.parseDouble() converts a string representing a decimal number to a double

86
Q

What is the output of the following code?

String str = “25”;
int age = Integer.parseInt(str);
a) The variable age will contain the value “25”
b) The program will throw an error
c) The variable age will contain the integer value 25
d) The variable age will remain uninitialized

A

c) The variable age will contain the integer value 25
Explanation: Integer.parseInt(str) converts the string “25” into the integer value 25.

87
Q

What is the advantage of using parse methods like Integer.parseInt() and Double.parseDouble() in Java?
a) They convert numbers into strings
b) They convert string representations of numbers into numeric types
c) They are only used for error handling
d) They are used to display strings in numeric format

A

b) They convert string representations of numbers into numeric types
Explanation: These methods allow you to convert string representations of numbers into numeric types such as int and double.

88
Q

What will happen if you try to use tab characters for indentation in Java?
a) The program will not compile
b) The program will compile but look messy depending on the environment
c) Java will throw a whitespace error
d) The program will run but result in runtime errors

A

b) The program will compile but look messy depending on the environment
Explanation: Using tabs for indentation can cause inconsistent formatting depending on the environment, but it won’t prevent the program from compiling.

89
Q
A