ITCS112 lec - Fundamentals of Programming Long Quiz
What is the output for the following code fragment? int [ ] a = new int [5]; a[0] = 5; a[1] = 4; a[2] = 3; System.out.print(a[0] + a[1] + a[4]);
output: 9
What is the output of the following code fragment? int [ ] items = {2, 7, 3, 5, 8, 9}; int funny = items[0]; for (int i = 0; i < items.length; i++) { if (items[ i ] > funny) funny = items[ i ]; } System.out.print(funny);
output: 9
___________ are used to test conditions
relational operators
______ allows a variable to be tested for equality against a list of values.
switch statement
If you want to test equality of an expression based from a list of values what selection structure should you use?
switch structure
What happens to an instantiated array object?
the array object goes back to its default value.
(true/false) The increment part of the for loop is executed after the first iteration of the loop.
true
(true/false) The output of the following Java code is: Stoor. int count = 5; System.out.print("Sto"); do { System.out.print('o'); count--; } while (count >= 5); System.out.println('r');
true
Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
while loop
a ______ loop in Java programming repeatedly executes a target statement as long as a given condition is true.
while loop
how many times is * printed? how many times id # printed? int i = 0; while (i < 10) { i++; System.out.println("*"); } while (i < 8) i++; System.out.println("#");
* is printed 10 times # is printed once
flow of control in a for loop
- init - condition - increment - condition
What is the output of this program code? public static void main(String[] args) { int count = -2; while (count < 3){ System.out.print(count + " ); count = count + 1 ; } }
-2 -1 0 1 2
A property used to get the length of arrays for loops
.length
How many times the variable i is checked in the below code? int i = 0; do { i++; System.out.println("in while loop"); } while (i < 3);
3
The logical operator that is true only if both expression1 and expression2 are true
And operator
What is the output of the following code fragment: int[ ] testArray = new int[5]; testArray[0] = 34; testArray[1] = 88; System.out.println( testArray[0] + " " + testArray[1] + " " + testArray[5] );
Error
What is the output? public static void main(String[] args) { if (2 < 3 + 4) System.out.println("Hip Hip.."); System.out.print("Hurray"); else System.out.println("Victory"); }
Error
(True/False) The value of the compound expression will not depend on the meaning of the operator.
False
(True/False) If the condition is true, it will execute the statement inside else.
False
(true/false) If an array index is less than or equal to zero, an InvalidIndexException exception is thrown.
False
(true/false) Like a do...while loop, the body of a while loop executes at least once.
False
(true/false) The following for loop executes 21 times. (Assume all variables are properly declared.) for (i = 1; i <= 20; i = i + 1) System.out.println(i);
False
(true/false) The output of the Java code, assuming that all variables are properly declared, is 32. num = 10; while (num <= 32) num = num + 5; System.out.println(num);
False
(true/false) The output of the Java code, assuming that all variables are properly declared, is: 2 3 4 5 6. n = 2; while (n >= 6) { System.out.print(n + " "); n++; } System.out.println();
False
(true/false) If the size of an array is n, then the last element of the array will be at index n+1
False; the size of the index will be at n-1 because the array index always starts at 0
The body of a while loop eventually must make the condition false. If not, it is called an _____________ , which will execute until the user interrupts the program.
Infinite loop
What is the output? public static void main(String[] args) { int Mark = 10; if (Mark !=6) System.out.println(""Mark is getting milk tea"); else System.out.println(""Mark is getting halo-halo"); System.out.println("Mark is getting grapes."); j
Mark is getting milk tea. Mark is getting grapes.
Can an else statement exist without an if statement preceding it?
No, an else statement can only exist with an if statement
The logical operator that is true if expression is false and vice versa
Not operator
The logical operator that is true if either Expression1 or Expression2 is true
Or operator
What is the output of the following code fragment: int[ ] testArray = {2, 4, 6, 8 }; testArray[0] = 23; testArray[3] = testArray[1]; System.out.println( testArray[0] + " " + testArray[3] );
Output: 23 4
What is the output? public static void main(String[] args) { if ( 15 > 8) System.out.println("Philippines"); else if (4%2==0) System.out.println("China"); System.out.println("Indonesia"); }
Philippines Indonesia
is extensively used in programming because it allows the program to decide an action based upon user's input or other processes.
Selection Structure
A __________ is a special input value that represents the end of input.
Sentinel value
What is the difference between the = and == symbols?
The = symbol stores a value in a variable. The == symbol compares two values to see if they are equal.
(True/False) In a switch statement a block is nothing but multiple statements which are grouped for a particular case.
True
(True/False) Selection statements in programs can change the flow of control.
True
(True/False) Whenever the value of expression is not matched with any of the cases inside the switch, then the default will be executed.
True
(True/False) Else is paired with the nearest if without an else.
True
(True/False) if is reserved word in Java and must be written in lowercase.
True
(true/false) A counter-controlled loop is used when the exact number of iterations is known.
True
(true/false) Assume that all variables in the following code are properly declared and that the input is 3 7 4 -1. The output is 13. num = console.nextInt(); sum = num; while (num != -1) { num = console.nextInt(); sum = sum + num; } System.out.println(sum);
True
When a boolean array object is instantiated, its components are initialized to false.
True
What selection structure should you use if you want your program to do something if a condition is true, but do nothing if that condition is false?
an if selection structure
What selection structure should be used if you want your program to do something if a condition is true and do something different if it is false?
an if-else selection structure
How is an array declared in java?
dataType[ ] arrayName;
A _______ is similar to a while loop, except the fact that it is guaranteed to execute at least one time
do while loop
It is more like a while statement, except that it tests the condition at the end of the loop body.
do...while loop
If you want to test multiple conditions what selection structure should you use?
else if structure within an if or if-else structure.
What is the output for the following code fragment? int [ ] car = new int [7]; car[0] = 2004; car[1] = 2006; System.out.println(car[0] + " " + car[1] + " " car[7]);
error
Arrays does not have a fixed number of elements
false
The array index always starts at 1
false; it starts at 0
Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
for loop
is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
for loop
What is the output? public static void main(String[] args) { int x = 5; if (x < 1); System.out.print("hello™); if ( x == 5) System.out.print("hi"); else System.out.print("no"); }
hellohi
What is the output of the program? public static void main(String[] args) { int x = 5; if (x < 1) System.out.print("hello"); if ( x == 5) System.out.print("hi"); else System.out.print(""no"); }
hi
What is the output? public static void main(String[] args) { int x = 5; if (x < 1) System.out.print("hello"); if ( x == 5) System.out.print("hi"); else System.out.print(""no"); }
hi
indicates the position of the element within the array.
index
An ______ is a loop that never stops. This is a common logical (semantic) error.
infinite loop
What will occur to the code given the following declaration: double[] numList = new double[20]; //the statement numList[12] = numList[5] + numList[7];
it updates the content of the thirteenth component of the array numList.
__________ a symbol or word used to connect two or more expressions.
logical operator
You can use one or more loops inside any other while, for, or do..while loop.
nested loops
What are the three logical operators?
not (!) and (&&) or (||)
What is the output for the following code fragment? int [ ] dedo = {1, 3, 6, 9, 2, 5, 7}; int que = dedo[0]; for (int k = 0; k < dedo.length; k++) { if (dedo[k] < que) que = dedo[k]; } System.out.print(que);
output: 1
What is the output of the following code fragment? double [ ] x = new double [4]; x[0] = 8.5; x[1] = 6.5; x[2] = 9.5; x[3] = 12.5; System.out.println(x[1 + 2]);
output: 12.5