quizz solution

Upload: phuc-nguyen-thai-vinh

Post on 03-Jun-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Quizz Solution

    1/10

    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 voidmain(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 4

    3 * 4 + 15 / 2 19

    -(1 + 2 * 3 + (1 + 2) * 3) -16

    42 % 5 + 16 % 3 3

    SE 142 - QUIZZ file:///home/marc/danang/cse142/exam/quizz/quizz-solu...

    of 10 05/05/2011 10:27 PM

  • 8/12/2019 Quizz Solution

    2/10

    Exercise 3: More Expressions

    Write the results of each of the following expressions.

    5 * 6 / 4 % 3 - 23 / (14 % 6) -10

    30 % 9 + 5 % 8 - 11 % 4 % 2 7

    1 + 9 / 2 * 2.0 9.0

    2.5 * 2 + 17 / 4 9.0

    4.5 / 3 / 2 + 1 1.75

    46 / 3 / 2.0 / 3 * 4/5 2.0

    50 / 9 / 2.0 + 200 / 10 / (5.0 / 2) 10.5

    Exercise 4: Values

    What are the values of a, b, and cafter the following statements? Write your answers in the boxes on the

    right.

    int a = 5;

    int b = 10;int c = b;

    a++; // a? 6

    b--; // b? 9

    c = c + a; // c? 16

    Exercise 5: More values

    What are the values of i, j, and kafter the following statements? Write your answers on the right.

    int i = 2;

    int j = 3;

    int k = 4;int x = i + j + k;

    i = x - i - j; // i? 4

    j = x - j - k; // j? 2

    k = x - i - k; // k? 1

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

    SE 142 - QUIZZ file:///home/marc/danang/cse142/exam/quizz/quizz-solu...

    of 10 05/05/2011 10:27 PM

  • 8/12/2019 Quizz Solution

    3/10

    Exercise 6: What's the output?

    Write the output produced by the fo llowing Java program:

    public class OddStuff {

    public static void main(String[] args) {

    int number = 32;

    for (int count = 1; count

  • 8/12/2019 Quizz Solution

    4/10

    Exercise 8: More forloop

    Complete the following code:

    public class Triangle {

    private static int max = 4;

    public static void main(String[] args) {

    for (int line = 1; line

  • 8/12/2019 Quizz Solution

    5/10

    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, pop); // say coke not pepsi or pop

    carbonated(pop, pepsi, pepsi); // say soda not soda or pepsi

    carbonated("pop", pop, "koolaid"); // say pepsi not koolaid or pop

    carbonated(say, "say", pop); // say say not pepsi or pepsi

    }

    public static void carbonated(String coke, String soda, String pop) {

    System.out.println("say " + soda + " not " + pop + " or " + coke);

    }

    }

    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); // three times two = 6

    sentence(two, three, 14); // 1 times three = 28

    sentence(three, three, number + 1); // 1 times 1 = 42

    sentence(three, two, 1); // three times 1 = 2

    sentence("eight", three, number / 2); // 1 times eight = 20

    }

    public static void sentence(String three, String one, int number) {

    System.out.println(one + " times " + three + " = " + (number * 2));

    }

    }

    SE 142 - QUIZZ file:///home/marc/danang/cse142/exam/quizz/quizz-solu...

    of 10 05/05/2011 10:27 PM

  • 8/12/2019 Quizz Solution

    6/10

    Exercise 12: if/elsemystery

    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/elsemystery

    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

    SE 142 - QUIZZ file:///home/marc/danang/cse142/exam/quizz/quizz-solu...

    of 10 05/05/2011 10:27 PM

  • 8/12/2019 Quizz Solution

    7/10

    mystery2(4, 20); 8 21

    SE 142 - QUIZZ file:///home/marc/danang/cse142/exam/quizz/quizz-solu...

    of 10 05/05/2011 10:27 PM

  • 8/12/2019 Quizz Solution

    8/10

    Exercise 14: Even More if/elsemystery

    Consider the following Java code.

    public static void mystery3(int x, int y) {

    int z = 4;

    if (z

  • 8/12/2019 Quizz Solution

    9/10

  • 8/12/2019 Quizz Solution

    10/10

    Exercise 18: Even more whileloop 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

    SE 142 - QUIZZ file:///home/marc/danang/cse142/exam/quizz/quizz-solu...