# 2002SCTestAnswers - COMPSCI 101 THE UNIVERSITY OF AUCKLAND...

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

COMPSCI 101 THE UNIVERSITY OF AUCKLAND SECOND SEMESTER, 2002 Campus: City and Tamaki COMPUTER SCIENCE TEST Principles of Programming (Time allowed: 75 minutes) NOTE: Attempt ALL questions. Write your answers in the space provided. There is space at the back for answers that overflow the allotted space No Calculators are permitted Surname SOLUTIONS Forenames Student ID Laboratory Day and Time Question Marks Out of Question 1 (Primitive Types) 15 Question 2 (Strings) 10 Question 3 (If statements) 10 Question 4 (Assignment 02) 10 Question 5 (Assignment 02) 10 Question 6 (Loops) 8 Question 7 (Arrays) 12 Question 8 (Classes) 15 Question 9 (Frames) 10 Total 100

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

View Full Document
Candidate's Name:___________________ 2 COMPSCI 101
Candidate's Name:___________________ 3 COMPSCI 101 1. What is the output for each of the following statements? (a) System.out.println( 1 + 2 + "3" + "4" + 5 + 6); 33456 (3 marks) (b) System.out.println( 2/3 + 3.0/2.0 + " " + 2/3 + 3.0/2.0); 1.5 01.5 (3 marks) (c) System.out.println( 5%10 + " " + 10%3 ); 5 1 (3 marks) (d) System.out.println( 1* 2+3 %4 +5/ 6+1.0 ); 6.0 (3 marks) (e) System.out.println( "\"+"+"\"+\n^\\Test\\" ); "+"+ ^\Test\ (3 marks)

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

View Full Document
Candidate's Name:___________________ 4 COMPSCI 101 2. Write a method called removeLetter which accepts a String s and an integer i as parameters. Your method should remove a single character from the String passed as a parameter, and it should return the String with the character removed. The character which is to be removed is located at the index specified by i . Example: Executing the statements: System.out.println( removeLetter( "Hello", 0 ) ); System.out.println( removeLetter( "Hello", 1 ) ); System.out.println( removeLetter( "Hello", 2 ) ); would produce the output: ello Hllo Helo You can assume that the integer passed to the method as the index will be a valid index value (e.g. it will be in the range 0 … length-1). Write your method below. private static String removeLetter( String s, int i ){ String result = s.substring(0,i) + s.substring(i+1); return result; } Note: No marks are allocated to private or static. (10 marks)
Candidate's Name:___________________ 5 COMPSCI 101 3. What is the output of each of the following code fragments? (a) System.out.println( 23==34 || 3==3); true (2 marks) (b) System.out.println( 1!=2 && !(3==3) || 2==2 ); true (2 marks) (c) int a = 4; int b = 4; int c = 2; if( a==b ) if ( b<c ){ if ( c>a ) System.out.println( "First" ); } else System.out.println( "Second" ); else System.out.println( "Third" ); System.out.println( "Fourth" ); Second Fourth (2 marks) (d) int a = 0; int b = 3; if( !(a<b) || (a!=b && b==3) ) System.out.println( a ); if( !(a<b || a!=b && b==3) ) System.out.println( b ); System.out.println("done"); 0 done (4 marks)

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

View Full Document
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 / 16

2002SCTestAnswers - COMPSCI 101 THE UNIVERSITY OF AUCKLAND...

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

View Full Document
Ask a homework question - tutors are online