{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

COMPSCI 101FC 2002 T EST A ND A NSWER B OOK PAGE 1 S TUDENT UPI: Question 1 (11 marks) a) What would be printed out by the following code segment? int i = 10; double d = i / 3; System.out.println(d); 3.0 (1 mark) b) What is printed by the following? System.out.println(7 + 7 + 7 + "7" + (7 + 7) + 7); 217147 (1 mark) c) Write an expression to generate a random integer between 8 and 13 (including 8 and 13) and assign it to the variable i . int i = (int)(Math.random() * 6) + 8; (1 mark) d) What is the output of the following code segment? System.out.println("a"); System.out.print("a"); System.out.println("a"); System.out.print("a"); System.out.println("a" + "b"); System.out.println("a"); a aa aab a (2 marks)

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

View Full Document
COMPSCI 101FC 2002 T EST A ND A NSWER B OOK PAGE 2 S TUDENT UPI: e) What is the output of the following code segment? double d = (int)(5.3 + 2.8); System.out.println(d); 8.0 (1 mark) f) What is the output of the following code segment? System.out.println("\\n\""); \n" (1 mark) g) What is the output of the following code segment? int i1 = 2 + 2 - 2 * 2 / 2; int i2 = 2 * 2 / 2 - 2 + 2; System.out.println(i1 + "." + i2); 2.2 (1 mark) h) What is the output of the following code segment: int i = 9 - 8 - (7 * 6) / 5 + 4 - (3 + 2) * 2; System.out.println(i); -13 (1 mark) i) What is the output of the following code segment: String s = "computer science"; String t = s.substring(3, 6); System.out.println(s.length() - t.length() + t); 13put (2 marks)
COMPSCI 101FC 2002 T EST A ND A NSWER B OOK PAGE 3 S TUDENT UPI: Question 2 (8 marks) Complete the following method which displays the question passed as a parameter and waits for the user to type a response. If the first character of the response is a “y” or a “Y” then the method should return true . In all other cases it should return false . You can assume that the readInput() method used in labs and assignments is available to return the String typed by a user. public boolean askQuestion(String question) { System.out.print(question); String answer = readInput(); String firstChar = answer.substring(0, 1); return firstChar.equals("Y") || firstChar.equals("y"); } Question 3 (5 marks) a) Consider the following piece of Java code. After this code has been executed are the following Boolean expressions true or false ? int a = 37; int b = 6; double x = b – 1.0; int c = (int)(a/x); boolean myCompare = c == a / b; 1. (c > b) && (a % 2 == 1) true 2. (x != 6.0) || myCompare true 3. myCompare ^ (b < (a – c)/b) false (3 marks)

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

View Full Document
COMPSCI 101FC 2002 T EST A ND A NSWER B OOK PAGE 4 S TUDENT UPI: b) Why is it not recommended to compare double values in the following manner?
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}