Java Basics Flashcards
What is the purpose of the main method in a Java program?
To execute the program
Which method moves the cursor to the next line after printing?
println()
What keyword is used to declare a constant in Java?
final
Which of the following is a valid identifier in Java?
A) 1stVariable
B) variable-name
C) $myVariable
D) my variable
C) $myVariable
What are the different primitive data types in Java?
A) int, float, String
B) int, double, boolean
C) char, String, Object
D) byte, String, boolean
B) int, double, boolean
How do you declare a variable in Java?
int x;
What is type casting in Java?
Converting one data type to another (e.g. int to double )
How do you declare a constant in Java?
final int x = 10;
What does the escape sequence \n do in Java?
Inserts a new line
What is the main difference between System.out.print() and System.out.println()?
println() adds a new line, while print() does not
Which of the following is a valid identifier in Java?
A) 1stVariable
B) first-variable
C) _firstVariable
D) first variable
C) _firstVariable
What is the primary purpose of the printf statement in Java?
To display formatted output
Which of the following is the correct syntax for the printf statement?
System.out.printf(format, item1, item2);
What does the format specifier %d represent in a printf statement?
An integer
Which of the following format specifiers would you use to display a floating-point number with two decimal places?
A) %f
B) %.2f
C) %d
D) %s
B) %.2f
What will be the output of the following code?
System.out.printf(“Value: %5d”, 42);
Value: 42
Which of the following is NOT a valid format specifier in the printf statement?
A) %s
B) %c
C) %b
D) %f
C) %b
What does the format specifier %s represent in a printf statement?
A) A single character
B) A string
C) An integer
D) A boolean
B) A string
what will correctly format a double value to three decimal places using printf?
12.34567
System.out.printf(“%.3f”, 12.34567);
What is the purpose of the remainder operator (%) in Java?
To find the remainder after division
Which of the following is a syntax error?
A) int x = 10;
B) System.out.println(“Hello World”)
C) if (x > 5 {
D) x = x + 1;
C) if (x > 5 {
What type of error occurs when a program attempts to divide by zero?
Runtime Error
Which of the following statements correctly checks if a number is even?
A) if (number % 2 = 0)
B) if (number % 2 == 0)
C) if (number / 2 == 0)
D) if (number & 2 == 0)
B) if (number % 2 == 0)
What is a logic error?
An error that produces incorrect results despite the program running successfully