lec 2 Flashcards

(32 cards)

1
Q

How do you print a variable in Java?

A

System.out.println(variableName);

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

How do you concatenate a variable with text in System.out.println()?

A

Use +, e.g., System.out.println(“Age: “ + age);

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

What happens when + is used between two numbers?

A

It performs addition.

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

What happens when + is used between a string and a number?

A

It concatenates them into a string.

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

How do you declare a constant variable?

A

Use the final keyword, e.g., final double PI = 3.14159;

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

What happens if you try to reassign a value to a final variable?

A

It causes a compilation error.

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

What are Java identifiers?

A

Names used for variables, methods, classes, etc.

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

What are the rules for naming identifiers?

A

They must start with a letter, _, or $, and cannot be a reserved word

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

How should Java constants be named?

A

Using uppercase letters with underscores, e.g., MAX_VALUE.

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

What must every Java program be contained within?

A

A class.

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

What is the naming rule for Java files?

A

The Java file name must match the class name and end with “.java”.

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

What is the purpose of System.out.println();?

A

It prints a line of text to the screen.

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

What must every Java statement end with?

A

A semicolon (;).

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

How does Java treat case sensitivity?

A

Java is case-sensitive; MyClass and myclass are different.

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

What is the purpose of the main() method in Java?

A

It serves as the entry point for execution.

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

What is the syntax for the main() method?

A

public static void main(String[] args)

17
Q

What are the two types of comments in Java?

A

Single-line (//) and multi-line (/* … */).

18
Q

How do you write a single-line comment?

A

Use // before the comment.

19
Q

How do you write a multi-line comment?

A

Use /* … */ to enclose multiple lines of comments.

20
Q

What are variables in Java?

A

Containers for storing data values.

21
Q

Name the five main variable types in Java.

A

String, int, float, char, and boolean.

22
Q

What data type would you use to store a whole number?

23
Q

What data type would you use to store a decimal number?

A

float or double.

24
Q

What data type would you use to store a single character?

25
What data type would you use to store true or false?
boolean.
26
How do you declare an integer variable?
int x;
27
How do you assign a value to a variable?
x = 10;
28
How do you declare and assign a variable in one step?
int x = 10;
29
What happens if you assign a new value to an existing variable?
It overwrites the previous value.
30
What is the syntax for declaring multiple variables in one line?
int x = 5, y = 6, z = 10;
31
What is the correct way to declare a floating-point variable in Java?
float x = 10.5f;
32
Which of the following is a valid Java identifier?
helloWorld123