Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Take home quiz for Java (CS 55). Explanations would be great. Thanks 1) To assig

ID: 3564864 • Letter: T

Question

Take home quiz for Java (CS 55). Explanations would be great. Thanks

1) To assign a double variable d to a float variable x, you write

A) x = d; B) x = (float)d; C) x = (int)d; D) x = (long)d

2) The Unicode of 'a' is 97. What is the Unicode for 'c'?

A) 99 B) 98 C) 96 D) 97


3) Analyze the following code.
public class Test {
public static void main(String[ ] args) {
int month = 09;
System.out.println("month is " + month);
}
}
A) The program displays month is 9
B) The program displays month is 9.0
C) The program displays month is 09
D) The program has a syntax error, because 09 is an incorrect literal value.

4) What is i printed?
public class Test {
public static void main(String[ ] args) {
int j = 0;
int i = ++j + j * 5;
System.out.println("What is i? " + i);
}
}
A) 5 B) 1 C) 6 D) 0

5) -24 % 5 is ________.
A) -1 B) -2 C) -3 D) -4 E) 0

6) Which of the following expression results in a value 1?
A) 25 % 5 B) 15 % 4 C) 2 % 1 D) 37 % 6

7) The following code displays ________.
double temperature = 50;
if (temperature >= 100)
System.out.println("too hot");
else if (temperature <= 40)
System.out.println("too cold");
else
System.out.println("just right");
A) just right B) too cold
C) too hot too cold just right D) too hot

8) Suppose x = 1, y = -1, and z = 1. What is the printout of the following statement?
if (x > 0)
if (y > 0)
System.out.println("x > 0 and y > 0");
else if (z > 0)
System.out.println("x < 0 and z > 0");
A) x < 0 and z < 0; B) x < 0 and z > 0; C) x > 0 and y > 0; D) no printout.

9) What is the output of the following code?
char ch = 'F';
if (ch >= 'A' && ch <= 'Z')
System.out.println(ch);
A) nothing B) f C) F f D) F

10) Analyze the following code fragments that assign a boolean value to the variable even.
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) All three are correct, but Code 2 is preferred.
B) Code 2 has a compile 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 3 is preferred.
E) Code 3 has a compile error, because you attempt to assign number to even.

11) The order of the precedence (from high to low) of the operators +, *, &&, ||, & is:
A) *, +, &, ||, &&
B) *, +, &, &&, ||
C) *, +, &&, ||, &
D) &&, ||, &, *, +
E) &, ||, &&, *, +

12) How many times will the following code print "Welcome to Java"?
int count = 0;
do {
System.out.println("Welcome to Java");
} while (count++ < 10);
A) 9 B) 0 C) 11 D) 8 E) 10

13) How many times will the following code print "Welcome to Java"?
int count = 0;
while (count < 10) {
System.out.println("Welcome to Java");
count++;
}
A) 0 B) 8 C) 9 D) 11 E) 10

14) After the continue outer statement is executed in the following loop, which statement is executed?
outer:
for (int i = 1; i < 10; i++) {
inner:
for (int j = 1; j < 10; j++) {
if (i * j > 50)
continue outer;
System.out.println(i * j);
}
}
next:
A) The statement labeled next.
B) The control is in the inner loop, and the next iteration of the inner loop is executed.
C) The program terminates.
D) The control is in the outer loop, and the next iteration of the outer loop is executed.

15) What is the number of iterations in the following loop:
for (int i = 1; i < n; i++) {
// iteration
}
A) n B) 2*n C) n - 1 D) n + 1

16) Does the return statement in the following method cause compile errors?
public static void main(String[ ] args) {
int max = 0;
if (max != 0)
System.out.println(max);
else
return;
}
A) Yes B) No

17) Each time a method is invoked, the system stores parameters and local variables in an area of
memory, known as ________, which stores elements in last-in first-out fashion.
A) storage area B) an array C) a stack D) a heap

18) Arguments to methods always appear within ________.
A) parentheses B) curly braces
C) brackets D) quotation marks

19) ________ is a simple but incomplete version of a method.
A) A main method
B) A method developed using top-down approach
C) A non-main method
D) A stub

20) In the following code, what is the printout for list1?
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;
for (int i = 0; i < list1.length; i++)
System.out.print(list1[i] + " ");
}
}
A) 0 1 2 B) 1 2 3 C) 0 1 3 D) 1 1 1

21) If you declare an array double[ ] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________.
A) 3.4 B) 5.5 C) 3.4 D) 2.0 E) undefined

22) Analyze the following code:
public class Test {
public static void main(String[ ] args) {
int[ ] oldList = {1, 2, 3, 4, 5};
reverse(oldList);
for (int i = 0; i < oldList.length; i++)
System.out.print(oldList[i] + " ");
}
public static void reverse(int[ ] list) {
int[ ] newList = new int[list.length];
for (int i = 0; i < list.length; i++)
newList[i] = list[list.length - 1 - i];
list = newList;
}
}
A) The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.
B) The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException.
C) The program displays 1 2 3 4 5.
D) The program displays 5 4 3 2 1.

23) The following program displays ________.
public class Test {
public static void main(String[ ] args) {
String s = "Java";
StringBuilder buffer = new StringBuilder(s);
change(s);
System.out.println(s);
}
private static void change(String s) {
s = s + " and HTML";
}
}
A) and HTML B) Java and HTML
C) Java D) nothing is displayed

24) What is displayed by the following statement?
System.out.println("Java is neat".replaceAll("is", "AAA"));
A) JavaAAAneat B) JavaAAA neat C) Java AAAneat D) Java AAA neat

25) Suppose you enter 34.3 57.8 789, then press the ENTER key. Analyze the following code.
Scanner scanner = new Scanner(System.in);
int intValue = scanner.nextInt();
int doubleValue = scanner.nextInt();
String line = scanner.nextLine();
A) After the last statement is executed, line contains characters '7', '8', '9', ' '.
B) The program has a runtime error because 34.3 is not an integer.
C) After the last statement is executed, intValue is 34.
D) After the last statement is executed, line contains characters '7', '8', '9'.

26) According to Java naming convention, which of the following names can be variables? (Choose all
that apply.)
A) class
B) TOTAL_LENGTH
C) totalLength
D) FindArea
E) findArea

27) If a program compiles fine, but it produces incorrect result, then the program suffers ________.
A) a runtime error B) a logic error C) a compilation error

28) Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x-- > 10)?
A) 9 B) 11 C) 10

29) The statement System.out.printf("%10s", 123456) outputs ________. (Note: * represents a space)
A) 12345***** B) ****123456 C) 23456***** D) 123456****

30) What is i after the following for loop?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}
A) 11 B) 9 C) 10 D) undefined

31) What is Math.rint(3.6)?
A) 3.0 B) 5.0 C) 4.0 D) 3

32) The ________ method sorts the array scores of the double[ ] type.
A) Njava.util.Arrays.sortArray(scores) B) java.util.Arrays.sorts(scores)
C) java.util.Arrays.sort(scores) D) java.util.Arrays(scores)

33) Which of the following is the correct statement to return a string from an array a of characters?
A) new String(a) B) toString(a)
C) String.toString(a) D) convertToString(a)

34) Which of the following are correct ways to declare variables? (Choose all that apply.)
A) int length, int width; B) int length, width;
C) int length; int width; D) int length; width;

35) Which of the following operators are right-associative?
A) && B) + C) % D) = E) *

36) How many times will the following code print "Welcome to Java"?
int count = 0;
while (count++ < 10) {
System.out.println("Welcome to Java");
}
A) 8 B) 11 C) 9 D) 0 E) 10

37) What is Math.rint(3.5)?
A) 5.0 B) 3.0 C) 3 D) 4 E) 4.0

38) What is the representation of the third element in an array called a?
A) a[3] B) a[2] C) a(2) D) a(3)

39) The StringBuilder methods ________ not only change the contents of a string buffer, but also
returns a reference to the string buffer. (Choose all that apply.)
A) delete B) insert C) append D) replace E) reverse

40) Which of the following assignment statements is incorrect? (Choose all that apply.)
A) i = j = k = 1; B) i == j == k == 1;
C) i = 1; j = 1; k = 1; D) i = 1 = j = 1 = k = 1;

Explanation / Answer

1)

Declare d as double and x as float.

x = (float)d; will assign the value of d to variable x.

x=d will not assign as it is not possible to convert double to float.

Hence, the correct option is B.

2)

The Unicode of 'a' is 97.

The Unicode of 'b' is 98.

The Unicode of 'c' is 99.

Hence, the correct option is A.

3)

            The variable month is initialized with a value 09 which is an incorrect literal value.

So when the program is executed, it will show an error message “The literal 09 of type int is out of range”.

Hence, the correct option is D.

4)

            The output of the program will be 6.

Initially j=0. In the statement int i = ++j + j * 5; ++j will increment the value of j and j=1. j*5 =1*5 = 5. So 1+5 will be 6 and 6 will be assigned to i.

Hence, the correct option is C.

5)       

            X%Y will give the remainder after dividing X/Y.

            -24%5 will give the remainder as -4.

Hence, the correct option is D.

6)

            X%Y will give the remainder after dividing X/Y.

            25%5 will give the remainder as 0.

            15%4 will give the remainder as 3.

            2%1 will give the remainder as 0.

            37%6 will give the remainder as 1.

Hence, the correct option is D.

7)

The code gives the output “just right”.

The variable temperature is initialized to 50.

First it is checked whether temperature >= 100. As the condition becomes false, the condition temperature <= 40 is checked. As this condition is also false, the statement following the else part is printed on the screen.

Hence, the correct option is A.

8)

The code gives the output x < 0 and z > 0.

            The variables are initialized as x = 1, y = -1, and z = 1.

First the condition x > 0 is checked. As it is true, the next condition y>0 is checked. As it is false, the statement following the else part System.out.println("x < 0 and z > 0"); is executed and x < 0 and z > 0 is printed on the screen.

Hence, the correct option is B.

9)

The code gives the output F.

The variable ch is initialized to 'F';

As F > A and F< Z, the condition (ch >= 'A' && ch <= 'Z') becomes true and the statement following the if part System.out.println(ch); is executed F is printed on the screen.

Hence, the correct option is D.

10)

All the three codes are correct. They will assign a boolean value to the variable even. There will not be any compile error.

Code 2: even = (number % 2 == 0) ? true: false; is preferred as it uses ternary operator.

Hence, the correct option is A.

11)

The order of the precedence (from high to low) of the operators +, *, &&, ||, & is:

*,+,&, &&,||.

*--->multiplication

+-->addition

&-->Bitwise AND

&&-->Logical AND

||-->Logical OR

Hence, the correct option is B.

12)

The given code print "Welcome to Java" 11 times.

Count++ will assign the value to count and then increment it. So initially count will be 0. The statement System.out.println("Welcome to Java"); will be executed and then condition checking happens. So the statement System.out.println("Welcome to Java"); will execute until count becomes 11.

Hence, the correct option is C.

13)

The given code print "Welcome to Java" 10 times.

Count++ will assign the value to count and then increment it. So initially count will be 0. First condition checking happens and then if the condition is true then t he statement System.out.println("Welcome to Java"); will be executed.

So the statement System.out.println("Welcome to Java"); will execute until count becomes 10.

Hence, the correct option is C.

14)

The control is in the outer loop, and the next iteration of the outer loop is executed.

Hence, the correct option is D.

15)

The loop will execute until the value of i is less than n. In other words, the loop will execute till i is (n-1). If the value of n is 10, then the loop will execute for 9 times as i is initialized to 1.

Hence, the correct option is C.

16)

The return statement in the given code does not cause any compilation error. If max=0, then no output is displayed. If max!=0, then the value of max is printed.

Hence, the correct option is B.

17)

Stack is a data structure in which the elements are stored in last in first out fashion.

Each time a method is invoked, the system stores parameters and local variables in an area of memory, known as stack, which stores elements in last-in first-out fashion.

Hence, the correct option is C.

18)

Arguments to methods always appear within brackets ().

Hence, the correct option is C.

19)

A stub is a small method which is usually incomplete.

Hence, the correct option is D.

20)

The given code outputs 0 1 2.

Initailly list1 is initialized with values 1 2 3.

The statements list1[0] = 0; list1[1] = 1; list2[2] = 2; will change the values of list1 to 0 1 2.

So list1 will contain the values 0 1 2 and these are printed.

Hence, the correct option is A.

21)

        Array indexing starts at 0.

double[ ] list = {3.4, 2.0, 3.5, 5.5} will initialize the list with values 3.4, 2.0,

3.5 and 5.5.

3.4 is stored at list[0].

2.0 is stored at list[1].

3.5is stored at list[2].

5.5 is stored at list[3].

Hence, the correct option is D.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote