1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt WHILE You were out FOR Crying out Loud! IF You insist What is your mal- FUNCTION Nothing to see here. Move Along
2 What is the output?? int x = 0; while(x<=7){ System.out.println(x); x++; }
4 int x = 10; while(x>0){ x-=2; System.out.println(x); } What is the output??
6 int x = 0, y=0; while(x<=100){ y+=x; x+=10; } System.out.println("x="+x); System.out.println("y="+y);
7 x=110 y=550
8 What is the output? int x = 1; while(x<=20){ if(x%2 != 0) System.out.print(x+" "); if(x%5==0) System.out.print("\n"); x++; }
10 Assume that the user has entered a value into a variable named exp. Write code that uses a while-loop to calculate 2 exp. (be sure to declare any other variables that you may use)
11 int exp=5; int count=0, ans=1; while(count<exp){ ans*=2; count++; } System.out.println(ans);
12 What is the output? for(int i=0; i<8; i++) System.out.println(i);
14 What is the output? for(int i=10; i>=0; i-=2){ System.out.println(i); }
16 What is the output? int x=5, ans=0; for(int i=x; i>0; i--){ ans += i*i; } System.out.println(ans);
17 55
18 What is the output? int x=3, y=5, ans=1; for(int i=y; i>0; i--){ ans*=x; } System.out.println(ans);
19 243
20 Assume the user has stored a value in a variable named var. Write a for-loop that calculates the factorial of var. Be sure to declare any other variables.
21 int fact=1; for(int i=1;i<=var;i++) fact*=i; System.out.println(fact);
22 What is the output? int a=92, f=65; int x=70; if(x>=a) System.out.println("You make A"); else if(x>=f) System.out.println("You pass"); else System.out.println("FAILURE");
23 You pass
24 What is the output? int x=2, y=3; int num=5; if(num%x==0) if(num%y==0) System.out.println(x*y+" divides "+num); else System.out.println(x +" divides "+num); else if(num%y==0) System.out.println(y +" divides "+num); else System.out.println("thanks for playing");
25 thanks for playing
26 What is the output? int x=2, y=3; int num=5; if(num>x) if(num<y) System.out.println("between"); else System.out.println("below"); System.out.println("thanks");
27 below thanks
28 What’s the output? int x=2, y=5,z=8; int num=19; if(x<=num) if(num<=y) System.out.println("[x,y]"); else if(num<=z) System.out.println("[y,z]"); else System.out.println("[z,i]"); else System.out.println("[-i,x]");
29 [z, i]
30 What’s the output? int x=3, y=14,z=28; int num=19; if(x<=num) if(num<=y) System.out.println("a"); else if(num<=z) System.out.println("b"); else System.out.println("c"); else System.out.println("d");
31 b
32 What is the output? public static void main(String[] args){ int num1=5, num2; num2 = sq(num1); System.out.println(num1); System.out.println(num2); } public static int sq(int x){ x*=x; return x; }
34 What are values of variables x, y, & z? public static void main(String[] args){ int x=5, y=10, z=15; x=fun(x,y); z=fun(y,x); } public static int fun(int a, int b){ a += b; b--; return(a-10); }
35 x=5 y=10 z=5
36 Values of x, y, & z? public static void main(String[] args){ int x=2, y=4, z=0; fun(x,y); z=fun(y,x); y=fun(z,x); } public static int fun(int n1, int n2){ int a=0; if(n1>n2) a=n1+(n1-n2); else a=n1-(n2-n1); n1*=1000; n2+=n1; return a; }
37 x=2 y=10 z=6
38 Values of x, y, & z? public static void main(String[] args) { int x=2, y=3,z; z=fun(x,y); y=fun(z,2); System.out.println("x="+x); System.out.println("y="+y); System.out.println("z="+z); } public static int fun(int n1, int n2){ int a=1; while(n2>0){ a*=n1; n2--; } return a; }
39 x=2 y=64 z=8
40 Write a function that accepts one parameter and returns the sum of all positive integers less than or equal to its value.
41 public static int sum(int x){ int sum=0; while(x>0){ sum+=x; x--; } return sum; }
42
43
44
45
46
47
48
49
50
51
52 FINAL JEOPARDY!! Write a function that calculates and returns the factorial of a given number.
53 public static int fact(int x){ int ans=1; for(int i=1; i<=x; i++) ans*=i; return ans; }