lec3 Flashcards
(30 cards)
What are the two main categories of data types in Java?
Primitive data types (byte, short, int, long, float, double, boolean, char) and Non-primitive data types (String, Arrays, Classes).
What are the integer types in Java?
byte, short, int, long.
What are the floating-point types in Java?
float, double.
What is the default type for a floating-point literal in Java?
double. Use f or F to define a float (e.g., 5.75f).
How do you declare a boolean variable in Java?
boolean isJavaFun = true;
How do you declare a character in Java?
char myChar = ‘A’; or using ASCII: char a = 65;
How do you create a Scanner object to read user input?
Scanner input = new Scanner(System.in);
What method do you use to read an integer from the user?
input.nextInt();
What method do you use to read a double from the user?
input.nextDouble();
What is a common mistake when using multiple Scanner objects?
Creating redundant Scanner objects instead of reusing one.
What are the five types of operators in Java?
Arithmetic, Assignment, Comparison, Logical, Bitwise.
What does the modulus operator % do?
It returns the remainder of a division (5 % 2 results in 1).
What is the difference between ++var and var++?
++var (pre-increment) increases the value before use, var++ (post-increment) increases it after use.
What does x += 5 mean?
It is the same as x = x + 5;
What does 5 / 2 return in Java?
2 (integer division).
How can you ensure 5 / 2 returns 2.5?
Use 5.0 / 2 or (double) 5 / 2.
What happens if an integer literal is too large for the assigned variable?
A compilation error occurs.
What happens when 1.0 - 0.9 is printed in Java?
Due to floating-point precision errors, it prints 0.09999999999999998.
What are the two types of type casting in Java?
Widening (automatic) and Narrowing (manual).
What is widening casting?
Converting a smaller type to a larger type (e.g., int → double).
What is narrowing casting?
Converting a larger type to a smaller type (e.g., double → int).
How do you explicitly cast double myDouble = 9.78; to an int?
int myInt = (int) myDouble;
What happens if you use a variable without initializing it?
Compilation error due to an undeclared or uninitialized variable.
What is integer overflow?
When an int exceeds its max value (2147483647 + 1 becomes -2147483648).