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