CPS180 Final Exam

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is the return value of "SELECT".substring(0, 5)?

"SELEC"

Which of the Boolean expressions below has incorrect syntax?

(x != 0) || (x = 0)

What is y? int x = 0; int y = 0; switch (x+1){ case 0: y = 0; case 1: y = 1; default: y = -1; }

-1

What is the output from System.out.println((int)Math.random() * 4)?

0

public static void main(String[]args) { int[]x={0,1,2,3,4,5}; xMethod(x,5); } public static void xMethod(int[]x,int length) { for(int i=0; i<length; i++) System.out.print(" "+x[i]); }

0 1 2 3 4

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; } } System.out.println(indexOfMax);

1

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

1 1 1 1 1 1

public static void main(String[]args) { int[]myList = {1,2,3,4,5,6}; for(int i = myList.length-2;i>=0; i--) myList[i+1]=myList[i]; for(int e: myList) System.out.print(e + " "); }

1 1 2 3 4 5

for(int i = 0; i<15; i++){ if(i % 4 == 1) System.out.print(i + " ");

1 5 9 13

int[] x = {120, 200, 016}; for(int i = 0; i<x.length; i++) System.out.print(x[i] + " ");

120 200 14

int i = 1; int j = 1; while(i<5){ i++; j = j*2; } System.out.println(j);

16

Assume that the ASCII code for character c is 99 and for a is 97. What is the printout of the following code? System.out.println('a' + 'c');

196

int x = 3; int y = 4; switch(x+3){ case 6: y=0; case 7: y=1; default: y +=1; }

2

int[]list1 = {3,2,1}; int[]list2 = {1,2,3}; list2=list1; list1[0]=0; list1[1]=1; list2[2]=2; for(int i=list2.length-1; i>=0; i--) System.out.print(list2[i] + " ");

2 1 0

int[]myList = {1,2,3,4,5,6}; for(int i=1; i<myList.length; i++) myList[i-1]=myList[i]; for(int e: myList) System.out.print(e + " ");

2 3 4 5 6 6

If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is

2.0

What is x after evaluating the following? x = (2 > 3) ? 2 : 3;

3

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; } } System.out.println(indexOfMax);

4

public static void main(String[] args) { int [][]values = {{3,4,5,1}, {33,6,1,2}}; for(int row = 0; row<values.length; row++) { System.out.print(m(values[row]) + " "); } } public static int m(int[]list) { int v = list[0]; for(int i = 1; i<list.length; i++) if(v<list[i]) v=list[i]; return v; }

5 33

int sum = 0; int item = 0; do{ item++; sum +=item; if(sum>4)break; }while (item<5); System.out.println(sum);

6

The expression (int)(76.0252175 * 100) / 100 evaluates to

76

int[][]matrix = new int[5][5]; for(int column = 0; column<matrix[4].length; column++); matrix[4][column] = 10;

A syntax error, because column is not defined.

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;

All three are correct, but Code 2 is preferred. WRONG!

//Code 1: int number = 45; boolean even; if(number % 2 == 0) even = true; else even = false; //Code 2: boolean even = (number % 2 == 0);

Both Code 1 and Code 2 are correct, but Code 2 is better.

//Enter an integer Scanner input = new Scanner(System.in); int number = input.nextInt(); if(number <= 0) System.out.println(number);

If number is zero, number is displayed.

Which of the following is a constant, according to Java naming conventions?

MAX_VALUE

boolean even = false; if(even){ System.out.println("It is even!");

The code displays nothing.

int x = 1; while(0<x) && (x<100) System.out.println(x++);

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

int sum = 0; for(int i = 0; i< 100000; i++){ Scanner input = new Scanner(System.in); sum += input.nextInt(); }

The program compiles and runs, but it is not efficient and unnecessary to execute the Scanner input = new Scanner(System.in); statement inside the loop. You should move the statement before the loop.

public class Exam1 { 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; } }

The program displays 1 2 3 4 5.

int []a = new int[4]; a[1] = 1; a = new int[2]; System.out.println("a[1] is " + a[1]);

The program displays a[1] is 0.

public static void main(String[]args) { xMethod(new double[]{3,3}); xMethod(new double[5]); xMethod(new double[3]{1,2,3}); } public static void xMethod(double[]a) { System.out.println(a.length); }

The program has a compile error because xMethod(new double[3]{1, 2, 3}) is incorrect.

public static void main(String[]args) { int[]x=new int[5]; for(int i=0; i<x.length;i++) x[i]=i; System.out.println(x[i]); }

The program has a syntax error because i is not defined in the last statement in the main method.

int month = 09; System.out.println("month is " +month);

The program has a syntax error, because 09 is an incorrect literal value.

boolean[][]x=new boolean [3][]; x[0] = new boolean[1]; x[1] = new boolean [2]; x[2] = new boolean [3]; System.out.println("x[2][2] is " + x[2][2]);

The program runs and displays x[2][2] is false.

boolean even = false; if(even = true){ System.out.println("It is even!");

The program runs fine, but displays nothing. WRONG!

int x; double d = 1.5; switch(d){ case 1.0: x=1; case 1.5: x=2; case 2.0: x=3;

The switch control variable cannot be double.

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

a[0][0]

What is the exact output of the following code? double area = 3.5; System.out.print("area"); System.out.print(area);

area3.5

boolean even = false; System.out.println((even ? "true" : "false"));

false

int number = 25; int i; boolean isPrime = true; for(i = 2; i< number; i++){ if(number %i == 0){ isPrime = false; break; } } System.out.println("i is " +i+ " isPrime is " +isPrime);

i is 5 isPrime is false

int number = 25; int i; boolean isPrime = true; for(i=2; i<number && isPrime; i++){ if(number%i ==0){ isPrime = false; } } System.out.print(i); System.out.print(isPrime);

i is 6 isPrime is false

for( ; ; ) System.out.println("Welcome to Java");

prints out Welcome to Java forever

What is the value of the following expression? true || true && false

true

What is the printout of the following code: double x = 10.1; int y = (int)x; System.out.println("x is " + x + " and y is " + y);

x is 10.1 and y is 10

int x = 1; int y = x+ x++; System.out.println("y is " +y);

y is 2

int x = 1; int y = x = x+1; System.out.println("y is " +y);

y is 2 because x + 1 is assigned to x and then x is assigned to y.

Do the following two programs produce the same result? Program I: public class Test { public static void main(String[ ] args) { int[ ] list = {1, 2, 3, 4, 5}; reverse(list); for (int i = 0; i < list.length; i++) System.out.print(list[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; } } Program II: 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; } }

yes


Ensembles d'études connexes

Pharm Ch 27 General and Local Anesthetic Agents

View Set

Research Methods Chapter 14 review

View Set

ITP 100 Software Development Book/Quiz 1,2,3,4

View Set

BIOL 1030 Chapter 11 Homework Questions

View Set

Unit 5: Baking and Pastry Applications

View Set

Pharm Block 4 - extra practice questions

View Set

Chem Y8i - How many atoms are there all together in a single group of the following chemicals?

View Set

Chemistry Chapter 14 - Acids and Bases

View Set