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

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

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

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

View Full Document
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) (limit > 0) && ((limit/count) > 3) 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; }
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.

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.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern