Sample Multiple Choice Question #1

Sample Multiple Choice Question #1 Consider the following program. public class DS0209 { public static void main(String args[]) { int x = 10; int y = ...
Author: Derrick Newton
27 downloads 0 Views 31KB Size
Sample Multiple Choice Question #1 Consider the following program. public class DS0209 { public static void main(String args[]) { int x = 10; int y = 20; System.out.println(x + " " + y); swap(x,y); System.out.println(x + " " + y); } public static void swap(int p, int q) { int temp = p; p = q; q = temp; } } What is printed as a result of executing the program?

(A) 10 10

20 20

(B) 10 20

20 10

(C) 10 20

10 20

(D) 20 20

20 20

(E) There is no output displayed.

Sample Multiple Choice Question #2 Consider the following method. public static double getMean(int list[]) // precondition: list is a non-empty array of integers. // postcondition: getMean returns the average (mean) of the integers in list.

{ int n = list.length; /* missing code */ } Implementation 1 int sum = 0; for (int item: list) sum += item; return (double) sum / n;

Implementation 2 int sum = 0; for (int item: list) sum += list[item]; return (double) sum / n; Implementation 3 int sum = 0; for each (int item: list) sum += item:list; return sum / n;

Which of the following code segments satisfies the postcondition of method getMean?

(A) (B) (C) (D) (E)

Implementation 1 only Implementation 2 only Implementation 3 only Implementations 1 and 2 only Implementations 2 and 3 only

Sample Multiple Choice Question #3 Consider the following method. public static void method0910(String s) { int n = s.length(); if (n > 0) { String temp = s.substring(0,n-1); method0910(temp); System.out.println(s); } } Which of the following is printed as a result of the call method0910("TANGO") ?

(A) O (B) TA TAN TANG TANGO ANGOT (C) TANGO ANGO NGO GO O (D) TA TAN TANG TANGO (E) T TA TAN TANG TANGO

Sample Multiple Choice Question #4 .

Consider the following program segment. for (int k = 1; k start // postcondition: returns a random integer x such that for all x, // start

Suggest Documents