You are here

Java - Chapters 6,7,8,9 - Multiple choice

Question 1
/ 2 pts

Given the following code, what will be the value of finalAmount when it is displayed?

public class Order

{

   private int orderNum;

   private double orderAmount;

   private double orderDiscount;

 

   public Order(int orderNumber, double orderAmt,

               double orderDisc)

   {

      orderNum = orderNumber;

      orderAmount = orderAmt;

      orderDiscount = orderDisc;

   }

 

   public double finalOrderTotal()

   {

      return orderAmount - orderAmount *

             orderDiscount;

   }

}

 

public class CustomerOrder

{

   public static void main(String[] args)

   {

      Order order;

      int orderNumber = 1234;

      double orderAmt = 580.00;

      double orderDisc = .1;

      order = new Order(orderNumber, orderAmt, orderDisc);

      double finalAmount = order.finalOrderTotal();

      System.out.printf("Final order amount = $%,.2f\n",

                        finalAmount);

   }

}


  


 
 
 

 
 
 
 

  


 
 
 
Correct!
  


 
 
 
 
 
 


Question 2
/ 2 pts

Which of the following statements will create a reference, str, to the string, "Hello, world"?

(1)    String str = new String("Hello, world");

(2)    String str = "Hello, world";


  


 
 
 
Correct!
  


 
 
 

  


 
 
 

  


 
 
 
 
 
 


Question 3
/ 2 pts

In UML diagrams, this symbol indicates that a member is public.


  


 
 
 

  


 
 
 

  


 
 
 
Correct!
  


 
 
 
 
 
 


Question 4
/ 2 pts

This refers to the combining of data and code into a single object.


  


 
 
 
Correct!
  


 
 
 

  


 
 
 

  


 
 
 
 
 
 


Question 5
/ 2 pts

A class specifies the ________ and ________ that a particular type of object has.

Correct!
  


 
 
 

  


 
 
 

  


 
 
 

  


 
 
 
 
 
 


Question 6
/ 2 pts

Given the following code, what will be the value of finalAmount when it is displayed?

 

public class Order

{

   private int orderNum;

   private double orderAmount;

   private double orderDiscount;

 

   public Order(int orderNumber, double orderAmt,

               double orderDisc)

   {

      orderNum = orderNumber;

      orderAmount = orderAmt;

      orderDiscount = orderDisc;

   }

   public int getOrderAmount()

   {

      return orderAmount;

   }

   public int getOrderDisc()

   {

      return orderDisc;

   }

}

 

public class CustomerOrder

{

   public static void main(String[] args)

   {

      int ordNum = 1234;

      double ordAmount = 580.00;

      double discountPer = .1;

      Order order;

      double finalAmount = order.getOrderAmount() —

             order.getOrderAmount() * order.getOrderDisc();

      System.out.printf("Final order amount = $%,.2f\n",

                        finalAmount);

   }

}


  


 
 
 
Correct Answer
 
 
 
 

  


 
 
 
You Answered
  


 
 
 
 
 
 


Question 7
/ 2 pts

Which of the following statements will create a reference, str, to the String, "Hello, World"?

Correct!
  


 
 
 

  


 
 
 

  


 
 
 

  


 
 
 
 
 
 


Question 8
/ 2 pts

What does the following UML diagram entry mean?


 
 
 
 
Correct!
 
 
 
 

 
 
 
 

 
 
 
 
 
 
 


Question 9
/ 2 pts

For the following code, which statement is NOT true?

public class Sphere

{

    private double radius;

    public double x;

    private double y;

    private double z;

}


  


 
 
 
Correct Answer
  


 
 
 
You Answered
  


 
 
 

 


 
 
 
 
 
 


Question 10
/ 2 pts

When you are working with a ________, you are using a storage location that holds a piece of data.


  


 
 
 

  


 
 
 

  


 
 
 
Correct!
  


 
 
 
 
 
 


Question 11
/ 2 pts

Class objects normally have ________ that perform useful operations on their data, but primitive variables do not.


  


 
 
 

  


 
 
 
Correct!
  


 
 
 

  


 
 
 
 
 
 


Question 12
/ 2 pts

The following statement creates an ArrayList object. What is the purpose of the <String> notation?

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


 
 
 
 

 


 
 
 

 
 
 
 
Correct!
 
 
 
 
 
 
 


Question 13
/ 2 pts

You can use this ArrayList class method to replace an item at a specific location in an ArrayList.


  


 
 
 

  


 
 
 

  


 
 
 
Correct!
  


 
 
 
 
 
 


Question 14
/ 2 pts

If numbers is a two-dimensional int array that has been initialized and total is an int that has been set to 0, which of the following will sum all the elements in the array?


  

 
 
Correct!
  

 
 

  

 
 

  

 
 
 
 
 


Question 15
/ 2 pts

Which of the following for loops is valid, given the following declaration?

String[] names = {"abc", "def", "ghi", "jkl"};


  

 
 

  

 
 
Correct!
  

 
 

  

 
 
 
 
 


Question 16
/ 2 pts

What would be the results of the following code?

final int SIZE = 25;

int[] array1 = new int[SIZE];

… // Code that will put values in array1

int value = 0;

for (int a = 0; a < array1.length; a++)

{

   value += array1[a];

}

" id="answer_9623" style="border-top: 1px solid rgb(221, 221, 221); padding: 8px 30px 0px; clear: both; margin: 0px 0px 8px; position: relative; opacity: 0.5;" title="Value contains the lowest value in array1..">

  


 
 
 
Correct!
  


 
 
 

  


 
 
 

  


 
 
 
 
 
 


Question 17
/ 2 pts

What would be the results of the following code?

final int SIZE = 25;

int[] array1 = new int[SIZE];

… // Code that will put values in array1

int value = 0;

for (int a = 0; a <= array1.length; a++)

{

   value += array1[a];

}


  


 
 
 

  


 
 
 

  


 
 
 
Correct!
  


 
 
 
 
 
 


Question 18
/ 2 pts

What will be the value of x[1] after the following code is executed?

int[] x = {22, 33, 44};

arrayProcess(x[1]);

...

public static void arrayProcess(int a)

{

   a = a + 5;

}

Correct!
  


 
 
 

  


 
 
 

  


 
 
 

  


 
 
 
 
 
 


Question 19
/ 2 pts

What would be the results after the following code was executed?

int[] x = {23, 55, 83, 19};

int[] y = {36, 78, 12, 24};

x = y;

y = x;


  

 
 

  

 
 
Correct!
  

 
 

  


 
 
 
 
 
 


Question 20
/ 2 pts

What will be the value of x[8] after the following code has been executed?

final int SUB = 12;

int[] x = new int[SUB];

int y = 20;

for(int i = 0; i < SUB; i++)

{

   x[i] = y;

   y += 5;

}


  


 
 
 
Correct!
  


 
 
 

  


 
 
 

  


 
 
 
 
 
 


Question 21
/ 2 pts

What is the value of scores[2][3] in the following array? 

int [] [] scores = { {88, 80, 79, 92}, {75, 84, 93, 80},

     {98, 95, 92, 94}, {91, 84, 88, 96} };


  


 
 
 

  


 
 
 

  


 
 
 
Correct!
  


 
 
 
 
 
 


Question 22
/ 2 pts

What would be the results of the following code? 

int[] array1 = new int[25];

… // Code that will put values in array1

int value = array1[0];

for (int a = 1; a < array1.length; a++)

{

   if (array1[a] < value)

     value = array1[a];

}


  


 
 
 
Correct!
  


 
 
 

  


 
 
 

  


 
 
 
 
 
 


Question 23
/ 2 pts

What will be the value of x[1] after the following code is executed? 

int[] x = {22, 33, 44};

arrayProcess(x);

public static void arrayProcess(int[] a)

{

   for(int k = 0; k < 3; k++)

   {

      a[k] = a[k] + 5;

   }

}


  


 
 
 

  


 
 
 

  


 
 
 
Correct!
  


 
 
 
 
 
 


Question 24
/ 2 pts

What would be the results of the following code?

int[] x = { 55, 33, 88, 22, 99,

            11, 44, 66, 77 };

int a = 10;

if(x[2] > x[5])

   a = 5;

else

   a = 8;

Correct!
  


 
 
 

 
 
 
 

  


 
 
 

  


 
 
 
 
 
 


Question 25
/ 2 pts

What will be the results of the following code?

final int ARRAY_SIZE = 5;

double[] x = new double[ARRAY_SIZE];

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

{

   x[i] = 10.0;

}


  


 
 
 

  


 
 
 

  


 
 
 
Correct!
  


 
 
 
 
 
 


Question 26
/ 2 pts

What will be the value of x[8] after the following code has been executed?

final int SUB = 12;

int[] x = new int[SUB];

int y = 100;

for(int i = 0; i < SUB; i++)

{

   x[i] = y;

   y += 10;

}


  


 
 
 

  


 
 
 

  


 
 
 
Correct!
  


 
 
 
 
 
 


Question 27
/ 2 pts

Assume the class BankAccount has been created, and the following statement correctly creates an instance of the class:

 BankAccount account = new BankAccount(5000.0);

 What is TRUE about the following statement?

 System.out.println(account);


  


 
 
 

 


 
 
 
Correct!
 


 
 
 

  


 
 
 
 
 
 


Question 28
/ 2 pts

Assuming the following declaration exists:

 enum Tree { OAK, MAPLE, PINE }

 What will the following code display?

 System.out.println(Tree.OAK);

Correct!
  


 
 
 

  


 
 
 

  


 
 
 

  


 
 
 
 
 
 


Question 29
/ 2 pts

Look at the following declaration:

enum Tree { OAK, MAPLE, PINE }

What is the fully-qualified name of the PINE enum constant?


  


 
 
 

  


 
 
 
Correct!
  


 
 
 

  


 
 
 
 
 
 


Question 30
/ 2 pts

Look at the following declaration:

enum Tree { OAK, MAPLE, PINE }

What is the ordinal value of the MAPLE enum constant?

Correct!
  


 
 
 

  


 
 
 

  


 
 
 

  


 
 
 
 
 
 


Question 31
/ 2 pts

When the this variable is used to call a constructor:

Correct!
 


 
 
 

  


 
 
 

 


 
 
 

  


 
 
 
 
 
 


Question 32
/ 2 pts

If the following is from the method section of a UML diagram, which of the following statements is TRUE?

+ add(object2:Stock): Stock

Correct!
 
 
 
 

 


 
 
 

  


 
 
 

 
 
 
 
 
 
 


Question 33
/ 2 pts

What will be returned from a method, if the following is the method header?

public Rectangle getRectangle()


  


 
 
 

  


 
 
 
Correct!
  

 
 

  


 
 
 
 
 
 


Question 34
/ 2 pts

Which of the following is NOT true about static methods?


 
 
 
 

  


 
 
 
Correct!
 
 
 
 

 

 
 
 
 
 


Question 35
/ 2 pts

If object1 and object2 are objects of the same class, to make object2 a copy of object1:


 
 
 
 

 


 
 
 
Correct!
 
 
 
 

  


 
 
 
 
 
 


Question 36
/ 2 pts

use the default constructor to create object2 with object1 data members


 
 
 
 
Correct Answer
 
 
 
 
You Answered
 
 
 
 

" id="answer_6986" style="border-top: 1px solid rgb(221, 221, 221); padding: 8px 30px 0px; clear: both; margin: 0px 0px 8px; position: relative; opacity: 0.5;" title="use the == operator, e.g. object1 == object2.">

  


 
 
 
 
 
 


Question 37
/ 2 pts

If the following is from the method section of a UML diagram, which of the following statements is TRUE?

+ equals(object2:Stock) : boolean

Correct!
 

 
 

  

 
 

 

 
 

  

 
 
 
 
 


Question 38
/ 2 pts

Java automatically stores this value in all uninitialized static member variables:


  


 
 
 

  


 
 
 

  


 
 
 
Correct!
  


 
 
 
 
 
 


Question 39
/ 2 pts

Static methods can only operate on ________ fields.


  


 
 
 

  


 
 
 
Correct!
  


 
 
 

  


 
 
 
 
 
 


Question 40
/ 2 pts

Look at the following code.

 

Integer myNumber;

myNumber = 5; 

Which of the following is TRUE about the second statement?

Correct!
  


 
 
 

  


 
 
 

  


 
 
 

 
 
 
 
 
 
 


Question 41
/ 2 pts

What will be the tokens in the following statement?

String str = "red$green&blue#orange";

String[] tokens = str.split("[$&#]");


  


 
 
 

  


 
 
 
You Answered
  


 
 
 
Correct Answer
  


 
 
 
 
 
 


Question 42
/ 2 pts

To convert the string, str = "285" to an int, use the following statement:


  


 
 
 

  


 
 
 
Correct!
  


 
 
 

  


 
 
 
 
 
 


Question 43
/ 2 pts

For the following code, how many times would the for loop execute?

String str = ("Ben and Jerry's ice cream is great.");

String[] tokens = str.split(" ");

for (String s : tokens)

   System.out.println(s);


  


 
 
 

  


 
 
 

  


 
 
 
Correct!
  


 
 
 
 
 
 


Question 44
/ 2 pts

What will be the tokens given the following statements?

 String str = ("January 1, 2016");

String[] tokens = str.split(" ");


  

 
 

  

 
 
Correct!
  

 
 

  

 
 
 
 
 


Question 45
/ 2 pts

What would be the results of executing the following code?

StringBuilder str = new StringBuilder(12);

str.append("The cow");

str.append(" jumped over the ");

str.append("moon.");

Correct!
  


 
 
 

  


 
 
 

  


 
 
 

  


 
 
 
 
 
 


Question 46
/ 2 pts

What will be the value of matches after the following code has been executed?

boolean matches;

String str1 = "The cow jumped over the moon.";

String str2 = "moon";

matches = str1.endsWith(str2);


  


 
 
 

  


 
 
 

  


 
 
 
Correct!
  


 
 
 
 
 
 


Question 47
/ 2 pts

Use the following import statement when using the Character wrapper class:

Correct!
  


 
 
 

  


 
 
 

  


 
 
 

  


 
 
 
 
 
 


Question 48
/ 2 pts

To convert the int variable, number to a string, use the following statement:

Correct!
  


 
 
 

  


 
 
 

  


 
 
 

  


 
 
 
 
 
 


Question 49
/ 2 pts

To convert the string, str = "285.74" to a double, use the following statement:


  


 
 
 

  


 
 
 

  


 
 
 
Correct!
  


 
 
 
 
 
 


Question 50
/ 2 pts

Each of the numeric wrapper classes has a static ________ method that converts a number to a string.

Correct!