APCS Test 1

Ace your homework & exams now with Quizwiz!

k ++

Take k, add one

2 + 3 * 12 / 7 -4 +8

5.45 Round to 5 *Addition/sub have a higher precedence than division/multiplication*

x ++

Add 1 to whatever x is

What is stored in result if int result = 13 -3 * 6 / 4 % 3; a) -5 b) 0 c) 13 d) -1 e) 12

Answer --> E 13 -3 * 6 / 4 % 3 13 - 18 / 4 % 3 13 -4 % 3 13 - 1 12

What has a higher precedence? Doubles or Integers?

Doubles

Consider the following code segment: int a = 10; double b = 3.7; inc c = 4; int x = (int)(a+b); double y = (double) a/c; double z = (double)(a/c); double w = x + y + z; System.out.println(w); What is printed as a result of the code above? a) 10 b) 15 c) 15.5 d) 17 e) 17.5

E --> 17.5 int x line = 13 double y line = 2.5 double x line = 2.0 (a / c is separate from the double --> dividing int by an int, then making it a double won't work (Casting))

Consider the following static method: public static int calculate(int x) { x = x + x; x = x + x; x = x + x; return x } Which of the following can be used to replace the body of calculate so that the modified version of calculate will return the same result of the original version for all of x? a) return 3 +x; b) return 3 * x; c) return 4 * x; d) return 6 * x; e) return 8 * x;

E- return 8 * x; Work= store 2 as x 2 = 2 + 2 4 = 4 + 4 8 = 8 + 8 16 return 8 * 2 = 16 w/o numbers x = x+ x 2x = 2x + 2x 4x = 4x + 4x 8x return 8 * x = 8x

Given the following declarations: int p = 5, q = 3; Which of the following expressions evaluate to 7.5? 1. (double) p *(double) q / 2; 2. (double) p * (double)(q/20; 3. (double)(p*q/2); a) 1 only b) 2 only c) 1 and 2 d) 1 2 and 3 e) None of the above

a) 1 only 5.0 * (double)q/2; 5.0 * 1.5 7.5

Look at the following program segment if a = 7 and b= 6 before execution, which of the following represents the correct values of c,d,p, and t after the execution? An undetermined value is represented with a question mark. if(a==6){ if(c==6) {c=9; d=9;} else{t=10;) t=10; if(c==6) c=5; else p = 9; a) c=6 d=? p=9 t=? b) c=5 d=? p=? t= 10 c) c=6 d=? p=? t=? d) c=5 d=9 p=? t=10 e) c=9 d=9 p=? t=?

a) c=6 , d= ?, p=9, t=?

What values are stored in x and y after the execution of the following segment? int x = 30, y = 40; if(x<=0) { y = x*3; if(y<50) x/10:} else y = x*2;} else y= -x a) x = 30 y= 90 b) x = 30 y = -30 c) x= 30 y = 60 d) x = 3 y= -3 e) x = 30 y = 40

b) x= 30, y=-30 ignore everything in first condition (30 <= 0 is false) only look at y = -30

Consider the following code segment: int k = 1; while(k<20) { if ((k%3)==1) System.out.print(k+" "); k++; } a) 2 5 8 11 14 17 b) 3 6 9 12 15 18 c) 1 4 7 10 13 16 19 d) 1 3 5 7 9 11 13 15 17 19 e) 2 4 6 8 1- 12 14 16 18 20

c) 1 4 7 10 13 16 19

Consider the following code segment: int x = 10; int y = x/3; int z = x % 2; x ++; System.out.print(x); What is printed as a result of executing the code segment above? a) 2 b) 4 c) 10 d) 11 e) 1.5

d) 11 two middle lines are irrelevant, only asking for x and nothing is reassigned int x = 10 10 ++ = 11

Consider the following code segment: int a = 11; int b = 4; double x = 11; double y = 4; System.out.print(a/b); System.out.print(" , "); System.out.print(x/y); System.out.print(" , ") System.out.print(a/y); What is printed as a result of the executing of the code segment? a) 3, 2.75, 3 b) 3, 2.75, 2,75 c) 2, 3 , 2 d) 2, 2.75, 2.75 e) Nothing will be printed because of a compile-time error

d) 2, 2.75, 2.75 a/b = 2 x/y = 2.75 a / y = 2.75 because doubles have the higher precedence

What will the output be for the following poorly formatted program segment if the input value for num is 22 > or equal to int num = call to a method that reads on integers if(num>0) if(num%5==0) System.out.print(num); else System.out.println(nu + " is negative); a) 22 b) 4 c) 2 is negative d) 22 is negative e) nothing will be printed

d) 22 is negative

What is the result of the following statement? int n = --3--1; system.out.println(n); a) 1 is displayed b) 2 is displayed c) 3 is displayed d) 4 is displayed e) syntax error

d) 4 is displayed (-(-3))-(-1) 3 + 1 4

What output will be produced by System.out.print(" // * This is not in a comment *//" a) *This is not a comment* b) * This is not a comment* c) /* This is not a comment*/ d) //*This is not a comment*// e) */This is not a comment*/

e) */This is not a comment*/

Consider the following statement: int i = x % 50; If x is a positive integer, which of the following could not be the value of i after the statement above executes? a) 0 b) 10 c) 25 d) 40 e) 50

e) 50 50 % 50 = 0 0 is neither positive or negative anything less than 50 will work

Consider the following code segment: int i = 1; int k = 1; while(i < 5){ k * = 1; k++;} System.out.print(k); a) 6 b) 10 c) 24 d) 120 e) Nothing will be printed

e) Nothing will be printed i = 1 k = 1 k2 = 1 k3 = 2 k4 = 2 k5 =3 k6 = 3 k7 = 4 repeated until 1 < 5 is met! infinite loop - i never gets passes 1 print line = outside loop

Refer to the following code segment double answer = 13 / 5; System.out.println("13/5 = " + answer); The output is 13/ 5 = 2.0....the programmer intended the output to be 13/5 = 2.6 Which of the following replacements for the first line of code will not fix the problem? a) double answer = (double) 13 / 5; b) double answer = 13 / (double) 5; c) double answer = 13.0 / 5; d) double answer = 13 / 5.0; e) double answer = (double)(13 / 5);

e) double answer = (double)(13 / 5);

Consider the following code segment: int a = 10; double b = 10.7; double c = a + b; int d = a + c; System.out.println(+ " " +d); What will be the output as a result of executing the code segment? a) 20 20 b) 20.0 20 c) 20.7 20 d) 20.7 21 e) Nothing will be printed because of a compile time error

e) nothing will be printed Line 4 -> int d = adding a double to an int won't work Cast before adding

k * = i;

whatever k is, multiply by i value, reassign answer to k


Related study sets

hesi next gen case studies for med surg

View Set

Intro to Business FINAL EXAM Ch.16-20 & Ch. C

View Set

Rad Protection Chapter 1 Workbook

View Set