lec3 Flashcards

(30 cards)

1
Q

What are the two main categories of data types in Java?

A

Primitive data types (byte, short, int, long, float, double, boolean, char) and Non-primitive data types (String, Arrays, Classes).

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

What are the integer types in Java?

A

byte, short, int, long.

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

What are the floating-point types in Java?

A

float, double.

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

What is the default type for a floating-point literal in Java?

A

double. Use f or F to define a float (e.g., 5.75f).

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

How do you declare a boolean variable in Java?

A

boolean isJavaFun = true;

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

How do you declare a character in Java?

A

char myChar = ‘A’; or using ASCII: char a = 65;

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

How do you create a Scanner object to read user input?

A

Scanner input = new Scanner(System.in);

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

What method do you use to read an integer from the user?

A

input.nextInt();

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

What method do you use to read a double from the user?

A

input.nextDouble();

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

What is a common mistake when using multiple Scanner objects?

A

Creating redundant Scanner objects instead of reusing one.

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

What are the five types of operators in Java?

A

Arithmetic, Assignment, Comparison, Logical, Bitwise.

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

What does the modulus operator % do?

A

It returns the remainder of a division (5 % 2 results in 1).

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

What is the difference between ++var and var++?

A

++var (pre-increment) increases the value before use, var++ (post-increment) increases it after use.

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

What does x += 5 mean?

A

It is the same as x = x + 5;

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

What does 5 / 2 return in Java?

A

2 (integer division).

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

How can you ensure 5 / 2 returns 2.5?

A

Use 5.0 / 2 or (double) 5 / 2.

17
Q

What happens if an integer literal is too large for the assigned variable?

A

A compilation error occurs.

18
Q

What happens when 1.0 - 0.9 is printed in Java?

A

Due to floating-point precision errors, it prints 0.09999999999999998.

19
Q

What are the two types of type casting in Java?

A

Widening (automatic) and Narrowing (manual).

20
Q

What is widening casting?

A

Converting a smaller type to a larger type (e.g., int → double).

21
Q

What is narrowing casting?

A

Converting a larger type to a smaller type (e.g., double → int).

22
Q

How do you explicitly cast double myDouble = 9.78; to an int?

A

int myInt = (int) myDouble;

23
Q

What happens if you use a variable without initializing it?

A

Compilation error due to an undeclared or uninitialized variable.

24
Q

What is integer overflow?

A

When an int exceeds its max value (2147483647 + 1 becomes -2147483648).

25
What is the unintended integer division problem?
When dividing two integers, Java truncates the decimal (e.g., 3 / 2 = 1).
26
How do you avoid unintended integer division?
Use 3 / 2.0 or (double) 3 / 2.
27
What does Math.pow(2, 3) return?
8.0 (because 2³ = 8).
28
What does System.currentTimeMillis() do?
Returns the current time in milliseconds since January 1, 1970 (Unix Epoch Time).
29
What is the output of System.out.println(1.0 / 3.0);?
0.3333333333333333 (double precision).
30
What Java operator is used to compare two values?
== (Equal to).