Quiz #1

  • Score: 50/66

image

Corrections

1)
The given method increases each element of the array numbers by 1. Code segment I does not work as intended. It assigns num a copy of each element of numbers. When num is incremented, it does not change the corresponding value stored in numbers.

2)
The Boolean expression in the for loop header should be changed to i < animals.length.

7)
MethodA() and al = 0 will cause a compile-time error

18)
In order to apply De Morgan’s laws and negate the condition in the if statement in the given code segment, the logical operator should be changed from || to &&. As an example, if a, b, c, and d have the values 1, 2, 2, and 2, respectively, then the given code segment would print "dog" and this code segment would print "cat".

27)
For this list, the method correctly removes the elements that were originally at index 1 and at index 5.

29)
When the value of num is 5, the code segment will print 5, as intended. This occurs when j is 1 and k is 3.

32)
The program works when you insert the statement lenCount = 0 between lines 12 and 13

34)
The original code segment prints the sum of twice the value of the elements in the array. This code segment prints a value that is twice the sum of the integers from 0 to arr.length - 1.

37)
The statement assigns a different value to b2 than the code segment assigns to b1 when num is between 0 and 100, exclusive.

38)
Statement 2 only works the same as the example

39)
The value 2000 is a multiple of 4, so the expression (val % 4) == 0 evaluates to true and the method returns true, as intended. The value 2000 is also a multiple of 100 and 400, so the return value is as intended even though the test for being a multiple of 400 is never made.

55)
!(a < b) && !(a > b) is equal to a == b

56)
The best way to fix the error is changing the while condition to be (k < myData.size() - 1)

62)
When x is 8, y will be assigned the value 3, since 8 is even. When x is 9, y will be assigned the value 1, since 9 is odd and not greater than 9. When x is 12, y will be assigned the value 3, since 12 is even. This set of values does not test the conditions when x is odd and greater than 9 and y is assigned the value 5.

63)
The code segment is intended to leave arr1 unchanged. Elements of arr1 will be modified by line 5 regardless of what relational operator is used.

Quiz #2

Corrections

45)
This would be the result if line 12 were executed once for each element of arr.

Line 12 is executed each time the variable sm is updated because a new smallest value is found. When j has the value 0, sm is updated for "day" and "of". When j has the value 1, sm is updated for "of". When j has the value 4, sm is updated for "year". When j has any of the values 2, 3, or 5, sm is not updated. Line 12 is executed four times.

48)
Option II is correct. The code segment uses an enhanced for loop to traverse the valueList array. The statement inside the loop calls the getNum method to access the num instance variable.

Option I is correct. The code segment uses a for loop to traverse the valueList array. The statement inside the loop calls the get method to access a Value object and then calls the getNum method to access the num instance variable. Option II is correct. The code segment uses an enhanced for loop to traverse the valueList array. The statement inside the loop calls the getNum method to access the num instance variable. Option III is incorrect. The code segment causes a compilation error because the getNum method must be called using the dot operator, not by passing the object reference as an argument.

52)
This image would require the second set of nested loops to initialize row to val – 1, increment both row and col in each iteration inner loop (instead of row being decremented) and changing the condition on the inner loop to col < 5 && row < 5.

The first set of nested for loops sets each element in board to “O”. The next for loop starts val at 0 and increments by 1 until val is 4, when val is 5 the loop terminates. When val is even, board is not updated, so nothing happens when val is 0. When val is 1, row is assigned 1 and col is assigned 0. The boolean condition in the while loop is true, so board[1][0] is assigned “X”. Then col is incremented to 1 and row is decremented to 0 and board[0][1] is assigned “X”. Then col is incremented to 2 and row is decremented to -1 and the while loop terminates. When val is 2, nothing changes about board. When val is 3, row is assigned 3 and col is assigned 0. The boolean condition in the while loop is true, so board[3][0] is assigned “X”. Then col is incremented to 1 and row is decremented to 2 and board[2][1] is assigned “X”. Then col is incremented to 2 and row is decremented to 1 and board[1][2] is assigned “X”. Then col is incremented to 3 and row is decremented to 0 and board[0][3] is assigned “X”. Finally, col is incremented to 4 and row is decremented to -1 and the while loop terminates. When val is 4, nothing changes about board.