1/58
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
1. Which of the following correctly instantiates a Scanner to read from standard input? (n=6)
D. Scanner s = new Scanner(System.in);
2. What is the result of the operation 30% 4?
C. 2 (30 divided by 4 is 7 with a remainder of 2.)
3. Which key word exits a switch case after execution?
b) break
4. Which line of code would you use if you want to format an output?
B) System.out.printf()
5. Which of these is incorrect?
C) system.out.println "x + y"; (Incorrect capitalization of `System`, and missing parentheses and semicolon.)
6. What does this code output: a=3; if (a<3){ System.out.println("Aww man :("); } else if (a>=3){ System.out.println("Yippie!!"); }
b.) Yippie!!
7. What is output to the terminal after the following code is run? System.out.println( (3/5) );
C. 0 (Integer division truncates the result: 3 divided by 5 is 0.)
8. Which output would be true? int x=10; if (x>10){ System.out.println("A"); } else if (x>=7){ System.out.println("B"); } else if (x>=4){ System.out.println("C"); } else{ System.out.println("D"); }
2. B (The first true condition is $x \ge 7$.)
9. double Juices = 16; if (Juices < 10) { System.out.print("Not enough Juices"); } else if (Juices == 10) { System.out.print("Just Enough Juices"); } else if (Juices > 10) { System.out.print("too many Juices"); } else System.out.print ("something went wrong"); What is the output?
C. Too many Juices
10. What printf flag is used to show a double? (n=2)
b) f% (The correct flag is `%f` for floating-point values.)
11. When printing a formatted line, what is the correct notation to use for a string?
b) %s
12. What is the Scanner class used for?
2. Reading user input
13. What does .2f do?
d. Round to 2 places (Used in `printf` to set the precision of a floating-point number to two decimal places.)
14. Select the correct line of code for this print statement: int day = 3; switch (day){ case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; default: System.out.println("Other day"); } What is the output?
C: Wednesday
15. Which of the following will present an error?
D) int x=2; float y=5 int result x/y; (Contains multiple fundamental syntax errors, including a missing equals sign for assignment.)
16. Which answer correct best casts the int fun=67 to a double called var? (n=3)
A. double var = (double) fun;
17. if (score >= 90) { System.out.println(A) } else (score >= 80) { System.out.println(B) } The input is 82 What is outputted?
2) B (Assuming the code was intended to be `else if (score >= 80)`. Since 82 is not $\ge 90$, it moves to the next condition, $82 \ge 80$, which is true.)
18. What is the value stored in j? a=2; j=0; j=a++;
c) 2 (The post-increment operator `a++` assigns the original value of `a` (2) to `j` before incrementing `a` to 3.)
19. Which one compiles
C. int x=5 (Uses the correct type capitalization. Note: It should end with a semicolon to compile: `int x=5;` )
20. How many decimal places will '%f' print by default?
A4: 6
21. What is the best data type to store single digit numbers?
C. short (It is an integer type large enough to store 0-9 and uses less memory than `int` or `double`.)
22. What is the result of this code? int x=5, y=7; if (x<10 && y>10) { System.out.println("A"); } else { System.out.println("B"); }
B. B (The condition $x < 10$ is true, but $y > 10$ is false. True AND False is False, so the `else` block executes.)
23. Which operator outputs the remainder of one integer divided by the other.
D.(%) (The modulo operator.)
24. System.out.print(int/double) will give what result:
b. double (The `int` is promoted to a `double` during the calculation, resulting in a floating-point value.)
25. Which of the following is a valid variable value for a number with decimals?
c. float price = 19.99;
26. What will the terminal output when the object is a "ball"? if (object.equals("owner") { ... } else if (object.equals("mailman") { ... } else if (object.equals("squirrel") { ... } else system.out.println("The dog sleeps"); }
D = The dog sleeps (Since "ball" does not match any of the preceding conditions, the `else` block executes.)
27. How do you print out a statement with precise formatting?
c) System.out.printf();
28. What is printed? int i=5; while (i<5) {} System.out.println(i); i++; System.out.println("Done");
A. 5 Done (The `while` loop condition $i < 5$ is immediately false, so the loop body is skipped. The program then prints the initial value of $i$ (5), and finally "Done".)
### Short Response Questions
A. Write a statement that will correctly print a float variable to two decimal places using printf? (n=2)
`System.out.printf("%.2f", myFloatVariable);`
B. Write the code to create and instantiate a new Scanner object called "scanner." (n=10)
`Scanner scanner = new Scanner(System.in);`
C. Write code that correctly uses a switch statement to print "hello world" when an int equals 1?
`int x = 1; switch (x) { case 1: System.out.println("hello world"); break; }`
D. Which type of conditional framework would be best when there are finite conditions and why? (n=2)
A `switch` statement. It is the most efficient and readable choice when checking a single variable against a finite set of discrete, constant values .
E. What data type is this variable? -32
`int` (Integer). It is the standard type for whole numbers, although it would also fit into a `byte` or `short`.
F. What do you put after every switch statement.
The `break;` keyword is placed after the code in each `case` block to prevent "fall-through" into the next case.
G. How do you type cast a double to an int?
By placing the target type `(int)` in parentheses before the variable you want to cast: `int myInt = (int) myDouble;`
H. What does the "i++" operator do?
It is the post-increment operator. It increases the value of the variable `i` by 1.
I. Write a short program that asks the user their age and name and print it out.
`import java.util.Scanner; //... in a main method ... Scanner s = new Scanner(System.in); System.out.print("Enter name: "); String name = s.nextLine(); System.out.print("Enter age: "); int age = s.nextInt(); System.out.printf("Hello %s, you are %d years old.\n", name, age); s.close();`
J. In your own words, what is the purpose of "else if" chains?
To test a series of mutually exclusive conditions sequentially. Only the code block corresponding to the first condition that evaluates to `true` is executed.
K. What happens if there's no condition match in an if/else statement without an else block?
If all `if` and `else if` conditions are false, the program skips all the conditional code blocks and continues execution with the code that follows the entire conditional structure.
L. Instantiate a for loop indexed at 1 that increments over 10 steps. (n=2)
`for (int i = 1; i <= 10; i++) { // ... }`
M. What is the difference between Switch statements and If/Else statements? (n=2)
`If/Else` tests Boolean expressions (ranges, inequalities, etc.). `Switch` tests a single variable for equality against discrete, constant values.
N. What is the difference between an int and a double?
`int` is a primitive data type for whole numbers (integers). `double` is a primitive data type for decimal numbers (floating-point).
O. Write a while loop that prints the numbers 1 through 5.
`int i = 1; while (i <= 5) { System.out.println(i); i++; }`
P. What data type would you use to store a decimal number?
`double` (The most common and precise choice in Java) or `float`.
Q. Explain what the following variable stores and why: boolean isEven = (14 % 2 == 0);
It stores the boolean value `true` . The expression $14 \% 2$ calculates the remainder, which is 0. The comparison $0 == 0$ is true.
R. Describe the difference between int, double, and String data types in Java.
`int` and `double` are primitive numeric types. `String` is a reference type (an object) used to store sequences of characters (text).
S. What does the following code print? int result = 3; for (int i = 0; i < 5; i++) { result = result 2; } System.out.println(result);
96 (The loop multiplies $3$ by $2$ five times: $3 \times 2^5 = 96$.)
T. Typecast a double to int
`int variableName = (int) doubleVariableName;`
U. Write a short program that declares a scanner and takes in an input from the user.
`import java.util.Scanner; //... Scanner s = new Scanner(System.in); System.out.print("Enter value: "); String input = s.nextLine(); s.close();`
V. Explain how to potentially write a program to find out if a number is odd or even.
Use the modulo operator (`%`) to check the remainder when the number is divided by 2. If `number % 2 == 0`, it is even . Otherwise, it is odd .
W. Explain how an if / else statement works.
The program evaluates a Boolean condition in the `if` block. If true, the `if` code runs. If false, the code in the optional `else` block runs. It provides a binary decision path.
X. If I was to switch from an int to float, write a type cast that would complete this task.
`float f = (float) myInt;` (The explicit cast is shown, though it's a widening conversion and is not strictly required by the compiler.)
Y. What is the difference between a float and a double data type.
`double` is double-precision (64-bit) and is the standard for high-accuracy decimals. `float` is single-precision (32-bit) and has less precision and a smaller range.
Z. What is the difference between System.out.print(); and System.out.println();?
`print()` prints the output and keeps the cursor on the same line . `println()` prints the output and moves the cursor to the next line (adds a newline character).
AA. Write a switch statement to determine if x is even or odd.
`int x = 5; switch (x % 2) { case 0: System.out.println("Even"); break; case 1: System.out.println("Odd"); break; }`
BB. What is the importance of breaks in switch statements, and why do we use them? (n=3)
`break` prevents "fall-through" . Without it, the program would execute the code blocks for all subsequent `case` labels after a match is found, leading to incorrect logic.
CC. What is wrong with the following command? - System.out.print("I am tired"):
The statement ends with a colon (`:`) instead of the required semicolon (`;`) .
DD. Why is it important to initialize a variable before attempting to use it in your program?
Initialization is necessary to ensure the variable has a known, defined state . Failing to initialize a local variable will cause a compiler error because the compiler cannot guarantee it holds a valid value.