Cpsc112A Asg2 Pratibha 10134087
Cpsc112A Asg2 Pratibha 10134087
Winter 2024
Submission: Submit your assignment to the instructor by email through Canvas Inbox
Do not wait until the last minute to do this assignment in case you run
into problems.
For programming questions, you may assume the user’s inputs are well-formatted.
A. Multiple Choice Questions [20 points, 2 points each]
1. Consider the following statements in Java.
int n = 0;
for (n = 1; n <= 20; n+=2)
{
n--;
}
What will be the value of ''n'' after these statements?
a. 19 b. 20 c. 21 d. 22
a. int
b. long
c. byte
d. double
int count = 0;
do {
System.out.println("Welcome to Java");
} while (count++ < 9);
System.out.println(count);
a. 11
b. 0
c. 9
d. 8
e. 10
Code 1:
boolean even;
if (number % 2 == 0)
even = true;
else
even = false;
Code 2:
boolean even = (number % 2 == 0);
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}
a. 9 b. 10 c. undefined d. 11
a.20,1 b.10,1
c. 20, 20 d. None of the above
a. 0,10
b. 0,310
c. 0,410
d. None of the above
class Test {
public static void main(String[] args) {
int[] list1 = {1, 2, 3};
int[] list2 = {1, 2, 3};
list2 = list1;
list1[0] = 0; list1[1] = 1; list2[2] = 2;
a. 1 2 3 b. 1 1 1 c. 0 1 2 d. 0 1 3
Code 1:
if (number % 2 == 0)
even = true;
else
even = false;
Code 2:
even = (number % 2 == 0) ? true: false;
Code 3:
even = number % 2 == 0;
a. Code 3 has a syntax error, because you attempt to assign number to even.
b. Code 2 has a syntax error, because you cannot have true and false literals in the conditional
expression.
c. All three are correct, but Code 1 is preferred.
d. All three are correct, but Code 2 is preferred.
e. All three are correct, but Code 3 is preferred.
1
12
123
...
...
1 2 3 4 5 ... n
import java.util.Scanner;
displayPattern(n);
}
is the following:
Your class Transpose should include the following method to do the transposition.
public static void transpose(int[][] arr)
Also, write a main() method in your Transpose class to test the method above. You need
initialize an array randomly and call the method.
Solution c)