CSE 110 - Exam # 2
1
Last Name (print) __________________ First Name _____________Class meets at: 10:45AM
Form 1
Please READ this before you start.
You are allowed to use a calculator and one sheet of notes. You are to complete all questions on your own. Anyone
caught cheating on this exam will receive a score of zero and will be reported to the School of Engineering Dean's
Office for appropriate disciplinary action. Write your answers legibly on the attached answer sheet; you may write
on the exam if you wish but no answers written on the exam will be graded. Also be sure to sign the academic
integrity pledge on the answer sheet. Note that if the instructor or TA cannot read your handwriting your answers may
be graded incorrectly. You have 50 minutes to complete the exam.
Rip the exam sheet from the answer sheet and put your answers on the answer sheet only.
Good luck!
Quick Reference:
class java.lang.String
char charAt(int index) // Returns the character at the specified
index.
int compareTo(String anotherString) // Compares two string
lexicographically.
int length( ) //Returns the length of this string.
String substring(int from , int to ) //Returns a new string that is
a substring of this string.
String concat (String str) // returning a new string
String replace(char oldChar, char newChar) // returns a new string
replacing oldChar with newChar
String substring(int from) //Returns substring(from, length())
String toLowerCase( ) //returns the string in lowercase
String toUpperCase() // returns the string in uppercase
int indexOf(String s) // returns the index of the first occurrence
of s, returns –1 if not found
class java.util.Scanner
String next() // reads the next string from the keyboard
double nextDouble()
int nextInt()
String nextLine() // reads the next whitespace – delimited string,
floating point value, integer or line and returns the value that was
read
CSE 110 - Exam # 2
2
MULTIPLE CHOICE (3 points each, choose the ONE correct answer) Write your choice in the answer sheet
provided.
1. Which of the following reserved words in Java is used to create an instance (object) of a class?
a) class
b) public
c) public or private, either could be used
d) import
e) new
2. The relationship between a class and an object is best described as
a) classes are instances of objects
b) objects are instances of classes
c) objects and classes are the same thing
d) classes are programs while objects are variables
3. In a UML diagram for a class:
a) classes are represented as rectangles,
b) there may be a section containing the name of the class
c) there may be a section containing the attributes (data) of the class
d) there may be a section containing the methods of the class
e) all of the above
4. In Java, “instantiation” means
a) Noticing the first time something is used
b) Creating a new object
c) Creating a new alias to an existing object
d) Launching a method
e) None of the above
5. A variable whose scope is restricted to the method where it was declared is known as a(n)
a) parameter
b) global variable
c) local variable
d) public instance data
e) private instance data
6. The do loop differs from the while loop in that
a) the while loop will always execute the body of the loop at least once
b) the do loop will always execute the body of the loop at least once
c) the do loop will continue to loop while condition in the while statement is false and the while loop
will continue to loop while the condition in the while statement is true
d) the while loop will continue to loop while condition in the while statement is false and the do loop
will continue to loop while the condition in the while statement is true
e) none of the above, there is absolutely no difference between the two types of loops
7. Given the following code, where x = 0, what is the resulting value of x after the for-loop terminates?
for (int i=0;i<=5;i++)
x += i;
a) 0
b) 4
c) 5
d) 10
e) 15
CSE 110 - Exam # 2
3
8. Given that s is a String, what does the following loop do? (1 pt)
for (int j = s.length( ); j > 0; j--)
System.out.print(s.charAt(j-1));
a) it prints s out backwards
b) it prints s out forwards
c) it prints s out backwards after skipping the last character
d) it prints s out backwards but does not print the 0th character
9. Which of the following methods is a static method? The class in which the method is defined is given after the dash
following the method name.
a) equals - String
b) toUpperCase - String
c) sqrt - Math
d) format - DecimalFormat
10. How many times will the following loop iterate?
int x = 10;
do {
System.out.println(x);
x--;
} while (x > 0);
a) 0 times
b) 1 time
c) 9 times
d) 10 times
e) 11 times
11. What is the output generated by the following code fragments.
int n = 64;
int j = 1;
while( j <= n )
{
j = 2 * j;
}
System.out.println ( "j: " + j);
a) j: 2
b) j: 64
c) j: 128
d) j: 256
12. The following nested loop structure will execute the inner most statement (x++) how many times?
for (int j = 0; j < 100; j++)
for (int k = 100; k > 0; k--)
x++;
a) 50
b) 100
c) 200
d) 1,000
e) 10,000
13. In Java a variable may contain
a) A value or a reference
b) A package
c) A method
d) A class
e) Any of the above
CSE 110 - Exam # 2
4
For next 3 questions, use the following class definition
import java.text.DecimalFormat;
public class Student
{
private String name;
private String major;
private double gpa;
private int hours;
public Student(String newName, String newMajor, double newGPA, int newHours)
{
name = newName;
major = newMajor;
gpa = newGPA;
hours = newHours;
}
public String toString( )
{
DecimalFormat df = new DecimalFormat("xxxx"); // xxxx needs to be replaced
return name + "\n" + major + "\n" + df.format(gpa) + "\n" + hours
}
}
14. Which of the following could be used to instantiate a new Student s1?
a) Student s1 = new Student( );
b) s1 = new Student( );
c) Student s1 = new Student("Jane Doe", "Computer Science", 3.333, 33);
d) new Student s1 = ("Jane Doe", "Computer Science", 3.333, 33);
e) new Student(s1);
15. Assume that another method has been defined that will compute and return the student’s class rank (Freshman,
Sophomore, etc). It is defined as: public String getClassRank( )
Given that s1 is a student, which of the following would properly be used to get s1’s class rank?
a) s1 = getClassRank( );
b) s1.toString( );
c) s1.getHours( );
d) s1.getClassRank( );
e) getClassRank(s1);
16. Another method that might be desired is one that updates the Student’s number of credit hours. This method will
receive a number of credit hours and add these to the Student’s current hours. Which of the following methods
would accomplish this?
a) public int updateHours( )
{ return hours; }
b) public void updateHours( )
{ hours++; }
c) public updateHours(int moreHours)
{ hours += moreHours; }
d) public void updateHours(int moreHours)
{ hours += moreHours; }
CSE 110 - Exam # 2
5
17. Which of the following would not be considered an algorithm?
a) pseudocode
b) a shopping list
c) travel direction
d) a recipe
e) a computer program
18. If x is an int where x = 0, what will x be after the following loop terminates?
while (x < 100)
x *= 2;
a) 2
b) 64
c) 100
d) 128
e) None of the above, this is an infinite loop
19. Examine the code in each row. Decide if the printed output will be true or false.
Point pointA = new Point(0, 0 );
Point pointB = new Point( 0, 0 );
System.out.println( pointA == pointB );
a) true
b) false
20. Consider a method defined with the header: public void foo(int a, int b). Which of the following
method calls is legal?
a) foo(0, 0.1);
b) foo(0 / 1, 2 * 3);
c) foo(0);
d) foo( );
e) foo(1 + 2, 3 * 0.1);
21. Write a method called countSpaces that takes in a String as a parameter and returns an integer that
represents the number of space characters (' ') contained in the string, zero otherwise. (10 pts)
CSE 110 - Exam # 2
6
22. For questions a, b, c: Assume this Triangle class: (18 pts)
public class Triangle {
private int side1, side2, side3;
public Triangle (int s1, int s2, int s3) {
side1 = s1;
side2 = s2;
side3 = s3;
} // constructor Triangle
public int findPerimeter()
{
return side1 + side2 + side3;
}
public String toString() {
return (side1 + " " + side2 + " " + side3) ;
} // method toString
a. Write the statement to instantiate a Triangle object with sides 10, 15, and 9.
b. Write a statement to output the perimeter of the triangle from question a to the screen.
c. Write a method that can be added to the class Triangle called shortest that returns the length of the
shortest side of the triangle.
23. For each of the following pairs, which represents a class and which represents an object of that class?
Write Class or Object in the space provided on the answer sheet. (6 pts)
Student _____________ David _______________
Cat ______________ Pet _______________
Vehicle ______________ Car ______________
24. (6 pts) Write the output generated by the following code fragments.
int n1 = 10;
int n2 = 1000;
while( n1 * n1 <= n2 )
{
System.out.println( n1 + " " + n2 );
n1 = n1 + 1;
n2 = n2 - 200;
}
int n =3;
for(int outer = 1; outer <= n; outer++ )
{
for( int inner = 1; inner <= outer; inner++ )
{
System.out.print( inner + " " );
}
System.out.println( );
}
CSE 110 - Exam # 2
7
ANSWER SHEET Exam 2 - CSE110 – Form 1
Last Name Printed _________________________First Name______________ Class meets at: 10:45AM
Lab Letter______
Note: You sign your name in the blank below to verify that you did not violate the ASU Student Academic
Integrity Policy during this exam. Any answer sheets that are returned to the instructor without a signature
will not be graded, and the student will be given a score of 0.
My signature ________________________________________ attests that I completed this exam on my
own, and that in doing so I did not violate the Arizona State University Student Academic Integrity Policy. I
understand that if I am detected while cheating that I will receive a score of 0.
1. __________
2. ___________
3. ___________
4. ___________
5. ____________
6. ___________
7. ____________
8. ____________
9. ____________
10. ___________
11. ___________
12. ___________
13. ___________
14. ___________
15. ___________
16. ____________
17. ____________
18. ____________
19. ____________
20. ____________
ASUID:
CSE 110 - Exam # 2
8
21. //write the method here
22.
a. //one statement _______________________________________________________________
b. //one statement _______________________________________________________________
c. // write the method shortest
23.
Student _____________ David _______________
Cat ______________ Pet _______________
Vehicle ______________ Car ______________
24.
a) Show the output:
b) Show the output.
Powered by TCPDF (www.tcpdf.org)