CPAN116 – Algorithms & Problem Solving – Quiz 4

Question: Look at the following code and answer following questions in order.
double numbers = new double<7>;
a. What is the name of the array that is being declared?
b. What is the size of the array?
c. What data type are the array elements?
d. What is the subscript of the last element in the array?

Answer:
a. numbers
b. 7
c. double
d. 6

 

Question: Match each item on the left with the correct item on the right.

Answer:
Parent Class = GradedActivity
Child Class = Exam
Number of Parent classes = 1
Number of child classes = 3
Number of operations in Project = 5
Number of operations in Lab = 4
Number of fields in Exam = 4

 

Question: Consider the following code fragment
int counter = 1;
while(counter < 10){
System.out.println(counter);
counter = counter + 1;
}
What will it display?

Answer:

1
2
3
4
5
6
7
8
9

 

Question: Consider the following code fragment
double temperature;
double MAX_TEMP = 102;
Scanner scan = new Scanner(System.in);
do{
System.out.println(“Enter the substance’s temperature”);
temperature = scan.nextDouble();
if(temperature > MAX_TEMP){
System.out.println(“The temperature is too high”);
}
}while(temperature > MAX_TEMP);
System.out.println(“Check it again in 15 minutes”);
What will it display if user enters 101?

Answer: Check it again in 15 minutes

 

Question: Match an item on left with the correct answer on right

Answer:
The best loop to use if the number of iterations is not known up front = while
The best loop to use when we need to do something to all the elements in an array = for
Pretest loop = while
Post test loop = do-while
A loop that always executes at least once = do-while

 

Question: The elements in an array can be accessed using a(an)

Answer: index

 

Question: Is Project a child class or a parent class?

Answer: Child class

 

Question: How many fields does Lab have?

Answer: 2

 

Question: What output will the following code fragment print?
String fruits[ ] = {“Banana”, “Strawberry”, “Apple”, “Pear”, “Grapes”, “Papaya”};
fruits[3] = “Orange”;
fruits[1] = “Blackberry”;
System.out.println(fruits[5]);

Answer: Papaya

 

Question: Fix the error in the following code:
String names[ ] = {“Meg”, “Jack”, “Steve”, “Bill”, “Lisa”};
int index;
for(index = 0; index <= names.length; index++){
System.out.println(names[index]);
}

Answer: for(index = 0; index <= names.length – 1; index++)

 

Question: What is the relationship described by: Plane has a Wing

Answer: aggregation, HAS-A, and composition

 

Question: The parent class inherits fields and methods from the child class.

Answer: False

 

Question: Write a Java declaration statement for a String array initialized with the following strings:
“Einstein”, “Newton”, “Copernicus”, “Kepler”.

Answer: String mathematician[] = {“Einstein”, “Newton”, “Copernicus”, “Kepler”};

 

Question: Assume an existing array of doubles named sales with size 5. Write a statement to assign the values to each of its five elements.

Answer:
for(int index = 0; index < sales.length; index++){
Display “Enter the value to set the array element”;
sales[index] = scan.nextInt();
}

Leave a comment