21
CS 180 Spring 2006 Exam I There are 20 multiple choice questions. Each one is worth 2 points. There are 5 programming questions worth 10 to 15 points. Answer the multiple choice questions on the bubble sheet given and the programming questions on the exam booklet. Fill in the Instructor, Course, Signature, Test, and Date blanks. For "Instructor" put your Recitation Instructor's last name. For "Course" put CS 180. For "Test" put Exam 1. 08:30 recitation in UNIV 101: Matt Carlson 09:30 recitation in UNIV 119: Andy Scharlott 10:30 recitation in STON 217: Nick Sumner 11:30 recitation in UNIV 101: Alvin Law Fill in the bubbles that correspond to your name, section and student ID in the bubble sheet. For your section number, use 0830, 0930,1030, or 1130 -- based on the start time of your Friday recitation. For your student ID, use the 10 digit ID number on your student ID card. DO NOT USE YOUR SOCIAL SECURITY NUMBER! Exams without names will be graded as zero. Only the answers on the bubble sheet will be counted. The questions will be discarded. For the programming questions create a Java class that would compile without error. Comments are nice, but are not required. Recitation Start Time: _________________________________

CS 177 Spring 2005 Exam Ics180/Fall2007Web/exams/e…  · Web viewInheritance allows a Java program to effectively utilize resources. Inheritance saves the time of having to define

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 177 Spring 2005 Exam Ics180/Fall2007Web/exams/e…  · Web viewInheritance allows a Java program to effectively utilize resources. Inheritance saves the time of having to define

CS 180 Spring 2006 Exam I

There are 20 multiple choice questions. Each one is worth 2 points. There are 5 programming questions worth 10 to 15 points.

Answer the multiple choice questions on the bubble sheet given and the programming questions on the exam booklet.

Fill in the Instructor, Course, Signature, Test, and Date blanks. For "Instructor" put your Recitation Instructor's last name. For "Course" put CS 180. For "Test" put Exam 1.

08:30 recitation in UNIV 101: Matt Carlson09:30 recitation in UNIV 119: Andy Scharlott10:30 recitation in STON 217: Nick Sumner11:30 recitation in UNIV 101: Alvin Law

Fill in the bubbles that correspond to your name, section and student ID in the bubble sheet. For your section number, use 0830, 0930,1030, or 1130 -- based on the start time of your Friday recitation.

For your student ID, use the 10 digit ID number on your student ID card. DO NOT USE YOUR SOCIAL SECURITY NUMBER!

Exams without names will be graded as zero. Only the answers on the bubble sheet will be counted. The questions will be discarded.

For the programming questions create a Java class that would compile without error. Comments are nice, but are not required.

Recitation Start Time: _________________________________

Student Last Name: __________________________________

Student First Name: __________________________________

Page 2: CS 177 Spring 2005 Exam Ics180/Fall2007Web/exams/e…  · Web viewInheritance allows a Java program to effectively utilize resources. Inheritance saves the time of having to define

Multiple Choice Questions (2 points each):

1. What does UML stand for?

a. Unified Modeling Languageb. Universal Moderation Languagec. Universal Modeling Languaged. Unified Markup Language

2. According to the textbook, “a value we pass to an object” is a definition for:

a. variableb. instancec. messaged. argument

3. Which of the following has the incorrect matching of expression and result given the following initialization of the String variable inputStr:

String inputStr = "Purdue University";

a. expr: inputStr.substring(inputStr.indexOf("u"), inputStr.indexOf("u")); output: ub. expr: inputStr.substring(0,6) + inputStr.substring(7,11); output: PurdueUnivc. expr: int spaceLoc = inputStr.indexOf(" "); inputStr.substring(spaceLoc+1, spaceLoc+7) + "sal"; output: Universald. expr: inputStr.substring(inputStr.length()-5, inputStr.length()-4) + inputStr.substring(inputStr.length()-6, inputStr.length()-5) + inputStr.substring(inputStr.length()-4, inputStr.length()-3) + inputStr.substring(inputStr.length()-2, inputStr.length()-1) ; output: rest

4. Given the inheritance diagram below, which of the following statements is valid?

a. Truck is a superclass of Vanb. Sedan is a subclass of Automobilec. Van is a superclass of Automobiled. Automobile is a descendant of Sedan

Page 3: CS 177 Spring 2005 Exam Ics180/Fall2007Web/exams/e…  · Web viewInheritance allows a Java program to effectively utilize resources. Inheritance saves the time of having to define

5. What is the value of inheritance? Pick the best answer.

a. Inheritance allows a Java program to effectively utilize resources.b. Inheritance saves the time of having to define methods for any given class.c. Inheritance can be used to efficiently design two or more entities that are different but share many

common features.d. Inheritance makes network programming in particular much easier

6. Which of the following lines of code are valid comments in Java?

I. /* I am not a valid comment! */II. // I am not a valid comment! */III. /* // I am not a valid comment!IV. // /* I am not a valid comment!

a. I and IIb. I, II and IVc. I, II, and IIId. all four lines are valid comments

7. Which of the following shows the syntax for object creation for a defined object?

a. <object name> = new <arguments> ( <class name> ) ;b. <class name> = new <object name> ( <arguments> ) ;c. <object name> = <class name> <identifier> ;d. <object name> = new <class name> ( <arguments> ) ;

8. What is the resulting string of str3 given the following?

String str1 = “Java is better than C++”; String str2 = “Fortran is better than both!”; String str3 = str1 + “, but ” + str2.substring(0, str2.length()-1) + “...”;

a. “Java is better than C++, but Fortran is better than both!...”b. “Java is better than C++Fortran is better than both!”c. “Java is better than C++, but Fortran is better than both…”d. “Java is better than C++, but ortran is better than both!…”

9. Given the following SimpleDateFormat object:

SimpleDateFormat sdf = new SimpleDateFormat(“MMMM dd, yyyy”);Date todaysDate = new Date();

Which is the proper resulting string for today’s date, February 9th, 2006 on the call sdf.format(todaysDate)?

a. Feb. 9, 2006b. February 9, 06c. 02/09/2006d. February 09, 2006

Page 4: CS 177 Spring 2005 Exam Ics180/Fall2007Web/exams/e…  · Web viewInheritance allows a Java program to effectively utilize resources. Inheritance saves the time of having to define

10. Given the class Account as defined below, answer the following question.

class Account {public int balance;private String ownerName;

public Account () { ownerName = “Unassigned”; balance = 0;}public Account (String name, int bal) { ownerName = name; balance = bal;}public void deposit(int amt){ balance += amt;}public void withdraw(int amt){

balance -= amt;}public int getBalance(){ return balance;}public String getOwnerName(){ return ownerName;}public void setOwnerName(String name){ ownerName = name;}

}

What is the output of the following code from a client class:

Account act1, act2, act3;act1 = new Account();act2 = new Account(“John”, 100);act3 = act1;act3.balance += 100;act2.withdraw(200);act3.deposit(100);act3 = new Account(“Jane”, 300);System.out.println(“Act1: “ + act1.getBalance() + “, “ + act1.getOwnerName());System.out.println(“Act2: “ + act2.getBalance() + “, “ + act2.getOwnerName());

a. Act1: 100, John Act2: 200, Unassigned

b. Act1: 200, Unassigned Act2: -100, John

c. Act1: 300, Jane Act2: -100, John

Page 5: CS 177 Spring 2005 Exam Ics180/Fall2007Web/exams/e…  · Web viewInheritance allows a Java program to effectively utilize resources. Inheritance saves the time of having to define

d. Act1: 500, Unassinged Act2: 300, Jane

11. Which of the following assignments is valid?

a. byte a = 128;b. short b = 128.0;c. float c = 128.0;d. double d = 128.0;

12. Which of the following code segments will compile without warning or error? Assume the proper packages have been imported.

a. Scanner scanner = new Scanner(System.out);b. double dNum; dNum = scanner.parseDouble();c. DecimalFormat df = new DecimalFormat("0.000"); double circumference = 6.28318; System.out.println(df.format(circumference));d. int num = 1234; System.out.println("The given value of num is \<"+ num + "\>");

13. Which of the following will compile and execute without error?

a. int a = Integer.parseInt( "42L" );b. double b = Integer.parseInt( "512" );c. int c = Double.parseDouble( 42.0 );d. float d = Double.parseDouble( "112358" );

14. Given the following declarations:

int i = 5, j = 6;double d = 20.0, e = 40.0;

What is the value of the following expression?

- -d*j+e/i–j/i

a. -113.2b. -113.0c. -127d. -126.8

15. Before one can begin writing programs that can take user input through a graphical user interface, ___________ must be imported.

a. java.io.*;b. javax.swing.*;c. java.packages.output.*;d. java.sys.*;

Page 6: CS 177 Spring 2005 Exam Ics180/Fall2007Web/exams/e…  · Web viewInheritance allows a Java program to effectively utilize resources. Inheritance saves the time of having to define

16. What is the difference between the keyword "public" and the keyword "private" in Java?

a. "public" methods of a class are used within client programs. "private" methods and data members determine internal details of a class.b. "private" methods of a class are used within client programs. "public" methods and data members determine internal details of a class.c. "public" and "private" can be used interchangeably.d. "public" should always be used for local variables. "private" should always be used for class data members and methods.

17. A(n) _______ is a special method that is executed when a new instance of the class is created.

a. accessorb. mutatorc. constructord. destructor

18. Given the definition of the following two classes, what will the code output?

class Q18Main {

public static void main( String[] args ) { Question18 q18; q18 = new Question18();

q18.init(); q18.count = q18.increment() + q18.increment(); System.out.println( q18.increment() ); } }

class Question18 { public int count; public void init() { count = 2; }

public int increment() { count = count + 1; return count; } }

a. 2b. 6c. 7d. 8

Page 7: CS 177 Spring 2005 Exam Ics180/Fall2007Web/exams/e…  · Web viewInheritance allows a Java program to effectively utilize resources. Inheritance saves the time of having to define

19. Given the definition of the following two classes, what will the code output?

class Q19Main { public static void main( String args[] ) { Question19 q19 = new Question19(); q19.a = 1; q19.b = 2; q19.methodOne( q19.a + q19.b + 3 ); } }

class Question19 { public int a; public int b; private int c = 0;

public void methodOne( int num ) { return num + a + b + c; } }

a. 6b. 9c. 10d. This code will not compile.

20. What is wrong with the following class?

class MyText {

private String word; private String temp; private int index;

public String firstLetter() { index = 0; return getLetter( word ); }

public String lastLetter() { index = word.length() - 1; return getLetter( word ); }

private String getLetter( String s ) { temp = s.substring( index, index + 1 ); return temp; }

}

a. There is nothing wrong with this classb. Cannot pass an object to a private methodc. Cannot access private data members within public methodsd. Cannot access private data members within private methods

Page 8: CS 177 Spring 2005 Exam Ics180/Fall2007Web/exams/e…  · Web viewInheritance allows a Java program to effectively utilize resources. Inheritance saves the time of having to define

Programming and Matching Questions:

1. (15 pts) MATCHING Given the definitions below, choose the term that best matches from the list A-Z and write the appropriate letter in the blank. Note: Please write clearly. Any unreadable letters will be counted as incorrect.

Page 9: CS 177 Spring 2005 Exam Ics180/Fall2007Web/exams/e…  · Web viewInheritance allows a Java program to effectively utilize resources. Inheritance saves the time of having to define

1. _____ a special method that is executed when a new instance of a class is created

2. _____ a mechanism to design two or more entities that are different but share many common features.

3. _____ a method that sets a property of an object

4. _____ the application of a systematic and disciplined approach to the development, testing, and maintenance of a program

5. _____ a process that converts a value of one data type to another data type

6. _____ a method that does not return a value

7. _____ memory locations used to store data values

8. _____ a method that returns information about an object

9. _____ a value passed to a method

10. _____ a placeholder in a called method to hold the value of a passed argument

11. _____ the internal details of how a method works are hidden from the client programmers

12. _____ a method that may be automatically added to a class by the compiler

13. _____ mechanism used to automatically deallocate unused space

14. _____ how to determine the order of operations when two or more operators are present in an expression

15. _____ a sequence of specific characters used to identify classes, objects, methods, etc.

A. inheritance

B. assignment conversion

C. pseudorandom number

generator

D. garbage collection

E. parameter

F. bytecode

G. constructor

H. variables

I. binary operator

J. precedence rules

K. typecasting

L. software engineering

M. null method

N. operator overloading

O. identifier

P. programmer defined

classes

Q. void method

R. echo printing

S. accessor

T. stub

U. argument

V. reserved word

W. default constructor

X. information hiding

Y. mutator

Z. accessibility modifier

Page 10: CS 177 Spring 2005 Exam Ics180/Fall2007Web/exams/e…  · Web viewInheritance allows a Java program to effectively utilize resources. Inheritance saves the time of having to define

2. (10 pts) Write a complete Java class which takes in a string consisting of two words (separated by a blank space) and prints out the two words in opposite order. Your program should use a JOptionPane to accept the input string (with an appropriate text to follow) and another JOptionPane to print out the result.

10

Page 11: CS 177 Spring 2005 Exam Ics180/Fall2007Web/exams/e…  · Web viewInheritance allows a Java program to effectively utilize resources. Inheritance saves the time of having to define

3. (10 pts) Using the standard input and standard output, i.e. System.in and System.out, write a complete Java class that reads in the user's height in feet and inches and then displays how tall the user is in centimeters. Note: There are 12 inches per foot and 1 inch = 2.54 cm. A sample execution may look like the following:

What is your height?Enter number in feet: (user enters 6)Enter number in inches: (user enters 2)You are 187.96 cm tall

11

Page 12: CS 177 Spring 2005 Exam Ics180/Fall2007Web/exams/e…  · Web viewInheritance allows a Java program to effectively utilize resources. Inheritance saves the time of having to define

4. (10 pts) Write a Dice class that will simulate the rolling of 2 dice. Remember each die can have 6 possible values, 1 through 6. You need to make sure you declare all the variables you need. You only need to write the roll method that will simulate a roll of the dice and display the value of each die as well as the total.

12

Page 13: CS 177 Spring 2005 Exam Ics180/Fall2007Web/exams/e…  · Web viewInheritance allows a Java program to effectively utilize resources. Inheritance saves the time of having to define

5. (15 pts) Write a program that computes the total ticket sales of a concert. There are three tiers of seating: A, B, and C. The program accepts the number of tickets sold and the price of a ticket for each of the three types of seats. The total sales are computed as follows:

totalSales = numOfASeats * priceOfASeat + numOfBSeats * priceOfBSeat + numOfCSeats * priceOfCSeat

You have to write two classes: Concert and Seat. The Concert class is the main class of the program. The Concert class must have three instances of the Seat class, one to represent each tier of seating. The quantity of each Seat type and price of each Seat should be set and retrieved on each Seat object by using accessor and mutator methods.

Input should be retrieved from the user using the JOptionPane class as used in class projects. Again, the user will input the quantity and cost of each type of Seat. The total sales should be shown to the user at the end of the program. You may assume all values are integer.

13