Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Pleaseeeee, Help please! 1.(O1) What is the result of the following code? Person

ID: 3765179 • Letter: P

Question

Pleaseeeee, Help please!

1.(O1) What is the result of the following code?

Person a;
Person b;
a = new Person("Clark Kent");
b = a;
a.changeName("Superman");
System.out.println(b.getName());

(a)The number “Clark Kent” is returned

(b)The number “Superman” is returned

(c)“Clark Kent” appears on the screen

(d)“Superman” appears on the screen

(e)Nothing

2.(O1) What is printed by the following code segment?

int s = 0;

for (int i = 1; i < 5; i++)

s += i;

System.out.println(s);

(a)4

(b)5

(c)10

(d)15

(e)None of the above

3 (O1) What will be the value of a after the assignment statement below? Assume b=5 and c=10.

int a = b * (-c + 2) / 2;

30

-30

20

-20

-6

4(O1) The following nested loop structure will execute the inner most statement (x++) how many times?

for (int j = 0; j < 4; j++)

for (int k = 12; k > 0; k--)

x++;

0

4

12

16

48

5(O1) How many times does the following loop iterate?

int x = 10;

while (x > 0) {

System.out.println(x);
x--;

}

0

1

9

10

11

6.(O1) Consider a method defined with header: . Which of the following method calls is legal?

absolute(-5.5);

absolute(true);

absolute(“-102.5”);

absolute(‘d’)

absolute(“4.5” + “-9.5”);

7(O1) Which of the statements below will add 1 to x if x is positive, subtract 1 from x if it is negative, and do nothing if x is 0?

if (x > 0)     (b) if (x > 0)        (c) if (x > 0)
   x++;              x++;                 x++;
else              else if (x < 0)     else (x < 0)
   x--;              x--;                 x--;

8.(O1) Which of the following is true if an integer is the age of a teenager?

(x > 12 && x < 20)

(x > 12 || x < 20)

(x >= 13 && x <= 19)

(x >= 13 || x <= 19)

(a)Both (1) and (2)

(b)Both (3) and (4)

(c)Both (1) and (3)

(d)Both (2) and (4)

(e)None of the above

9.(O2) What are the three components of a class

(a)Variables, methods, if statements

(b)Fields, constructors, methods

(c)Visibility modifier, type, name

(d)Fields, components, methods

(e)Void, int, double

10.(O2) The behavior of an object is defined by the object’s

(a)Visibility modifiers

(b)Constructors

(c)Instance data

(d)Methods

(e)None of the above

11.(O2) The relationship between a class and an object is best described as:

(a)Objects and classes are the same thing

(b)Classes are programs while objects are variables

(c)Objects are the instance data of classes

(d)Objects are instances of classes

(e)Classes are instances of objects

12.(O2) To define a class that will represent a car, which of the following definitions is most appropriate?

(a)

(b)

(c)

(d)

(e)

13.(O3) What is the best way to allow someone to change the state of an object?

(a)Make all the fields publicly accessible

(b)Create a method which gives a user a standard interface to change the values, but conceals the details of modifying the field.

(c)Make the user execute the constructor every time he or she wants new values.

(d)Have a series of if statements than make the user select a new value based on different conditions.

(e)Program a method for every possible value and have the user call that method when s/he wants the field variable to be a specific value.

14.(O3) These two ways of setting up a string yield identical results:

String s = new String(“string”);
String s = “string”;

(a)True

(b)False

15.(O4) Which of the following statements reads in a line of input from a user:

Scanner s = new Scanner(System.in); s.next();

Scanner s = new Scanner(System.in); s.nextLine();

Scanner s = new Scanner(“System.in”); s.next();

Scanner s = new Scanner(“System.in”); s.nextLine();

(e)None of the above

16.(O4) Which of the following statements is needed to use the class:

import java.util.Random;

import Random;

(c)Both (a) and (b)

(d)Nothing

(e)None of the above

17.(O4) Which of the following statements generates a random number between 10 and 15 (inclusive)?

r.nextInt(-6) + 20;

r.nextInt(-5) + 20;

r.nextInt(5) + 10;

r.nextInt(6) + 10;

(e)None of the above

18.(O5) What will the following line of Java code do?

// System.out.println(“Hello”);

(a)Do nothing

(b)Cause “Hello” to be output

(c)Cause a syntax error

(d)Cause “(Hello)” to be output

(e)Cause the program to crash

19.(O5) If a method does not have a return statement, then

(a)It must be defined to be a public method

(b)It will produce a syntax error when compiled

(c)It must be an int, double, float, or String method

(d)It must be a void method

(e)It cannot be called from outside the class that defined the method

For the following questions consider the following consider the following partialBoxCar and BoxCarPart class definitions:

public class BoxCar

{

   private BoxCarPart leftPart;

   private BoxCarPart rightPart;

   // ...

}

public class BoxCarPart

{

   private Square box;

   private Circle wheel;

  

   // ...

}

The Square and Circle classes also each have a setColor method with the following method signature: public void setColor(Color newColor).

20.(O3) What two statements would be needed inside of a method for the class. Assume this method has the following method signature:

public void setColor(Color boxColor, Color wheelColor)

(a)

wheel.setColor(wheelColor);

(b)

wheelColor = Color.blue;

(c)

wheel = wheelColor;

(d)

wheelColor.setColor(wheel);

(e)

wheel.setColor = wheelColor;

21.(O3) Which of the following is a valid call to the method above inside of the class.

(a)

(b)

(c)

(d)

(e)

For the following questions consider the following partial class:

public class Student

{

private String name;

private String major;

private double gpa;

private int hours;

public Student(String newName, String newMajor, int newHours)

{

name = newName;

major = newMajor;

gpa = 0.0;

hours = newHours;

}

public String toString( )

{

DecimalFormat df = new DecimalFormat(0.00);

return name + + major + + df.format(gpa) + + hours

}

}

22.(O3) Which of the following could be used to instantiate a new ?

(a)

(b)

(c)

(d)

(e)

23. (O3) 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 could be used to get s1’s class rank?

(a)

(b)

(c)

(d)

(e)

24(O4) What would be the proper variable declaration and instantiation for a list of Student objects?

ArrayList<Student> students;

ArrayList<String> students = new ArrayList<Student>;

ArrayList<Student> students = new ArrayList<Student>();

ArrayList<String> students = new ArrayList<String>;

ArrayList<Student> students = new ArrayList<String>();

25(O5) 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?

public int updateHours( )

{    return hours; }

public void updateHours( )

{    hours++; }

public updateHours(int moreHours)

{    hours += moreHours; }

public void updateHours(int moreHours)

{    hours += moreHours; }

public int updateHours(int moreHours)

{    return hours + moreHours; }

public class BoxCar

{

   private BoxCarPart leftPart;

   private BoxCarPart rightPart;

   // ...

}

public class BoxCarPart

{

   private Square box;

   private Circle wheel;

  

   // ...

}

Explanation / Answer

1. d
2. c
3. -20
4. 48
5. 10

7. a
8. c
9. b
10. d
11. d
12.
13. e
14. a
15. b
16. a
17. d
18. a
19. d
20.
24. a and c
25. d