0% found this document useful (0 votes)
236 views

Quizz Solution

This document contains the solutions to a quiz for a CSE 142 class. It includes 18 exercises testing skills with Java syntax, expressions, variables, conditionals, loops, and methods. For each exercise, the document provides the code snippets or method calls being tested and the expected output.

Uploaded by

Phuc SJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
236 views

Quizz Solution

This document contains the solutions to a quiz for a CSE 142 class. It includes 18 exercises testing skills with Java syntax, expressions, variables, conditionals, loops, and methods. For each exercise, the document provides the code snippets or method calls being tested and the expected output.

Uploaded by

Phuc SJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CSE 142 - QUIZZ

le:///home/marc/danang/cse142/exam/quizz/quizz-solu...

ECE Advanced Program, CSE 142 QUIZZ


April 18, 2011 SOLUTIONS
Quizz instructions Quizz duration is 40 minutes. Quizz is closed-book, closed-notes, closed-neighbors. No electronic devices may be used, including calculators. Exercise 1: Syntax errors The following program contains 10 errors! What are they (just underline the errors and write the correct code on the same line as the error)?
public class Tricky { public static void main(String[] args) { System.out.println("Hello world"); System.out.println("Do you like this program?"); System.out.println(); System.out.println("I wrote it myself."); } }

Exercise 2: Expressions Write the results of each of the following expressions. 12 / 5 + 8 / 4 3 * 4 + 15 / 2


4 19

-(1 + 2 * 3 + (1 + 2) * 3) -16 42 % 5 + 16 % 3
3

1 of 10

05/05/2011 10:27 PM

CSE 142 - QUIZZ

le:///home/marc/danang/cse142/exam/quizz/quizz-solu...

Exercise 3: More Expressions Write the results of each of the following expressions. 5 * 6 / 4 % 3 - 23 / (14 % 6) 30 % 9 + 5 % 8 - 11 % 4 % 2 1 + 9 / 2 * 2.0 2.5 * 2 + 17 / 4 4.5 / 3 / 2 + 1 46 / 3 / 2.0 / 3 * 4/5
-10 7 9.0 9.0 1.75 2.0

50 / 9 / 2.0 + 200 / 10 / (5.0 / 2) 10.5 Exercise 4: Values What are the values of a , b , and c after the following statements? Write your answers in the boxes on the right.
int a int b int c a++; b--; c = c = 5; = 10; = b; // a? 6 // b? 9 // c? 16

+ a;

Exercise 5: More values What are the values of i , j , and k after the following statements? Write your answers on the right.
int int int int i = j = k = i j k x x x x = = = = 2; 3; 4; i + i j i -

j + k; j; k; k;

// i? 4 // j? 2 // k? 1

Describe in a few lines what is the code really doing:

2 of 10

05/05/2011 10:27 PM

CSE 142 - QUIZZ

le:///home/marc/danang/cse142/exam/quizz/quizz-solu...

Exercise 6: What's the output? Write the output produced by the following Java program:
public class OddStuff { public static void main(String[] args) { int number = 32; for (int count = 1; count <= number; count++) { System.out.println(number); number = number / 2; } } }

32 16 8 4 Exercise 7: simple for loop Complete the following code:


public class Count { private static int factor = 2; private static int limit = 10; public static void main(String[] args) { for ( int i = 1; i <= limit; i++ ) { System.out.println( factor + " times " + i + " = " + factor*i ); } } }

to produce the following output:


2 2 2 2 2 2 2 2 2 2 times times times times times times times times times times 1 = 2 2 = 4 3 = 6 4 = 8 5 = 10 6 = 12 7 = 14 8 = 16 9 = 18 10 = 20

3 of 10

05/05/2011 10:27 PM

CSE 142 - QUIZZ

le:///home/marc/danang/cse142/exam/quizz/quizz-solu...

Exercise 8: More for loop Complete the following code:


public class Triangle { private static int max = 4; public static void main(String[] args) { for (int line = 1; line <= max; line++) { for (int stars = 1; stars <= 2*(max - line + 1) - 1 ; stars++) { System.out.print("*"); } System.out.println(); } } }

so that it produces the following output:


******* ***** *** *

Exercise 9: What's the output? Write the output produced by the following Java method:
public static void figure() { int max = 4; for (int i = 1; i <= max; i++) { for (int j = 1; j <= i; j++) { System.out.print("X"); } for (int j = 1; j <= 2 * max - 2 * i; j++) { System.out.print("-"); } for (int j = 1; j <= i; j++) { System.out.print("X"); } System.out.println(); } }

X------X XX----XX XXX--XXX XXXXXXXX

4 of 10

05/05/2011 10:27 PM

CSE 142 - QUIZZ

le:///home/marc/danang/cse142/exam/quizz/quizz-solu...

Exercise 10: Parameter Mystery Fill in the boxes with the output that each method call would produce:
public class MysterySoda { public static void main(String[] args) { String soda = "coke"; String pop = "pepsi"; String coke = "pop"; String pepsi = "soda"; String say = pop; carbonated(coke, soda, carbonated(pop, pepsi, carbonated("pop", pop, carbonated(say, "say", } public static void carbonated(String coke, String soda, String pop) { System.out.println("say " + soda + " not " + pop + " or " + coke); } } pop); pepsi); "koolaid"); pop); // // // // say say say say coke not pepsi or pop soda not soda or pepsi pepsi not koolaid or pop say not pepsi or pepsi

Exercise 11: More parameter Mystery Fill in the boxes with the output that each method call would produce:
public class MysteryNumbers { public static void main(String[] args) { String one = "two"; String two = "three"; String three = "1"; int number = 20; sentence(one, two, 3); sentence(two, three, 14); sentence(three, three, number + 1); sentence(three, two, 1); sentence("eight", three, number / 2); } public static void sentence(String three, String one, int number) { System.out.println(one + " times " + three + " = " + (number * 2)); } } // // // // // three times two 1 times three = 1 times 1 = 42 three times 1 = 1 times eight = = 6 28 2 20

5 of 10

05/05/2011 10:27 PM

CSE 142 - QUIZZ

le:///home/marc/danang/cse142/exam/quizz/quizz-solu...

Exercise 12: if /else mystery Consider the following Java code. Fill in the boxes with the output produced by each of the method calls.
public static void mystery(int n) { System.out.print(n + " "); if (n > 10) { n = n / 2; } else { n = n + 7; } if (n * 2 < 25) { n = n + 10; } System.out.println(n); }

mystery(40); 40 20 mystery(8); 8 15 mystery(0); 0 17 mystery(12); 12 16 mystery(20); 20 20 Exercise 13: More if /else mystery Consider the following Java code. Fill in the boxes with the output produced by each of the method calls.
public static void mystery2(int a, int b) { if (a < b) { a = a * 2; } if (a > b) { a = a - 10; } else { b++; } System.out.println(a + " " + b); }

mystery2(10, 3); 0 3 mystery2(6, 6); 6 7 mystery2(3, 4); -4 4

6 of 10

05/05/2011 10:27 PM

CSE 142 - QUIZZ

le:///home/marc/danang/cse142/exam/quizz/quizz-solu...

mystery2(4, 20); 8 21

7 of 10

05/05/2011 10:27 PM

CSE 142 - QUIZZ

le:///home/marc/danang/cse142/exam/quizz/quizz-solu...

Exercise 14: Even More if /else mystery Consider the following Java code.
public static void mystery3(int x, int y) { int z = 4; if (z <= x) { z = x + 1; } else { z = z + 9; } if (z <= y) { y++; } System.out.println(z + " " + y); }

Fill in the boxes with the output produced by each of the method calls. mystery3(3, 20); 13 21 mystery3(4, 5); 5 6 mystery3(5, 5); 6 5 mystery3(6, 10); 7 11 Exercise 15: while loop basics Consider the following loop.
int x = 1; System.out.print(x); while (x < 100) { x = x + x; System.out.println(", " + x); }

How many times does the code in the body while loop above execute? 7 What output is produced? 1, 2, 4, 8, 16, 32, 64, 128

8 of 10

05/05/2011 10:27 PM

CSE 142 - QUIZZ

le:///home/marc/danang/cse142/exam/quizz/quizz-solu...

Exercise 16: while loop mystery Fill in the boxes below with the output produced by each method call.
public static void mystery(int x, int y) { int z = 0; while (x % y != 0) { x = x / y; z++; System.out.print(x + ", "); } System.out.println(z); }

mystery(25, 2); mystery(32, 4);

12, 1 0

mystery(10345, 10); 1034, 103, 10, 3


31, 15, 7, 3, 1, 0, 6

mystery(63, 2);

Exercise 17: More while loop mystery Fill in the boxes below with the output produced by each method call.
public static void mystery2(int x) { int y = 1; int z = 0; while (2 * y <= x) { y = y * 2; z++; } System.out.println(y + " " + z); }

mystery2(1); 1 0 mystery2(6); 4 2 mystery2(19); 16 4 mystery2(39); 32 5 mystery2(74); 64 6

9 of 10

05/05/2011 10:27 PM

CSE 142 - QUIZZ

le:///home/marc/danang/cse142/exam/quizz/quizz-solu...

Exercise 18: Even more while loop mystery Fill in the boxes below with the output produced by each method call.
public static void mystery3(int x) { int y = 0; while (x % 2 == 0) { y++; x = x / 2; } System.out.println(x + " " + y); }

mystery3(19); 19 0 mystery3(42); 21 1 mystery3(48); 3 4 mystery3(40); 5 3 mystery3(64); 1 6

10 of 10

05/05/2011 10:27 PM

You might also like