Download CS 177 Spring 2005 Exam I

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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: _________________________________________
1
Multiple Choice Questions (2 points each):
1. Which of the following lines will compile without warning or error.
a.
b.
c.
d.
char c = "a";
byte b = 257;
boolean b = null;
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.
b.
c.
d.
error: static context cannot reference non-static aMethod.
error: method main not correct
error: array must include parameter
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 <= 40) ? (hoursWorked * payRate) :
(40 * payRate + 1.5 * (hoursWorked - 40) * payRate);
a.
b.
c.
d.
650
400
500
430
◄◄◄
4. The basic idea of ________ is that it allows the same program instruction to
mean different things in different contexts.
a.
b.
c.
d.
polymorphism
◄◄◄
encapsulation
object oriented programming
inheritance
2
5. What will happen when you attempt to compile and run the following code?
public class MySwitch
{
public static void main(String[] argv)
{
MySwitch ms = new MySwitch();
ms.aMethod();
}
public void aMethod()
{
int k=10;
switch(k)
{
case 10:
System.out.println("ten");
case 20:
System.out.println("twenty");
break;
default: //Put the default at the bottom, not here
System.out.println("This is the default output");
break;
}
}
}
a.
b.
c.
d.
Compile
Compile
Compile
None of
time error target of switch must be an integral type
and run with output "This is the default output"
and run with output "ten"
these options
◄◄◄
6. What output is produced by the following?
int time = 4, tide = 2;
if ( time + tide > 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.
b.
c.
d.
"Time and tide wait for no one."
"Time and tide wait for me."
◄◄◄
"The tide cannot hurt Purdue Pete."
"Paper covers rock."
7. A mutator method is a method that:
a.
b.
c.
d.
allows you to change the value of an instance variable
reads and returns the value of an instance variable
prints to the screen the value of an instance variable
mutator methods do not exist
3
◄◄◄
8. Which of the following is not a primitive type in Java?
a.
b.
c.
d.
byte
short
long
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.
b.
c.
d.
4
4
5
5
3
4
4
4
5
5.2
5
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.
b.
c.
d.
System.in
◄◄◄
System.out
System.keyboard
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
◄◄◄
4
12. What is the output of the following code segment?
int i;
double d = 12.5;
i = d;
System.out.print(i);
a.
b.
c.
d.
12
This code will produce a compile time error
13
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.
b.
c.
d.
private int studentNumber
public int getNumber()
public void setName()
◄◄◄
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.
b.
c.
d.
studentNumber
100
◄◄◄
0
nothing will be printed - this will generate an error
5
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.
b.
c.
d.
100 200 "100"
100 100 "100"
100100 100100 100"100"
100 100 100"100"
◄◄◄
16. Which applet method should be used to draw figures and use the drawString()
method?
a.
b.
c.
d.
init
paint
main
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.
b.
c.
d.
0
1
4
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.
b.
c.
d.
nothing
an int
a String
n!
◄◄◄
6
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.
b.
c.
d.
Yes, that checks to see if the memory locations are equal, not the objects.
No, the == operator is how things are supposed to be compared in Java.
Yes, p1 and p2 must be compared using a switch statement only.
Yes, you should test if p1.name == p2.name.
◄◄◄
20. What will this code print to the console? Note the local variables.
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!
7
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
return name;
}
public void setAge(int n)
{
// TODO: set the age of the student to value of the argument
age = n;
}
public String toString()
{
// TODO: return a String object of the form:
//
STUDENT NAME: Purdue Pete, AGE: 136
return "STUDENT NAME: " + name + ", AGE: " + age;
}
public boolean sameName(Student soAndSo)
{
// TODO: return true if the student has the same name
//
return false otherwise
return (name.equals(soAndSo.getName());
}
} // end class
8
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
import java.util.*;
public class WeightConverter
{
public static void main(String[] argv)
{
int pounds;
double kg;
Scanner keyboard = new Scanner(System.in);
// read in weight in pounds
System.out.print("Enter an integer weight in pounds: ");
pounds = keyboard.nextInt();
// convert to kilograms and display
kg = pounds * 16 * 28.3495 / 1000;
System.out.println(pounds + " pounds = " + kg + " kilograms");
} // end main
} // end class
9
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 5digit integer as a String. Also, do not convert the string into an integer.
import java.util.*;
public class ReverseDigit
{
public static void main(String[] argv)
{
String inputNumber;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a 5-digit number: ");
inputNumber = keyboard.nextLine();
for(int i=4; i>=0; i--)
{
System.out.println(inputNumber.charAt(i));
}
} // end main
} // end class
10
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
...
import java.util.*;
public class CICalculator
{
public static void main(String[] argv)
{
double principal;
double rate;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a real number for initial investment: ");
principal = keyboard.nextDouble();
System.out.print("Enter the annual interest rate: ");
rate = keyboard.nextDouble();
for(int i=0; i<=20; i++)
{
System.out.println(i + "
}
} // end main
" + principal*power(1+rate,i));
public static double power(double base, double n)
{
double total=1;
for(int i=0; i<n; i++)
total*=base;
return total;
} // end power
} // end class
11
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.
public class
{
private
private
private
private
Piggybank
int
int
int
int
numQuarters;
numDimes;
numNickels;
numPennies;
public void setQuarters(int q)
{
this.numQuarters = q;
}
public void setDimes(int d)
{
this.numDimes = d;
}
public void setNickels(int n)
{
this.numNickels = n;
}
public void setPennies(int p)
{
this.numPennies = p;
}
public int getTotal()
{
return 25*numQuarters + 10*numDimes +
5*numNickels + 1*numPennies;
}
} // end class
12