{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Exam2005Solution - Solutions to the 2005 exam Question 1A[4...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Solutions to the 2005 exam Question 1A [4] In this question, use only the following ( algorithm format ) Boolean expressions: comparison operators: <, >, =, , , and Boolean logical operators: NOT, AND, OR arithmetic operators: +, -, *, /, and MOD (modulo) variable names and constants. Use parentheses where necessary. Do not use Java syntax! The Ontario “Drive Clean” program requires that a car must pass an emissions test for licence renewal if both of the following conditions are met: The car’s model year MY is odd and the current year CY is even, or vice versa The difference between the current year and the model year is at least 3 but no more than 20. Write a Boolean expression that is true if a car needs a Drive Clean emissions test, and false
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Question 1A (((MY MOD 2 = 1) AND (CY MOD 2 = 0)) OR ((MY MOD 2 = 0) AND (CY MOD 2 = 1))) AND ((CY – MY 3) AND (CY – MY 20)) Alternative ((MY MOD 2 + CY MOD 2) = 1) AND ((CY – MY 3) AND (CY – MY 20)) Question 1B [4] What will be printed by the following program? Write your answer below the program. class ABC { public static void main(String[ ] args) { char[][] x = {{'i', 'j'}, {'4', '5'}}; char[][] y = {{'x', 'y'}, {'a', 'b'}}; int i; char t; for (i = 0; i < 2; i=i+1) { t = y[(i+1)%2][1]; y[i][(i+1)%2] = x[i][0]; y[(i+1)%2][1] = t; } System.out.print(y[0][1]); System.out.println(y[1][0]); } } t = y[1][1] = ‘b’ y[0][1] = x[0][0]=‘i’ y[1][1] = ‘b’ t=y[0][1] = ‘i’ y[1][0] = x[1][0] = ‘4’ y[0][1] = ‘i’
Image of page 2
Question 1B Answer: i4 Question 1C [4] class C1 { private int[] v1 = {1,4,9}; public int v3; public static int m1 (C2 p) { ... } private C1 m2 (int m) { ... } } class C2 { public static char v3; public C2 (int n) { ... } private void m3 (int m) { ... } } Suppose that the following instructions are used in the main() method in a class Test . Each choice should be considered independently – as it if were in its own main() method. Circle the letter of the statement which does NOT cause a compilation error.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Question 1C (a) int v2 = C1.m1(this); (b) char v3 = C2.v3; (c) C1 x = new m2(4); (d) C1 w = new C1(); w.v1[2] = 3; (e) C2 y = new C2(); int z = C1.m1(y); Error: m1 expects object of type C2 as parameter No error Error: no constructor m2 Error: v1 is private in C1 Error: no constructor with zero arguments in C2 Question 2 [8] class AClass { public static void main(String[ ] args) { aMethod(137210); System.out.println( ); } public static void aMethod(int i) { // see next slide } } Here is a program that uses recursion.
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern