Advertisement Upgrade to remove ads

Which of these packages is automatically loaded, so you don't need to import it?


1. java.applet
2. java.string
3. java.awt
4. java.lang

4. java.lang 100%

Named constants make your programs clearer and easier to maintain. To create a constant in Java that can be used in all the methods in your class, you'll normally use all of these modifiers except ____________ .


1. final
2. const
3. static
4. public

2. const 100%

Method definitions should be placed:


1. after the class it is used in
2. before the class it is used in
3. inside the Java class library
4. inside the body of your class

4. inside the body of your class 100%

Which of these shows the correct way to print a literal number?


1. System.out.println(123.75);
2. System.out.println({123.75});
3. System.out.println("123.75");
4. System.out.println('123.75');

1. System.out.println(123.75); 100%

The generic term for methods that calculate and reutrn a value is a ____________________.

Student Response Value Correct Answer Feedback
1. algorithm
2. procedure
3. subroutine
4. function

4. function 100%

public int minimum(int x, int y)
{
int smaller;

if (x < y)
smaller = x;
else
smaller = y;

return smaller;
}

Based on the above code, what would be the output of the statement

int s = minimum(5, minimum(3, 7));


1. 7
2. 3
3. There would be no output; this is not a valid statement.
4. 5

2. 3 100%

Suppose that alpha and beta are int variables. The statement alpha = beta++; is equivalent to the statement(s) ____.


1. alpha = beta;
beta = beta + 1; 100%
2. None of these
3. alpha = 1 + beta;
4. alpha = alpha + beta;

1. alpha = beta;
beta = beta + 1; 100%

If s is a String object, which of these relational expressions is illegal (that is, your code will not compile if you use it) :


A. s == "Hi"
B. s != "Hi"
C. s > "Hi"
D. All of these are legal

C. s > "Hi" 100%

If an account balance is $1,000 or more, you pay interest. If the balance is less than $1,000 you charge a fee. The best way to write this is to :


A. use two if statements
B. use a single if, else statement
C. use an if, else if, else statement
D. use a switch statement

B. use a single if, else statement 100%

In a for loop, which of the following is executed first?


1. logical expression
2. for loop statement
3. update expression
4. initial expression

4. initial expression 100%

A loop that searches for a particular value in input is called a(n) :


A. counted [or counting] loop
B. sentinel loop
C. data loop
D. endless loop

B. sentinel loop 100%

int x, y;

if (x > 5)
y = 1;
else if (x < 5)
{
if (x < 3)
y = 2;
else
y = 3;
}
else
y = 4;

Based on the code above, if the value of y is found to be 2, what is a possible value of x?


1. 3
2. 6
3. 2
4. 5

3. 2 100%

Given the following switch statement where x is an int:
switch (x)
{
case 3 : x += 1;
case 4 : x += 2;
case 5 : x += 3;
case 6 : x++;
case 7 : x += 2;
case 8 : x--;
case 9 : x++
}
If x is currently equal to 5, what will the value of x be after the switch statement executes?


A. 8
B. 9
C. 10
D. 11 100%
E. 12

D. 11 100%

What is the output of the following Java code?

int x = 55;
int y = 5;

switch (x % 7)
{
case 0:
case 1:
y++;
case 2:
case 3:
y = y + 2;
case 4:
break;
case 5:
case 6:
y = y - 3;
}

println(y);



1. None of these
2. 2
3. 8
4. 5

2. 2 100%

What is wrong, logically, with the following code?
if (x > 10)
println("Large");
else if (x > 6 && x <= 10)
println("Medium");
else if (x > 3 && x <= 6)
println("Small");
else
.println("Very small");


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

A. 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 100%

Which of the following is not a function of the break statement?


1. To skip the remainder of a switch structure
2. To eliminate the use of certain boolean variables in a loop
3. To ignore certain values for variables and continue with the next iteration of a loop
4. To exit early from a loop

3. To ignore certain values for variables and continue with the next iteration of a loop 100%

What is the output of the following Java code?

int count = 1;
int num = 25;
while (count < 25)
{
num = num - 1;
count++;
}
println(count + " " + num);


1. 24 0
2. 25 0
3. 25 1
4. 24 1

3. 25 1 100%

Which of the following statements creates alpha, an array of 5 components of the type int, and initializes each component to 10?

(i) int[] alpha = {10, 10, 10, 10, 10};
(ii) int[5] alpha = {10, 10, 10, 10, 10}


1. None of these
2. Only (ii)
3. Only (i)
4. Both (i) and (ii)

3. Only (i) 100%

Which of the following about Java arrays is true?
(i) Array components must be of the type double.
(ii) The array index must evaluate to an integer.


1. None of these
2. Both (i) and (ii)
3. Only (ii)
4. Only (i)

3. Only (ii) 100%

boolean found = true;

while (found)
{
entry = readInt();
triple = entry * 3;
if (entry > 33)
found = false;
}

The above code is an example of a(n) ____ loop.


1. EOF-controlled
2. flag-controlled
3. counter-controlled
4. sentinel-controlled

2. flag-controlled 100%

To respond to events in your graphical applets or applications, you'll need to import the ____________ package.


1. java.awt.event
2. java.event
3. javax.swing
4. java.awt

1. java.awt.event 100%

Assume the assignment statement: a = b; where a and b are type java.awt.Rectangle. This is called a _________ copy and we say that a and b both have ___________ semantics.


1. shallow, value
2. deep, value
3. shallow, reference
4. deep, reference

3. shallow, reference 100%

The Graphics drawPolyline() method uses ________________ to draw a series of lines.


1. a two-dimensional array of int
2. a one-dimensional array of Point
3. a parallel array of Points
4. two parallel arrays of int

4. two parallel arrays of int 100%

In the inheritance hierarchy Animal, Mammal, Rodent, "Mickey Mouse", Mammal is a _____________ of Animal.


1. subclass
2. instance
3. ancestor
4. generalization
5. superclass

1. subclass 100%

What is stored in alpha after the following code executes?

int[] alpha = new int[5];
int j;

for (j = 0; j < 5; j++)
{
alpha[j] = j + 1;

if (j > 2)
alpha[j - 1] = alpha[j] + 2;
}


1. None of these
2. alpha = {1, 2, 3, 4, 5}
3. alpha = {1, 5, 6, 7, 5}
4. alpha = {4, 5, 6, 7, 9}

1. None of these 100%

An object is a(n) ____ of a class.


1. field
2. constant
3. method
4. instance

4. instance 100%

When you instantiate an object from a class, ____ is reserved for each instance field in the class.


1. a field name
2. a constructor
3. memory
4. a signature

3. memory 100%

public class Illustrate
{
private int x;
private int y;

public Illustrate()
{
x = 1;
y = 2;
}

public Illustrate(int a)
{
x = a;
}

public void print()
{
System.out.println("x = " + x + ", y = " + y);
}

public void incrementY()
{
y++;
}
}

What does the default constructor do in the class definition above?


1. Sets the value of x to 0
2. Sets the value of x to a
3. There is no default constructor.
4. Sets the value of x to 1

4. Sets the value of x to 1 100%

A(n) ____ constructor is one that requires no arguments.


1. class
2. default
3. write
4. explicit

2. default 100%

If a class's only constructor requires an argument, you must provide an argument for every ____ of the class that you create.


1. parameter
2. type
3. object
4. method

3. object 100%

Consider the following class definition.

public class Rectangle
{
private double length;
private double width;

public Rectangle()
{
length = 0;
width = 0;
}

public Rectangle(double l, double w)
{
length = l;
width = w;
}

public void set(double l, double w)
{
length = l;
width = w;
}

public void print()
{
System.out.println(length + " " + width);
}

public double area()
{
return length * width;
}

public double perimeter()
{
return 2 length + 2 width;
}
}

Which of the following statements correctly instantiate the Rectangle object myRectangle?

(i) myRectangle Rectangle = new Rectangle(10, 12);
(ii) class Rectangle myRectangle = new Rectangle(10, 12);
(iii) Rectangle myRectangle = new Rectangle(10, 12);


1. Only (i)
2. Both (ii) and (iii)
3. Only (iii)
4. Only (ii)

3. Only (iii) 100%

You are ____ required to write a constructor method for a class.


1. sometimes
2. often
3. never
4. always

3. never 100%

Consider the following statements.

public class Circle
{
private double radius;

public Circle()
{
radius = 0.0;
}

public Circle(double r)
{
radius = r;
}

public void set(double r)
{
radius = r;
}

public void print()
{
System.out.println(radius + " " + area + " "
+ circumference);
}

public double area()
{
return 3.14 radius radius;
}

public double circumference()
{
return 2 3.14 radius;
}

}

Circle myCircle = new Circle();
double r;

Which of the following statements are valid in Java? (Assume that cin is Scanner object initialized to the standard input device.)

(i)
r = cin.nextDouble();
myCircle.area = 3.14 r r;
System.out.println(myCircle.area);
(ii)
r = cin.nextDouble();
myCircle.set(r);
System.out.println(myCircle.area());


1. None of these
2. Only (ii)
3. Both (i) and (ii)
4. Only (i)

2. Only (ii) 100%

Data types that contain a single value are known as ___________________ types.


1. fundamental
2. reference
3. structured
4. scalar
5. class

4. scalar 100%

Examine the following UML diagram:


Which of these fields or methods are inherited by the Person class?


1. name, getName(), setName(), getID()
2. toString()
3. None of them
4. studentID, name, getName(), setName(), getID()
5. getName(), setName(), studentID, getID()
6. getName(), setName(), name

2. toString() 100%

Errors made using formal parameter variables defined as enumerated types are ___________________:


1. compile time errors
2. logic errors
3. runtime errors
4. None of these
5. runtime exceptions

1. compile time errors 100%

Assume that you have an ArrayList variable named a containing 4 elements, and an object named element that is the correct type to be stored in the ArrayList. Which of these statements replaces the first object in the collection with element?


1. a[0] = element;
2. a.add(0, element);
3. a.set(element, 0);
4. a.set(0, element);

4. a.set(0, element); 100%

Which of the following lines of code explicitly calls the toString() method, assuming that pete is an initialized Student object variable?


1. println(pete.toString());
2. println("" + pete);
3. println(super.toString());
4. println(pete);

1. println(pete.toString()); 100%

If you do not specify a superclass when using inheritance, the class you are defining implicitly extends the __________________ class.


1. Object
2. Component
3. GObject
4. Root
5. java.lang

1. Object 100%

Which of the following statements is true?


1. The class Throwable, which is derived from the class Exception, is the superclass of the class Object.
2. None of these
3. The class Exception, which is derived from the class Object, is the superclass of the class Throwable.
4. The class Throwable, which is derived from the class Object, is the superclass of the class Exception.

4. The class Throwable, which is derived from the class Object, is the superclass of the class Exception. 100%

double[] as = new double[7];
double[] bs;

bs = as;

How many objects are present after the code fragment above is executed?


1. 14
2. 2
3. 1
4. 7

3. 1 100%

Polygon is a class that defines regular polygons (figures such as equilateral triangles, squares, and regular pentagons—polygons where all the sides have the same length). It has, among other public methods, one named area(), which takes no parameters, and returns as type double the area of the polygon.
Classes Square, EqiTriangle and Pentagon are derived from Polygon. Square and EqiTriangle each have, among other public member functions, one named area(), which takes no parameters and returns as type double the area of a Square and EqiTriangle, respectively. Pentagon does not define a method named area().
Suppose you wish to call Polygon's area() method in the definition of EqiTriangle's area() method; both area() methods have the same signature. How is this done?


A. (polygon)area();
B. super.area();
C. this.area();
D. area();
E. parent.area();

B. super.area(); 100%

double[][] vals = {{1.1, 1.3, 1.5},
{3.1, 3.3, 3.5},
{5.1, 5.3, 5.5},
{7.1, 7.3, 7.5}}

How many rows are in the array above?


1. 3
2. 0
3. 2
4. 4

4. 4 100%

Which of the following is an exception thrown by the methods of the class String?

Feedback
1. FileNotFoundException
2. NumberFormatException
3. NoSuchElementsException
4. NullPointerException

4. NullPointerException 100%

A car dealership needs a program to store information about the cars it has for sale. For each car, the want to keep track of the following information: number of doors (2 or 4), whether the car has air conditioning, and its average number of miles per gallon. Which of the following is the best design?


1. --------------------------------------------------
Use four unrelated classes: Car, Doors, AirConditioning and MilesPerGallon.
2. --------------------------------------------------
Use three classes: Doors, AirConditioning, and MilesPerGallon, each of which has a subclass Car.
3. --------------------------------------------------
Use one class, Car which has three instance variables: int numDoors, boolean hasAir and double milePerGallon.
4. --------------------------------------------------
Use a class Car which has three subclasses: Doors, AirConditioning and MilesPerGallon.
5. --------------------------------------------------
Use a class Car which has a subclass Doors, which has a subclass AirConditioning which has a subclass MilesPerGallon.

3. --------------------------------------------------
Use one class, Car which has three instance variables: int numDoors, boolean hasAir and double milePerGallon. 100%

Consider the following code segment.

for (int i = 0; i < 20; i = i + 2)
{
if (i % 3 == 1)
System.out.print(i + " ");
}

What is printed as a result of executing the code segment?


1. 4 16
2. 0 6 12 18
3. 4 10 16
4. 1 4 7 10 13 16 19
5. 0 2 4 6 8 10 12 14 16 18

3. 4 10 16 100%

Consider the following code segment:

ArrayList<String> list = new ArrayList<String>();

list.add("P");
list.add("Q");
list.add("R");
list.set(2, "s");
list.add(2, "T");
list.add("u");

What is printed as a result of executing this code segment?


1. [P, Q, R, s, T]
2. [P, Q, s, T, u]
3. [P, Q, T, s, u]
4. [P, T, Q, s, u]
5. [P, T, s, R, u]

3. [P, Q, T, s, u] 100%

Consider the following class definition:

public class TimeRecord
{
private int hours;
private int minutes; // 0 <= minutes < 60

public TimeRecord(int h, int m)
{
hours = h;
minutes = m;
}

/* @return the number of hours /
public int getHours() { / implementation not shown / }

/* @return the number of minutes /
public int getMinutes() { / implementation not shown / }

/** Adds h hours and m minutes to this TimeRecord.
* @param h the number of hours
* @param m the number of minutes
*/
public void advance(int h, int m)
{
hours = hours + h;
minutes = minutes + m;
/ missing code /
}

// ... other methods not shown
}

The instance variable minutes must always be at least 0 and less than 60, even when the time is changed. Which of the following code sections can be used to replace the / missing code / in the advance() method so that this condition remains true and so that the time is correctly advanced?


1. --------------------------------------------------
minutes = minutes + hours % 60;
2. --------------------------------------------------
hours = hours + minutes / 60;
3. --------------------------------------------------
minutes = minutes % 60;
4. --------------------------------------------------
hours = hours + minutes % 60;
minutes = minutes / 60;
5. --------------------------------------------------
hours = hours + minutes / 60;
minutes = minutes % 60;

5. --------------------------------------------------
hours = hours + minutes / 60;
minutes = minutes % 60; 100%

Consider the following class definition:

public class Gribble implements Comparable<Gribble>
{
// ... other methods not shown
}

Which of the following method signatures will satisfy the interface requirement that this class agreed to?

I. public int compareTo(Object other)
II. public int compareTo(Gribble other)
III. public boolean compareTo(Gribble other)


1. III only
2. I and II only
3. I, II and III
4. I only
5. II only

5. II only 100%

Assume that the following declarations have been made in a class:

private String s;
private int n;

public void changer(String x, int y)
{
x = x + "peace";
y = y * 2;
}

public void mystery()
{
s = "world";
n = 6;

changer(s, n);

System.out.println("s = " + s + ", n = " + n);
}

What is printed when the mystery() method is called?


1. s = peace, n = 12
2. s = world, n = 12
3. s = worldpeace, n = 6
4. s = worldpeace, n = 12
5. s = world, n = 6

5. s = world, n = 6 100%

Please allow access to your computer’s microphone to use Voice Recording.

Having trouble? Click here for help.

We can’t access your microphone!

Click the icon above to update your browser permissions above and try again

Example:

Reload the page to try again!

Reload

Press Cmd-0 to reset your zoom

Press Ctrl-0 to reset your zoom

It looks like your browser might be zoomed in or out. Your browser needs to be zoomed to a normal size to record audio.

Please upgrade Flash or install Chrome
to use Voice Recording.

For more help, see our troubleshooting page.

Your microphone is muted

For help fixing this issue, see this FAQ.

Star this term

You can study starred terms together

NEW! Voice Recording

Create Set