CS 177 Spring 2005 Exam I .edu
CS 180 Fall 2005 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 REC 122: Matt Carlson
08:30 recitation in BRNG B254: Andy Scharlott
09:30 recitation in BRNG B268: Nick Sumner
11:30 recitation in REC 308: Rimma Nehme
01:30 recitation in UNIV 103: 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,1130, or 0130 -- 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: __________________________________
Student ID: _________________________________________
Multiple Choice Questions (2 points each):
1. Which of the following lines will compile without warning or error.
a. char c = "a";
b. byte b = 257;
c. boolean b = null;
d. int i = 10;
2. What will happen if you try to compile and run the following code
public class MyClass {
public static void main(String[] arguments)
{
aMethod(arguments);
}
public void aMethod(String[] arguments)
{
System.out.println(arguments);
System.out.println(arguments[1]);
}
}
a. error: static context cannot reference non-static aMethod.
b. error: method main not correct
c. error: array must include parameter
d. aMethod must be declared with String
3. What is the value of pay after executing the code below?
int payRate = 10;
int hoursWorked = 42;
int pay;
pay = (hoursWorked 6 )
System.out.println( "Time and tide wait for no one." );
else if ( time + tide > 5 )
System.out.println( "Time and tide wait for me." );
else if ( time + tide > 4 )
System.out.println( "The tide cannot hurt Purdue Pete." );
else
System.out.println( "Paper covers rock." );
a. "Time and tide wait for no one."
b. "Time and tide wait for me."
c. "The tide cannot hurt Purdue Pete."
d. "Paper covers rock."
7. A mutator method is a method that:
a. allows you to change the value of an instance variable
b. reads and returns the value of an instance variable
c. prints to the screen the value of an instance variable
d. mutator methods do not exist
8. Which of the following is not a primitive type in Java?
a. byte
b. short
c. long
d. String
9. What is the output of the following code segment?
int i = 14;
double d = 10.0;
System.out.print(i++/3 + " ");
System.out.print(++i/4 + " ");
System.out.print((d+i)/5);
a. 4 3 5
b. 4 4 5.2
c. 5 4 5
d. 5 4 5.2
10. Complete the following Java statement to allow the instance of the Scanner class to read keyboard input.
Scanner keyboard = new Scanner(__________);
a. System.in
b. System.out
c. System.keyboard
d. System.input
11. What will happen when you attempt to compile and run the following code?
public class MyClass
{
public static void main(String[] argv)
{
//call doSomething();
}
private void doSomething()
{
for (int i = 0; i < 10; i++)
{
System.out.println("Value of i = " + i);
}
}
}
a. A compile time error indicating that no method is being called in the program
b. A run time error indicating that there is an infinite loop
c. Successful compile and at run time the values 0 to 9 are printed out
d. Successful compile but no output at runtime
12. What is the output of the following code segment?
int i;
double d = 12.5;
i = d;
System.out.print(i);
a. 12
b. This code will produce a compile time error
c. 13
d. This code will produce a runtime error
Use the following class for the questions 13 and 14:
public class Person {
public static final String school = "Purdue University";
private int studentNumber;
private String fullName;
public double GPA;
public int getNumber()
{
return studentNumber;
}
public void setName(String name)
{
this.fullName = name;
}
public void setNumber(int num)
{
this.studentNumber = num;
}
} // end class
13. Which of the following refers to a mutator method?
a. private int studentNumber
b. public int getNumber()
c. public void setName()
d. public static final String school
14. What will be printed to the screen given this code segment:
Person student = new Person();
student.setNumber(100);
System.out.println(student.getNumber());
a. studentNumber
b. 100
c. 0
d. nothing will be printed - this will generate an error
15. What will the following code output?
String s = "100";
System.out.print(s + 100 + " ");
System.out.print(s + "100" + " ");
System.out.print(s + "\"100\"");
a. 100 200 "100"
b. 100 100 "100"
c. 100100 100100 100"100"
d. 100 100 100"100"
16. Which applet method should be used to draw figures and use the drawString() method?
a. init
b. paint
c. main
d. start
17. How many times will "Hello World" be printed out?
int count = 0;
for (int i = 0; i < 5; i++)
count++;
System.out.println("Hello World");
a. 0
b. 1
c. 4
d. 5
18. What does this method return?
public void computeFactorial(int n)
{
int total = 1;
while(n > 0)
{
total *= n;
n--;
}
System.out.println(n);
}
a. nothing
b. an int
c. a String
d. n!
19. Let p1 and p2 be objects of type Piano. Suppose that you want to know if p1 and p2 contain the same information. Is there anything wrong with the following way of checking? Why or why not?
if (p1 == p2)
{
System.out.println("Equal!");
}
else
{
System.out.println("Not Equal!");
}
a. Yes, that checks to see if the memory locations are equal, not the objects.
b. No, the == operator is how things are supposed to be compared in Java.
c. Yes, p1 and p2 must be compared using a switch statement only.
d. Yes, you should test if p1.name == p2.name.
20. What will this code print to the console?
public static void main(String[] args)
{
String message = "Hi everyone!";
setMessage(message);
System.out.println(message);
}
public static void setMessage(String msg)
{
boolean hello = false;
if (hello == false)
{
msg = "Goodbye!";
System.out.println(msg);
}
else
{
msg = "CS180 rocks!";
System.out.println(msg);
}
}
a. CS180 rocks!
Hi everyone!
b. Goodbye!
Hi everyone!
c. Goodbye!
Goodbye!
d. Hi everyone!
Hi everyone!
Programming Questions:
1. (10 pts) Complete the TODO's in the skeleton code below:
public class Student
{
private String name;
private int age;
public String getName()
{
// TODO: return the name of the student
}
public void setAge(int n)
{
// TODO: set the age of the student to value of the argument
}
public String toString()
{
// TODO: return a String object of the form:
// STUDENT NAME: Purdue Pete, AGE: 136
}
public boolean sameName(Student soAndSo)
{
// TODO: return true if the student has the same name
// return false otherwise
}
}
2. (10 pts) Write a complete Java class called “WeightConverter” that converts weight in pounds to weight in kilograms, using the formula:
weightKilo = weightPound * 16 * 28.3495 / 1000
Your program should prompt the user to enter an integer weight in pounds (just a whole number, without any fractional part), and then let the program print out the equivalent weight in kilograms, including the fractional part. A sample dialog may be:
Enter an integer weight in pounds: 120
120 pounds = 54.43104 kilograms
3. (10 pts) Write a complete Java class called “ReverseDigit” that reads in a five digit number (such as 47906) and outputs the number, one digit per line, in reverse order:
6
0
9
7
4
Your prompt should tell the user to enter a five-digit number. You can assume that the user follows directions. Your program will NOT read the number as a value of type int. You must read and store the 5-digit integer as a String. Also, do not convert the string into an integer.
4. (15 pts) Assume principal to be the original investment, rate to be the annual interest rate, and n to be the number of investment years, compounding interest can give you the amount at the end of the nth year according to the following formula:
amount = principal * (1 + rate)n
Write a complete java class called “CICalculator” that prompts the user for the initial principal and the annual interest rate, print out the amount at the end of each year for 20 years. Here is a sample execution:
Enter a real number for initial investment: 1000.0
Enter the annual interest rate: 0.05
0 1000.0
1 1050.0
2 1102.5
3 1157.62500
...
5. (15 pts) Write a complete java class called "Piggybank" that will help someone keep track of the amount of money they have in their piggybank. This class should include variables to keep track of the number of quarters, the number of dimes, the number of nickels, and the number of pennies (there will be no paper money or other coins).
You should include mutator methods for all variables. In addition to these methods, you should have one method called getTotal() that returns an integer which is the total amount of money (in cents) that the piggy bank contains. For example, if you have 3 quarters, 2 dimes, 4 nickels, and 3 pennies, getTotal() will return 118.
Note:
- You do not need to include an input method.
- Make sure to use appropriate names for all variables and methods.
- You should be sure to set methods and variables to either public or private (choose the right one for the method or variable).
- One quarter is 25 cents, one dime is 10 cents, one nickel is 5 cents, and one penny is 1 cent.
................
................
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 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