Java Basics Flashcards

1
Q

What is the purpose of the main method in a Java program?

A

To execute the program

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

Which method moves the cursor to the next line after printing?

A

println()

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

What keyword is used to declare a constant in Java?

A

final

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

Which of the following is a valid identifier in Java?
A) 1stVariable
B) variable-name
C) $myVariable
D) my variable

A

C) $myVariable

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

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

A

B) int, double, boolean

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

How do you declare a variable in Java?

A

int x;

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

What is type casting in Java?

A

Converting one data type to another (e.g. int to double )

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

How do you declare a constant in Java?

A

final int x = 10;

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

What does the escape sequence \n do in Java?

A

Inserts a new line

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

What is the main difference between System.out.print() and System.out.println()?

A

println() adds a new line, while print() does not

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

Which of the following is a valid identifier in Java?
A) 1stVariable
B) first-variable
C) _firstVariable
D) first variable

A

C) _firstVariable

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

What is the primary purpose of the printf statement in Java?

A

To display formatted output

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

Which of the following is the correct syntax for the printf statement?

A

System.out.printf(format, item1, item2);

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

What does the format specifier %d represent in a printf statement?

A

An integer

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

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

A

B) %.2f

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

What will be the output of the following code?
System.out.printf(“Value: %5d”, 42);

A

Value: 42

17
Q

Which of the following is NOT a valid format specifier in the printf statement?
A) %s
B) %c
C) %b
D) %f

A

C) %b

18
Q

What does the format specifier %s represent in a printf statement?
A) A single character
B) A string
C) An integer
D) A boolean

A

B) A string

19
Q

what will correctly format a double value to three decimal places using printf?
12.34567

A

System.out.printf(“%.3f”, 12.34567);

20
Q

What is the purpose of the remainder operator (%) in Java?

A

To find the remainder after division

21
Q

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;

A

C) if (x > 5 {

22
Q

What type of error occurs when a program attempts to divide by zero?

A

Runtime Error

23
Q

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)

A

B) if (number % 2 == 0)

24
Q

What is a logic error?

A

An error that produces incorrect results despite the program running successfully

25
Q

Which method is used to read an integer input from the user using the Scanner class?

A

nextInt()

26
Q

Which of the following statements is true about logic errors?
A) They are detected by the compiler.
B) They cause the program to crash.
C) They produce incorrect output without crashing the program.
D) They are the easiest type of error to identify.

A

C) They produce incorrect output without crashing the program.

27
Q

common cause of syntax errors?

A
  • Using the wrong variable type
  • Forgetting to close a string with a quotation mark
  • Incorrectly using operators
28
Q

What is the output of the following code if x is 10?
if (x = 5) {
System.out.println(“x is 5”);
}

A

A syntax error

29
Q

Which of the following statements correctly describes a runtime error?
A) It occurs when the code is written incorrectly.
B) It occurs when the program is running and encounters an unexpected condition.
C) It is always caused by a syntax error.
D) It can be fixed by simply recompiling the code.

A

It occurs when the program is running and encounters an unexpected condition.

30
Q

What type of error is caused by using a variable that has not been initialized?

A

Runtime Error

31
Q
A