Final Exam for Introduction to Programming | COMP 110, Exams of Computer Science

Material Type: Exam; Class: Introduction to Programming; Subject: COMPUTER SCIENCE; University: University of North Carolina - Chapel Hill; Term: Fall 2006;

Typology: Exams

Pre 2010

Uploaded on 03/16/2009

koofers-user-isv
koofers-user-isv 🇺🇸

9 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Final Exam
COMP 110-001 FALL 2006
Dec. 14, 2006
1. Closed book and closed notes
2. Write all scratch works and answers on the exam itself. If you need extra space, let me
know. Indicate your final answer by drawing a box / circle around it to separate it
from your scratch work.
3. Write legibly. If I can’t read it, you will not get any credit for it.
4. You don’t have to write comments in the codes.
5. It’s always a good idea to show me the steps.
I pledge that I have neither received nor given unauthorized aid on this examination.
Signed : ___________________________________________
Printed Name : ____________________________________________
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Final Exam for Introduction to Programming | COMP 110 and more Exams Computer Science in PDF only on Docsity!

Final Exam

COMP 110-001 FALL 2006

Dec. 14, 2006

  1. Closed book and closed notes
  2. Write all scratch works and answers on the exam itself. If you need extra space, let me know. Indicate your final answer by drawing a box / circle around it to separate it from your scratch work.
  3. Write legibly. If I can’t read it, you will not get any credit for it.
  4. You don’t have to write comments in the codes. 5. It’s always a good idea to show me the steps. I pledge that I have neither received nor given unauthorized aid on this examination. Signed : ___________________________________________ Printed Name : ____________________________________________

1. Multiple Choice( 20 points) Identify the letter of the choice that best completes the statement or answers the question. (1) Which of the following statements about a named constant is NOT true? ______ a. Its content cannot change during program execution. b. Its value can be changed during program execution. c. It is a memory location. d. It is declared using the reserved word final. ( 2 ) How many constructors can a class have? _____ a. 0 c. 2 b. 1 d. Any number ( 3 ) How can a method send a primitive value back to the caller? _____ a. It cannot send primitive values back to the caller b. By using the return statement c. By assigning the value to one of its parameters d. It can call its caller with the value (4) In a Java program, the file name must be the same as the ____. a. method name c. object name b. class name d. data name (5) What is the value of counter after the following statements executes? _____ counter = 0; while (counter <= 50) counter = counter + 1; a. 48 c. 51 b. 50 d. 53 (6) Which of the following is NOT true about return statements? _____ a. A value-returning method returns its value via the return statement. b. Return statements can be used in void methods to return values. c. A method can have more than one return statement. d. Whenever a return statement executes in a method, the remaining statements are skipped and the method exits.

2. Explain Concepts ( 8 points) (1) Briefly explain the terms class and object , and describe how they’re related to each other. You can use specific examples if you want. (2) Describe the difference between shallow copying and deep copying.

3. What is the output after executing following codes? ( 8 points) (1) int a = 2; int b = 0; int i = 0; while (i < 5) { System.out.println("a = " + a + " b = " + (++b)); a = a + 2; i = a / 2; if (i > 6 ) continue; i = 2; } (2) int x = 7; boolean found = false; do { System.out.print(x + " "); if (x <= 2) found = true; else x = x – 5; } while (x > 0 && !found);

5. Sorting ( 10 points) Sort the following list using the bubble sort, the selection sort, and the insertion sort. Show the list after each iteration of the outer for loop. 28 18 5 42 1 30 (a) Bubble sort (b) Selection sort (c) Insertion sort

6. Consider the following definitions of a recursive method. ( 8 points) (a) public static void mystery(int num) { if (num > 0) { mystery(num – 1); System.out.print(num + " "); } } What is the output of the following statement? mystery(4); (b) public static int mystery(int first, int last) { if (first > last) return 0; else if (first == last) return first; else return first + mystery(first + 1, last – 1); } What is the output of the following statement? System.out.println(mystery(6, 10));

public class Robot { // member variables // Current robot location and direction. // Constructors // No parameters: locate the robot at (0,0) facing up. public Robot() { } // Two parameters: locate the robot at (x,y) facing up. public Robot(int x, int y) { } // Methods // Move the robot one square in the direction it is facing. public void move() { } // Question continues on the next page.

// Continued from the previous page. // Turn the robot one quarter turn to the right (e.g. from up // to right;from right to down; from down to left, // and from left to up). public void turn() { } // What is the robot’s current x position? public int whereX() { } // What is the robot’s current y position? public int whereY() { } // What direction is the robot currently facing // (in degrees: 0, 90, 180, or 270)? public int whatDirection() { } } // End of Robot class

9. ( 8 points) A prime number (or prime integer, often simply called a "prime" for short) is a positive integer that has no positive integer divisors other than 1 and itself. (More concisely, a prime number is a positive integer having exactly one positive divisor other than 1). For example, the only divisors of 13 are 1 and 13, making 13 a prime number, while the number 24 has divisors 1, 2, 3, 4, 6, 8, 12, and 24 (corresponding to the factorization ), making 24 not a prime number. Write a method that uses a loop to decide whether a number is prime or not. public static boolean IsPrime1(int num) {

10. Recursion, decimal to binary ( 8 points) In this program, we want to design and write a program that uses recursion to convert a non-negative integer in decimal format into the equivalent binary number. First we define some terms. Let x be a non-negative integer. We call remainder of x after division by 2 the rightmost bit of x. Thus, the rightmost bit of 33 is 1 and rightmost bit of 28 is 0. Following is an example of finding the binary representation of 35 (This is different algorithm from the one we learned in the class). 35 divided by 2 : quotient 17 remainder 1 17 divided by 2 : quotient 8 remainder 1 8 divided by 2 : quotient 4 remainder 0 4 divided by 2 : quotient 2 remainder 0 2 divided by 2 : quotient 1 remainder 0 1 divided by 2 : quotient 0 remainder 1 The rightmost bit of the first computation becomes the rightmost bit of the final answer. The second rightmost bit is computed from the quotient of the first computation and so on until the quotient becomes 0. (1) Fill in the blanks (underlined) in the following code that converts a non-negative decimal to the equivalent binary number (It is going to print the resulting binary number). public static _______ decToBin(int num) { if( ________________ ) { decToBin( __________ ); System.out.print( ___________ ); } } (2) What is the output of the following statement? decToBin(69);