Test 2 Flashcards

(22 cards)

1
Q

Analyze the following code fragment: int x = 1; while (0 < x) && (x < 100) System.out.println(x++); What is the issue?

A

The code does not compile because (0 < x) && (x < 100) is not enclosed in a pair of parentheses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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++; }}

A

2 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Given the following method, what is the printout of the call nPrint(‘a’, 4)?

A

Invalid call because char ‘a’ cannot be passed to string message.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which of the loop statements always have their body executed at least once?

A

The do-while loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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); }}

A

1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How many times will the following code print “Welcome to Java”? int count = 0; while (count++ < 10) { System.out.println(“Welcome to Java”);}

A

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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]); }}

A

The program runs fine and displays x[0] is 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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; }

A

1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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] + “ “); }}

A

1 1 1 1 1 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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);

A

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which of the following statements are correct?

A

char[][] charArray = {{‘a’, ‘b’}, {‘c’, ‘d’}};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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(); } }}

A

.
..

….
…..

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is y after the following for loop statement is executed? int y = 0; for (int i = 0; i < 10; ++i) { y += 1; }

A

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the index variable for the element at the first row and first column in array a?

A

a[0][0]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How many times will the following code print “Welcome to Java”? int count = 0; while (count < 9) { System.out.println(“Welcome to Java”); count++;}

A

9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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]); }}

A

The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException.

17
Q

If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is __________.

18
Q

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; }}

19
Q

Which of the following is correct:

A

int a[] = new int[2]

20
Q

Assume int[] r = {{1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}}. What is r[1].length?

21
Q

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++; } }}

22
Q

Which of the following loops prints “Welcome to Java” 10 times?

A

for (int count = 0; count < 10; count++) { System.out.println(“Welcome to Java”);}