Test 2 Flashcards
(22 cards)
Analyze the following code fragment: int x = 1; while (0 < x) && (x < 100) System.out.println(x++); What is the issue?
The code does not compile because (0 < x) && (x < 100) is not enclosed in a pair of parentheses.
Show the output of the following code:
public class Test
{ public static void main(String[] args) { int[] x = {1, 2, 3, 4, 5};
increase(x);
int[] y = {1, 2, 3, 4, 5};
increase(y[0]); System.out.println(x[0] + “ “ + y[0]);
}
public static void increase(int[] x) { for (int i = 0; i < x.length; i++) x[i]++; }
public static void increase(int y) { y++; }}
2 1
Given the following method, what is the printout of the call nPrint(‘a’, 4)?
Invalid call because char ‘a’ cannot be passed to string message.
Which of the loop statements always have their body executed at least once?
The do-while loop
What is the printout of the following program? public class Test { public static void main(String[] args) { int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}}; int v = values[0][0]; for (int[] list : values) for (int element : list) if (v > element) v = element; System.out.print(v); }}
1
How many times will the following code print “Welcome to Java”? int count = 0; while (count++ < 10) { System.out.println(“Welcome to Java”);}
10
Analyze the following code: public class Test { public static void main(String[] args) { int[] x = new int[3]; System.out.println(“x[0] is “ + x[0]); }}
The program runs fine and displays x[0] is 0.
What is the value of balance after the following code is executed? int balance = 10; while (balance >= 1) { if (balance < 9) break; balance = balance - 9; }
1
What is output of the following code: public class Test { public static void main(String[] args) { int list[] = {1, 2, 3, 4, 5, 6}; for (int i = 1; i < list.length; i++) list[i] = list[i - 1]; for (int i = 0; i < list.length; i++) System.out.print(list[i] + “ “); }}
1 1 1 1 1 1
How many times will the following code print “Welcome to Java”? int count = 0; do { System.out.println(“Welcome to Java”); count++;} while (count < 10);
10
Which of the following statements are correct?
char[][] charArray = {{‘a’, ‘b’}, {‘c’, ‘d’}};
The following code displays: public class PrintTriangle { public static void main(String[] args) { int num = 5; for(int i = 1; i <= num; i++) { for(int j = 0; j < i; j++) { System.out.print(“*”); } System.out.println(); } }}
.
..
…
….
…..
What is y after the following for loop statement is executed? int y = 0; for (int i = 0; i < 10; ++i) { y += 1; }
10
What is the index variable for the element at the first row and first column in array a?
a[0][0]
How many times will the following code print “Welcome to Java”? int count = 0; while (count < 9) { System.out.println(“Welcome to Java”); count++;}
9
Analyze the following code: public class Test { public static void main(String[] args) { int[] x = new int[5]; int i; for (i = 0; i <= x.length; i++) x[i] = i; System.out.println(x[i]); }}
The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException.
If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is __________.
3
What is the output of the following code? double[] myList = {1, 5, 5, 5, 5, 1}; double max = myList[0]; int indexOfMax = 0; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) { max = myList[i]; indexOfMax = i; }}
1
Which of the following is correct:
int a[] = new int[2]
Assume int[] r = {{1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}}. What is r[1].length?
4
The following loop displays: public class PrintStarsWithWhile { public static void main(String[] args) { int numOfStars = 5; int i = 0; while (i < numOfStars) { System.out.print(“*”); i++; } }}
5 Astericks
Which of the following loops prints “Welcome to Java” 10 times?
for (int count = 0; count < 10; count++) { System.out.println(“Welcome to Java”);}