2004S1TestAnswers

2004S1TestAnswers - e) What is printed by the following?...

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

View Full Document Right Arrow Icon
CompSci 101 S1 2004 – City and Tamaki Terms Test Model Answers Question 1 (20 marks) a) What is printed by the following? System.out.println("5" + 5 + 9); 559 (1 mark) b) What is printed by the following? System.out.println(6 + 3 + "8"); 98 (1 mark) c) What is printed by the following? System.out.println((int) 5.3); 5 (1 mark) d) What is printed by the following? System.out.println("\"n\\\n\""); "n\ " (2 marks) e) What is printed by the following? System.out.println(5 + 9 % 2 * (10 / 4) - 6); 1 (2 marks) f) What is printed by the following? System.out.println(5 / 2.0 * 3 / 5); 1.5 (1 mark) g) What is printed by the following? String string01 = "food looks good"; System.out.println(string01.indexOf("oo",4)); 6 (2 marks) h) What is printed by the following? System.out.println(Math.pow(Math.max(4,3), 2)); 16.0 (2 marks) i) What is printed by the following? System.out.print((int)3.14159 + "\nis the"); System.out.print("value of pi."); 3 is the value of pi. (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
j) What is printed by the following? String string02 = "abcdefghij"; System.out.println(string02.substring(3,string02.length()-3)); defg (2 marks) k) What is printed by the following? String string03 = " 11 22 33 "; String string04 = string03.trim(); string04+=3; System.out.println(string04); 11 22 333 (2 marks) l) What is printed by the following? double number01 = 12; int number02 = 5; number01 -= number02; System.out.println(number01); 7.0 (2 marks) Question 2 (10 marks) Consider the following section of Java source code, and answer the questions below: int x = 5; int y; char c = ‘a’; y = (int)Math.pow(x,2); Rectangle aRect = new Rectangle(x,y,30,20); String rectString = "Rect1" + " is " + aRect.toString(); a) List all the identifiers which appear in the above code fragment String, Math, Rectangle, x, y, c, aRect, rectString pow, toString (5 marks) b) Identify all the variables in the above code fragment and categorize them according to whether they are primitive or object types. PRIMITIVE VARIABLES: x, y, c OBJECT VARIABLES: aRect, rectString (5 marks)
Background image of page 2
Question 3 (10 marks) You need to find and correct five errors in the program Q3 . There is no more than one error on each line of code. The application program Q3 is supposed to generate a random number between 0 and 26, and then print out that number of letters from the alphabet. Three examples of the application being executed are shown below – the output must be identical to that shown: Example 1: C:\> java Q3
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
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 / 7

2004S1TestAnswers - e) What is printed by the following?...

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

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