CS 177 Spring 2005 Exam I
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: _________________________________
Student Last Name: __________________________________
Student First Name: __________________________________
Multiple Choice Questions (2 points each):
1. What does UML stand for?
a. Unified Modeling Language ***
b. Universal Moderation Language
c. Universal Modeling Language
d. Unified Markup Language
2. According to the textbook, “a value we pass to an object” is a definition for:
a. Variable
b. instance
c. message
d. 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: u ***
b. expr: inputStr.substring(0,6) + inputStr.substring(7,11);
output: PurdueUniv
c. expr: int spaceLoc = inputStr.indexOf(" ");
inputStr.substring(spaceLoc+1, spaceLoc+7) + "sal";
output: Universal
d. 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?
[pic]
a. Truck is a superclass of Van
b. Sedan is a subclass of Automobile ***
c. Van is a superclass of Automobile
d. Automobile is a descendant of Sedan
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 II
b. I, II and IV ***
c. I, II, and III
d. all four lines are valid comments
7. Which of the following shows the syntax for object creation for a defined object?
a. = new ( ) ;
b. = new ( ) ;
c. = ;
d. = new ( ) ; ***
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, 2006
b. February 9, 06
c. 02/09/2006
d. February 09, 2006 ***
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
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 \");
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.2
b. -113.0 ***
c. -127
d. -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.*;
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. accessor
b. mutator
c. constructor ***
d. 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. 2
b. 6
c. 7
d. 8 ***
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. 6
b. 9
c. 10
d. 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 class ***
b. Cannot pass an object to a private method
c. Cannot access private data members within public methods
d. Cannot access private data members within private methods
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.
1. _G/W_ a special method that is executed when a new instance of a class is created
2. __A__ a mechanism to design two or more entities that are different but share many common features.
3. __Y__ a method that sets a property of an object
4. __L__ the application of a systematic and disciplined approach to the development, testing, and maintenance of a program
5. __K__ a process that converts a value of one data type to another data type
6. __Q__ a method that does not return a value
7. __H__ memory locations used to store data values
8. __S__ a method that returns information about an object
9. __U__ a value passed to a method
10. __E__ a placeholder in a called method to hold the value of a passed argument
11. __X__ the internal details of how a method works are hidden from the client programmers
12. __W_ a method that may be automatically added to a class by the compiler
13. __D__ mechanism used to automatically deallocate unused space
14. __J__ how to determine the order of operations when two or more operators are present in an expression
15. __O__ 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
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.
import javax.swing.*;
public class Occurrences
{
public static void main(String[] args)
{
String str1 = JOptionPane.showInputDialog(null,
“Enter the string with two words:”);
int index = str1.indexOf(‘ ‘);
JOptionPane.showMessageDialog(null, “Backwards: ” +
str1.substring(index+1) +
“ ” +
str1.substring(0, index));
}
}
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
import java.util.*;
public class HeightConverter
{
final static double CM_TO_INCH = 2.54;
public static void main( String[] args )
{
Scanner scanner = new Scanner( System.in );
System.out.print( "What is your height?\nEnter number in feet: " );
int feet = scanner.nextInt();
System.out.print( "Enter number in inches: " );
int inches = scanner.nextInt();
System.out.println( "You are "
+ (12*CM_TO_INCH*feet + CM_TO_INCH*inches)
+ " cm tall." );
}
}
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.
class PlayDice {
public static void main( String args[] ) {
Dice d1 = new Dice();
d1.roll();
}
}
class Dice {
private static final int MAX_NUMBER = 6;
private static final int MIN_NUMBER = 1;
private int number1;
private int number2;
//Rolls the dice
public void roll( ) {
number1 = (int) (Math.floor(Math.random() * MAX_NUMBER + MIN_NUMBER));
number2 = (int) (Math.floor(Math.random() * MAX_NUMBER + MIN_NUMBER));
System.out.println("You rolled: " + number1 + " and " + number2 +
" resulting in " + (number1 + number2));
}
}
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.
import javax.swing.JOptionPane;
public class Concert {
public static void main( String args[] ) {
int numA, costA, numB, costB, numC, costC;
Seat a = new Seat();
Seat b = new Seat();
Seat c = new Seat();
numA = Integer.parseInt(JOptionPane.showInputDialog( null, "Number of A seats" ));
costA = Integer.parseInt(JOptionPane.showInputDialog( null, "Cost of an A seat" ));
a.setQuantity( numA );
a.setCost( costA );
numB = Integer.parseInt(JOptionPane.showInputDialog( null, "Number of B seats" ));
costB = Integer.parseInt(JOptionPane.showInputDialog( null, "Cost of a B seat" ));
b.setQuantity( numB );
b.setCost( costB );
numC = Integer.parseInt(JOptionPane.showInputDialog( null, "Number of C seats" ));
costC = Integer.parseInt(JOptionPane.showInputDialog( null, "Cost of a C seat" ));
c.setQuantity( numC );
c.setCost( costC );
JOptionPane.showMessageDialog( null,
"Total ticket sales: " + ( (a.getQuantity() * a.getCost()) +
(b.getQuantity() * b.getCost()) +
(c.getQuantity() * c.getCost()) ) );
}
}
class Seat {
private int _num;
private int _cost;
public void setQuantity( int num ) { _num = num; }
public int getQuantity( ) { return _num; }
public void setCost( int cost ) { _cost = cost; }
public int getCost() { return _cost; }
}
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- the mysteries of life
- cs 177 spring 2005 exam i
- grade 4 cluster quiz a office of superintendent of
- building access short survey
- construction of diagrams
- math 10005 kent
- mango dr y s r horticultural university
- triangle problems
- unit 4 practice problems scottsbluff public schools
- sat practice test 7 for assistive technology math test
Related searches
- cs ny employee benefits nyship
- 7 cs of communication ppt
- a p i final exam quizlet
- cs ny gov employee benefits
- 7 cs of effective communication
- the 7 cs of communication
- cs phd salary
- seven cs of communication
- project ideas for cs students
- 177 pellet trajectory chart
- oxford cs philosophy
- the 7 cs of communication definitions