2002FCTestAnswers

2002FCTestAnswers - COMPSCI 101FC 2002 TEST AND ANSWER BOOK...

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

View Full Document Right Arrow Icon
COMPSCI 101FC 2002 TEST AND ANSWER BOOK PAGE 1 STUDENT 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)
Background image of page 1

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

View Full DocumentRight Arrow Icon
COMPSCI 101FC 2002 TEST AND ANSWER BOOK PAGE 2 STUDENT 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)
Background image of page 2
COMPSCI 101FC 2002 TEST AND ANSWER BOOK PAGE 3 STUDENT 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. true 2. (x != 6.0) || myCompare true 3. myCompare ^ (b < (a – c)/b) false (3 marks)
Background image of page 3

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

View Full DocumentRight Arrow Icon
COMPSCI 101FC 2002 TEST AND ANSWER BOOK PAGE 4 STUDENT UPI: b) Why is it not recommended to compare double values in the following manner? double x = 0.2 + 0.2 + 0.2;
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.

This note was uploaded on 03/01/2012 for the course COMPSCI 101 taught by Professor Adrianaferraro during the Summer '12 term at Auckland.

Page1 / 12

2002FCTestAnswers - COMPSCI 101FC 2002 TEST AND ANSWER BOOK...

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