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

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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’
Background 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.
Background image of page 3

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

View Full DocumentRight Arrow Icon
(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
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

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

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online