All Flashcards
What is the function of the 'void' keyword in a method declaration?
It indicates that the method does not return a value.
What is the difference between System.out.print() and System.out.println()?
println() adds a new line after printing, while print() does not.
Why is Java considered a high-level language?
Because it is more human-readable and further removed from machine code.
What is the purpose of the Scanner class?
To obtain user input from the console.
Why should reserved words be avoided when naming variables?
Because they have predefined meanings in Java and cannot be used as variable names.
What happens during integer division?
The decimal part of the result is truncated.
What does the 'final' keyword do when declaring a variable?
It makes the variable constant, meaning its value cannot be changed after initialization.
Why is casting necessary?
To convert a value from one data type to another, especially when precision might be lost.
What is the significance of descriptive variable names?
They improve code readability and understanding.
What is the difference between declaration and initialization of a variable?
Declaration introduces the variable and its type; initialization assigns it a value.
How are primitive data types used in real-world applications?
int: Storing ages, counts, or quantities | double: Storing prices, temperatures, or measurements | boolean: Storing flags, status indicators, or conditions.
How is the Scanner class applied in real-world scenarios?
Taking user input for forms, games, or interactive applications.
How is casting applied in real-world scenarios?
Converting precise measurements to whole numbers for display or storage, or when interfacing with systems that require specific data types.
What does the following code output?
java
int x = 5;
double y = 2.0;
System.out.println(x / y);
2.5
What does the following code output?
java
int a = 10;
a %= 3;
System.out.println(a);
1
What does the following code output?
java
int num = 7;
num++;
System.out.println(num);
8
What does the following code output?
java
double price = 19.99;
int roundedPrice = (int) price;
System.out.println(roundedPrice);
19
What does the following code output?
java
int result = 5 + 3 * 2;
System.out.println(result);
11
Identify the error in the following code:
java
int number = 4.5;
System.out.println(number);
Cannot assign a double value to an int variable. Requires explicit casting.
What does the following code output?
java
System.out.println(5 / 2);
System.out.println(5.0 / 2);
2 2.5
What does the following code output?
java
int x = 8;
x /= 2;
System.out.println(x);
4
What does the following code output?
java
boolean flag = true;
System.out.println(!flag);
false
What does the following code output?
java
int count = 10;
count -= 5;
System.out.println(count);
5