Sample%20Final%20Exam%20with%20Answers

Sample%20Final%20Exam%20with%20Answers - page 1 The...

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

View Full Document Right Arrow Icon
page 1 The University College of the Cariboo Computing Science Department COURSE: COMP 113 – 01, 02 DATE: Thursday April 15, 2004 SEMESTER: Winter 2004 INSTRUCTORS : Wayne Babinchuk Maya Rupert FINAL EXAMINATION N AME : ______________________________________________ S TUDENT N UMBER : _______________________ Please answer all questions in the space provided. If you need more room, put the remainder of your answer on the back of the page and indicate this with an arrow. Use INPUT to read in data from the user and OUTPUT to write data out to the user unless the question specifically states to use another approach. You have 2.5 hours to complete this exam. The following questions have various degrees of difficulty. You may do the questions in any order. Do the questions that you know the answers to first. Do not spend too much time on any one question. Answers that do not follow sound programming principles, or are inefficient will result in a loss of marks. Total: 30 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
page 2 1. Evaluate the following expressions. You are to answer true or false, representing the value the expression evaluates to. The variables are assigned as follows. [1 mark] int limit = 10; int count = 3; a) (count > 0) || (limit < 10) TRUE b) FALSE 2. Write the following switch statement as an if-else statement. [2 marks] switch (vehicle) ANSWER: { case 1: if (vehicle == 1) OUTPUT( "Passenger car."); { OUTPUT( "Passenger car."); toll = 0.50; toll = 0.50; case 2: OUTPUT( "Bus."); toll = 1.50; } break; else if (vehicle == 2) case 3: { OUTPUT( "Bus."); OUTPUT( "Truck"); toll = 1.50; toll = 2.00; } break; else if (vehicle == 3) default : { OUTPUT( "Truck"); OUTPUT( "unknown vehicle"); toll = 2.00; } } OUTPUT( toll ); else OUTPUT( "unknown vehicle"); OUTPUT( toll ); 3. Write a method called swap( ) , defined in a class definition file, that swaps the values of two instance variables: a and b . For example, if a = 5, and b = -2 before swap is called, then after the method executes a = -2, and b = 5. [1 mark] public void swap( ) { int temp = a; a = b; b = temp; }
Background image of page 2
page 3 4. What output will be produced by the following code segment? [2 marks] k = 4; OUTPUT: while (k >= 1) 4 2 1 { 3 2 1 System.out.print(k + " "); 2 2 1 j = 2; 1 2 1 while (j >= 1) { System.out.print(j + " "); --j; } System.out.println(); k--; } 5. If arrays called myArray1 and myArray2 already exist, explain the effect(s) , if any, the following code segment has on the two arrays. [1 mark] myArray2 = myArray1; myArray2[0] = 5; The first statement makes myArray2 an alternate name for (or referebce to) myArray1. The second statement changes the first location in myArray2 to 5, but this also changes the first
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 04/14/2011 for the course COMP 1130 taught by Professor Na during the Winter '11 term at Thompson Rivers.

Page1 / 10

Sample%20Final%20Exam%20with%20Answers - page 1 The...

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