Chapter 4: Writing Classes / Mid Term
Test Name:______
Multiple Choice Questions:
1)The behavior of an object is defined by the object’s
a)instance data
b)constructor
c)visibility modifiers
d)methods
e)all of the above
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
e)objects are the instance data of classes
3)To define a class that will represent a car, which of the following definitions is most appropriate?
- private class car
- public class car
- public class Car
- public class CAR
- private class Car
4)Which of the following reserved words in Java is used to create an instance of a class?
- class
- public
- public or private, either could be used
- import
- new
5)In order to preserve encapsulation of an object, we would do all of the following except for which one?
- Make the instance data private
- Define the methods in the class to access and manipulate the instance data
- Make the methods of the class public
- Make the class final
- All of the above preserve encapsulation
6)If a method does not have a return statement, then
- it will produce a syntax error when compiled
- it must be a void method
- it can not be called from outside the class that defined the method
- it must be defined to be a public method
- it must be an int, double, or String method
7)Consider a sequence of method invocations as follows: main calls m1, m1 calls m2, m2 calls m3 and then m2 calls m4, m3 calls m5. If m4 has just terminated, what method will resume execution?
- m1
- m2
- m3
- m5
- main
8)A variable whose scope is restricted to the method where it was declared is known as a(n)
- parameter
- global variable
- local variable
- public instance data
- private instance data
9)A class’ constructor usually defines
- how an object is initialized
- how an object is interfaced
- the number of instance data in the class
- the number of methods in the class
- if the instance data are accessible outside of the object directly
10)Having multiple class methods of the same name where each method has a different number of or type of parameters is known as
- encapsulation
- information hiding
- tokenizing
- importing
- method overloading
11)Instance data for a Java class
- are limited to primitive types (e.g., int, double, char)
- are limited to Strings
- are limited to objects(e.g., Strings, classes defined by other programmers)
- may be primitive types or objects, but objects must be defined to be private
- may be primitive types or objects
12)An example of passing a message to a String where the message has a String parameter occurs in which of the following messages?
- length
- substring
- equals
- toUpperCase
- none of the above, it is not possible to pass a String as a parameter in a message to a String
For questions 13-15, 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
}
}
13)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);
14)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);
15)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;
}
e)public int updateHours(int moreHours)
{
return hours + moreHours;
}
16)A set of code has already instantiated c to be a Coin and has input a String guess, from the user asking whether the user guesses that a coin flip will result in “Heads” or “Tails”. Which of the following sets of code will perform the coin flip and see if the user’s guess was right or wrong?
a) c.flip( );
if(c.isHeads( ).equals(guess)) System.out.println("User is correct");
b) if(c.flip( ).equals(guess)) System.out.println("User is correct");
c) if(c.isHeads( ).equals(guess)) System.out.println("User is correct");
d) c.flip( );
if(c.toString( ).equals(guess)) System.out.println("User is correct");
e) c.flip( ).toString( );
if(c.equals(guess)) System.out.println("User is correct");
17)What does the following code compute?
int num = 0;
for(int j = 0; j < 1000; j++)
{
c.flip( );
if(c.isHeads()) num++;
}
double value = (double) num / 1000;
a)the number of Heads flipped out of 1000 flips
b)the number of Heads flipped in a row out of 1000 flips
c)the percentage of heads flipped out of 1000 flips
d)the percentage of times neither Heads nor Tails were flipped out of 1000 flips
e)nothing at all
18)In the Rational class, defined in chapter 4, the methods reduce and gcd are declared to be private. Why?
a)Because they will never be used
b)Because they will only be called from methods inside of Rational
c)Because they will only be called from the constructor of Rational
d)Because they do not use any of Rational’s instance data
e)Because it is a typo and they should be declared as public
Use the following information to answer questions 19 - 20. The Die class from chapter 4 has two constructors defined as follows. Assume MIN_FACES is an int equal to 4.
public Die( ) public Die(int faces)
{{
numFaces = 6;if(faces < MIN_FACES) numFaces = 6;
faceValue = 1;else numFaces = faces;
}faceValue = 1;
}
19)The instruction Die d = new Die(10); results in
a)The Die d having numFaces = 6 and faceValue = 1
b)The Die d having numFaces = 10 and faceValue = 1
c)The Die d having numFaces = 10 and faceValue = 10
d)The Die d having numFaces = 6 and faceValue = 10
e)A syntax error
20)The instruction Die d = new Die(10, 0); results in
a)The Die d having numFaces = 6 and faceValue = 1
b)The Die d having numFaces = 10 and faceValue = 1
c)The Die d having numFaces = 10 and faceValue = 10
d)The Die d having numFaces = 6 and faceValue = 10
e)A syntax error
For questions 21 - 23, use the following class definition:
public class Swapper
{
private int x;
private String y;
public int z;
public Swapper(int a, String b, int c)
{
x = a;
y = b;
z = c;
}
public String swap( )
{
int temp = x;
x = z;
z = temp;
return y;
}
public String toString( )
{
if (x < z) return y;
else return "" + x + z;
}
}
21)If the instruction Swapper s = new Swapper(0, "hello", 0); is executed followed by s.toString( ); what value is returned from s.toString( )?
a)"hello"
b)"hello00"
c)"00"
d)"0"
e)0
22)Which of the following criticisms is valid about the Swapper class?
a)The instance data x is visible outside of Swapper
b)The instance data y is visible outside of Swapper
c)The instance data z is visible outside of Swapper
d)All 3 instance data are visible outside of Swapper
e)None of the methods are visible outside of Swapper
23)If we have Swapper r = new Swapper (5, "no", 10); then r.swap( ); returns which of the following?
a)nothing
b)"no"
c)"no510"
d)"510"
e)"15"
24)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);
25)Consider a method defined with the header: public void doublefoo(double x). Which of the following method calls is legal?
a)doublefoo(0);
b)doublefoo(0.555);
c)doublefoo(0.1 + 0.2);
d)doublefoo(0.1, 0.2);
e)all of the above are legal except for d
Question 26 refers to the following method:
public int dogYears (int age)
{
if (age >= 0)
return age * 7;
else
return 0;
}
26)What would be an appropriate precondition for the method?
a)// Precondition: tells how old your dog is
b)// Precondition: age is someone’s age
c)// Precondition: age >= 0
d)// Precondition: age is an int
e)// Precondition: returns age * 7
27)What is a mutator method?
a)A method that modifies a value.
b)A method that provides read-only access to a value.
c)A method that has the same name, but different parameters, as another method.
d)A method that is called when an object is first created.
e)A method that does not return a value.
True/False Questions:
28)Java methods can return only primitive types (int, double, boolean, etc).
29)Formal parameters are those that appear in the method call and actual parameters are those that appear in the method header.
30)All Java classes must contain a main method which is the first method executed when the Java class is called upon.
31)Java methods can return more than one item if they are modified with the reserved word continue, as in public continue int foo( ) { … }
32)The following method header definition will result in a syntax error: public void aMethod( );
33)A method defined in a class can access the class’ instance data without needing to pass them as parameters or declare them as local variables.
34)The interface of a class is based on those data instances and methods that are declared public.
35)Defining formal parameters requires including each parameters type.
36)Every class definition must include a constructor.
37)While multiple objects of the same class can exist, there is only one version of the class.
38)A class may contain methods but not variable declarations.
39)A constructor is a method that gets called automatically whenever an object is created, for example with the new operator.
40)A constructor must have the same name as its class.
41)A constructor must always return an int.
42)A class’s instance data are the variables declared in the main method.
43)The methods in a class should always be made public so that those outside the class can use them
44)The body of a method may be empty.
45)The return statement must be followed a single variable that contains the value to be returned.
46)The number and types of the actual parameters must match the number and types of the formal parameters.
47)The different versions of an overloaded method are differentiated by their signatures.
48)If a method takes a double as a parameter, you could pass it an int as the actual parameter.
49)A method defined without a return statement will cause a compile error.
50)The println method on System.out is overloaded.
51)Method decomposition is the process of creating overloaded versions of a method that do the same thing, but operate on different data types.
52)An object may be made up of other objects.
53)In a method with both a precondition and a postcondition, the postcondition should be true when the method finishes executing regardless of whether the precondition was true.
54)An assertion represents an assumption the programmer makes about a program.
55)An accessor method provides access to a value and allows the caller to change that value.
Multiple Choice Questions Chapters 1 – 3:
56)Consider the following outline of a nested if-else structure which has more if clauses than else clauses. Which of the statements below is true regarding this structure?
if (condition1)
if (condition2)
statement1;
else statement2;
a)syntactically it is invalid to have more if clauses than else clauses
b)statement2 will only execute if condition1 is false and condition2 is false
c)statement2 will only execute if condition1 is true and condition2 is false
d)statement2 will only execute if condition1 is false, it does not matter what condition2 is
e)statement2 will never execute
57)Which memory capacity is the largest?
a)1,500,000,000,000 bytes
b)100 gigabytes
c)3,500,000 kilobytes
d)10 terabyte
e)12,000,000 megabytes
58)The ability to directly obtain a stored item by referencing its address is known as
a)random access
b)sequential access
c)read-only access
d)fetch access
e)volatility
59)A URL (Universal Resource Locator) specifies the address of
a)A computer on any network
b)A computer on the Internet
c)A local area network (LAN) on the Internet
d)A document or other type of file on the Internet
e)A java program on the Internet
60)Once we have implemented the solution, we are not done with the problem because
a)the solution may not be the best (most efficient)
b)the solution may have errors and need testing and fixing before we are done
c)the solution may, at a later date, need revising to handle new specifications
d)the solution may, at a later date, need revising because of new programming language features
e)all of the above
61)Which of the following would be a legal Java identifier?
a)i
b)class
c)ilikeclass!
d)idon'tlikeclass
e)i-like-class
62)Forgetting a semicolon will cause
a)a syntax error
b)a run-time error
c)a logical error
d)no error at all
e)converting the statement into a comment
63)Following Java naming convention, which of the following would be the best name for a class about store customers?
a)StoreCustomer
b)Store Customer
c)storeCustomer
d)STORE_CUSTOMER
e)Store-Customer
64)Which of the following characters does not need to have an associated “closing” character in a Java program?
a){
b)(
c)[
d)
e)all of these require closing characters
65)6 bits can be used to represent ___ distinct items or values
a)6
b)20
c)24
d)32
e)64
Use the following class definition to answer questions 66-69.
public class Questions66_69
{
public static void main(String[ ] args)
{
System.out.print("Here");
System.out.println("There " + "Everywhere");
System.out.println("But not" + "in Texas");
}
}
66)The program will print the word "Here" and then print
a)"There Everywhere" on the line after "Here"
b)"There" on the line after "Here" and "Everywhere" on the line after "There"
c)"There Everywhere" on the same line as "Here"
d)"ThereEverywhere" on the same line as "Here"
e)"ThereEverywhere" on the line after "Here"
67)The final println command will output
a)"But not in Texas"
b)"But notin Texas"
c)"But not" on one line and "in Texas" on the next line
d)"But not+in Texas"
e)"But not + in Texas"
68)Which of the following would not be considered an algorithm?
a)a recipe
b)a computer program
c)pseudocode
d)a shopping list
e)travel directions
69)How many lines of output are provided by this program?
a)1
b)2
c)3
d)4
e)5
70)A reasonable comment for this program might be
a)// a program that demonstrates the differences between print, println and how + works
b)// a program that outputs a message about Texas
c)// a program that demonstrates nothing at all
d)// a program that outputs the message “Here There Everywhere But not in Texas”
e)// a program that has three output statements in it
71)If you want to output the text "hi there", including the quote marks, which of the following could do that?
a)System.out.println("hi there");
b)System.out.println(""hi there"");
c)System.out.println("\"hi there");
d)System.out.println("\"hi there\"");
e)none, it is not possible to output a quote mark because it is used to mark the beginning and ending of the String to be output.
72)What value will z have if we execute the following assignment statement?
- double z = 5 / 10;
a)z will equal 0.0
b)z will equal 0.5
c)z will equal 5.0
d)z will equal 0.05
e)none of the above, a run-time error arises because z is a double and 5 / 10 is an int
73)What value will z have if we execute the following assignment statement?
int z = 50 / 10.00;
a)5
b)5.0
c)50
d)10
e)none of the above, a run-time error arises because z is an int and 50 / 10.00 is not
74)Volatility is a property of
a)RAM
b)ROM
c)disk
d)software
e)computer networks
75)A cast is required in which of the following situations?
a)using charAt to take an element of a String and store it in a char
b)storing an int in a double
c)storing a double in a double
d)storing a double in an int
e)all of the above require casts
76)Given the following assignment statement, which of the following answers is true regarding the order that the operators will be applied based on operator precedence?
- a = (b + c) * d / e – f;
a)*, /, +, -
b)*, +, /, -
c)+, *, /, -
d)+, /, *, -
e)+, -, *, /\
77)Consider having three String variables a, b and c. The statement c = a + b; can also be achieved by doing
a)c = a.length( ) + b.length( );
b)c = (int) a + (int) b;
c)c = a.concat(b);
d)c = b.concat(a);
e)c = a.plus(b);
78) Of the following if statements, which one correctly executes three instructions if the condition is true?
a) if (x < 0)
a = b * 2;
y = x;
z = a – y;
b) {
if (x < 0)
a = b * 2;
y = x;
z = a – y;
}
c) if { (x < 0)
a = b * 2;
y = x;
z = a – y ;
}
d) if (x < 0)
{
a = b * 2;
y = x;
z = a – y;
}
e) b, c and d are all correct, but not a
Given the nested if-else structure below, answer questions 88-90.
if (a > 0)
if (b < 0)
x = x + 5;
else
if (a > 5)
x = x + 4;
else
x = x + 3;
else
x = x + 2;
79) If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?
a) 0
b) 2
c) 3
d) 4
e) 5
80) If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?
a)0
b)2
c)3
d)4
e)5
81) 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)100
b)200
c)10,000
d)20,000
e)1,000,000
82)If x is currently 0, a = 1 and b = -1, what will x become after the above statement is executed?
b)0
c)2
d)3
e)4
f)5
83)How many times will the following loop iterate?
int x = 10;
while (x > 0)
{
System.out.println(x);
x--;
- }
b)0 times
c)1 time
d)9 times
e)10 times
f)11 times
84)Given a String, s, which is assumed to have at least one character in it, which of the following conditions would determine if the first character of the String is the same as the last character?
a)(s.charAt(0) == s.charAt(s.length( )))
b)(s.charAt(1) == s.charAt(s.length( )))
c)(s.charAt(0) == s.charAt(s.length( ) - 1))
d)(s.charAt(0) == s.charAt(s.length( ) + 1))
e) (s.charAt(0) == s.charAt(last))
85)What is wrong, logically, with the following code?
if (x > 10) System.out.println("Large");
else if (x > 6 & x <= 10) System.out.println("Medium");
else if (x > 3 & x <= 6) System.out.println("Small");
else System.out.println("Very small");
- There is no logical error, but there is no need to have (x <= 10) in the second conditional or (x <= 6) in the third conditional
b)There is no logical error, but there is no need to have (x > 6) in the second conditional or (x > 3) in the third conditional
c)The logical error is that no matter what value x is, “Very small” is always printed out
d)The logical error is that no matter what value x is, “Large” is always printed out
e)There is nothing wrong with the logic at all