CS 177 Spring 2005 Exam I



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? 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!

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

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

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.

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

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 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download