0% found this document useful (0 votes)
379 views12 pages

Section 6: Correct

The document contains questions and answers related to Java programming concepts like loops, methods, objects, arrays, exceptions, and debugging. Some key points: - Update expressions in for loops are executed after each iteration. break statements terminate loops while continue skips to the next iteration. - Methods can be overloaded by changing the number, type, or order of parameters. Non-static fields and methods require object instantiation. - Arrays have a fixed size set at creation and can cause out-of-bounds exceptions. ArrayLists are dynamically sized but don't store primitives. - Exceptions indicate problems at runtime. Catch blocks handle specific exceptions while try blocks contain code that might throw exceptions. -

Uploaded by

Imas Rohayati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
379 views12 pages

Section 6: Correct

The document contains questions and answers related to Java programming concepts like loops, methods, objects, arrays, exceptions, and debugging. Some key points: - Update expressions in for loops are executed after each iteration. break statements terminate loops while continue skips to the next iteration. - Methods can be overloaded by changing the number, type, or order of parameters. Non-static fields and methods require object instantiation. - Arrays have a fixed size set at creation and can cause out-of-bounds exceptions. ArrayLists are dynamically sized but don't store primitives. - Exceptions indicate problems at runtime. Catch blocks handle specific exceptions while try blocks contain code that might throw exceptions. -

Uploaded by

Imas Rohayati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Section 6

(Answer all questions in this section)


1. When is an update expression in a for loop executed?
Mark for Review

(1) Points
After each iteration through the loop. (*)
After two iterations through the loop.
Before each iteration through the loop.
Before the first iteration through the loop.
Correct
2. Which statement will produce the output: 2, 4, 6, 8, 10?
Mark for Review

(1) Points
for (int i = 0; i < 10; i += 2) {
  System.out.print(i + " ");
}
for (int i = 2; i <= 10; i += 2) {
  System.out.print(i + " ");
} (*)
for (int i = 0; i < 8; i += 2) {
   System.out.print(i + " ");
}
for (int i = 1; i < 10; i += 2) {
  System.out.print(i + " ");
}
Correct
3. You need to calculate the squares of numbers from 1 to 5. Which of the
items should be present in your looping statement?
Mark for Review

(1) Points
Initialization Expression , Update Expression
Initialization Expression , Condition Expression
Initialization Expression , Condition Expression , Update Expression (*)
Condition Expression , Update Expression
Correct
4. What is the result?

public static void main(String[] args) {


    for (;;) {
     System.out.println("Welcome to Java");
   }
}
Mark for Review
(1) Points
Program prints “Welcome to Java” once.
Program prints “Welcome to Java” an infinite number of times. (*)
Compilation error as expressions are missing in the for loop.
No error and no output.
Correct
5. A continue statement is used to skip the remaining statements in the
body of a loop and continue with the next iteration of the loop.
Mark for Review

(1) Points
True (*)
False
Correct
6. Which is used to terminate a loop?
Mark for Review

(1) Points
break (*)
switch
catch
continue
Correct
7. Which of the two are pre-test loops?
Mark for Review

(1) Points
do-while
for(*)
while(*)
forEach
Incorrect. Refer to Section 6 Lesson 2.
8. Which statement is NOT true about do-while loops?
Mark for Review

(1) Points
Statements in the loop are executed repeatedly until the condition
becomes false.
The number of times a do-while loop is executed is dependent upon the
value of the counter variable. (*)
Statements in the loop are executed once until the condition becomes
false.
Statements in the loop are executed once initially, and then the condition
is evaluated.
Correct
9. What is the output?
public static void main(String[] args) {
   int num = 1;
   while (num >= 200){
     System.out.print(num + "" "");
     num = num * 5;
   }
}
Mark for Review

(1) Points
5 25 125
1 5 25 125 175
No output. (*)
1 5 25 125
Incorrect. Refer to Section 6 Lesson 2.
Section 7
(Answer all questions in this section)
10. Which three can vary in overloaded methods?
Mark for Review

(1) Points
Method return type.
The names of parameters
Number of parameters.(*)
Types of parameters.(*)
Order of parameters.(*)
Correct
Previous
11. All overloaded methods share the same name.
Mark for Review

(1) Points
True (*)
False
Correct
12. Methods can call other methods in the same class.
Mark for Review

(1) Points
True (*)
False
Correct
13. An object must be instantiated before its non-static fields and
methods can be accessed.
Mark for Review

(1) Points
True (*)
False
Correct
14. You never need to instantiate a Math object.
Mark for Review

(1) Points
True (*)
False
Correct
15. A constructor is a special method which is commonly used to set the
initial values of an object’s fields.
Mark for Review

(1) Points
True (*)
False
Correct
16. How could you write the Employee constructor so that its parameters
are named the same as the fields they’re initializing?

public class Employee{


   private String name;
   private double salary;
   public Employee(String name, double salary){
     //initialize name
     //initialize salary
   }
}
Mark for Review

(1) Points
public Employee(String name, double salary){
   name = name;
   salary = salary;
}
public Employee(String name, double salary){
   name = this.name;
   salary = this.salary;
}
public Employee(String name, double salary){
   this.name = name;
   this.salary = salary;
} (*)
public Employee(String name, double salary){
   this.name = this.name;
   this.salary = this.salary;
}
Correct
17. You create an Employee object with a String employeeName field.
What is the default value for employeeName?
Mark for Review

(1) Points
null (*)
A space
“Name”
“default”
Correct
18. What will happen when you try to access an object reference with a
null value?
Mark for Review

(1) Points
NullPointerException. (*)
The value null is retrieved from the memory location.
An empty object is returned.
You will get a compilation error.
Correct
19. Which has a default value of null?
Mark for Review

(1) Points
boolean
int
String (*)
double
Correct
20. In the following statements, how many employee objects are created?

Employee e1 = new Employee();


Employee e2 = new Employee();
Employee e3 = new Employee();

Mark for Review

(1) Points
3 (*)
2
0
1
Correct
Previous
Section 7
(Answer all questions in this section)
21. In this statement, identify the type of the variable s.

Student s = new Student();


Mark for Review

(1) Points
String
Class
null
Student (*)
Correct
22. Which two statements are true about objects of the same class?
Mark for Review

(1) Points
Each object will have the same reference variable to the location in memory.
All objects are equal.
Each new instance of an object will have a different location in memory.(*)
All objects of the same class have the same methods.(*)
Correct
23. First, you decide the radius of each circle in the logo. Then using the
same radius you draw 5 circles of same size. All these circles will have
properties like radius and color. All circles share behaviors to calculate
circumference and area. Can you identify which of the following is an
object?
Mark for Review

(1) Points
fiveCircles
radius
circumference
circle (*)
Correct
24. You have created an Employee class with all required fields and
methods. 10 employees join the company. Should you copy and paste the
Employee class for all 10 employees?
Mark for Review

(1) Points
True
False (*)
Correct
25. How can you retrieve a value from a method?
Mark for Review
(1) Points
Use a return statement and define the method’s return type as non-
void (*)
Define the method return type as void
Define a variable as a field member of the method
Pass a variable as an argument to the method.
Correct
26. What is encapsulation?
Mark for Review

(1) Points
A technique for writing more than one main method.
A technique for including primitives within an ArrayList.
A technique for limiting one class’s visibility to another. (*)
A technique for debugging.
Correct
27. Which two statements are true about private access modifier?
Mark for Review

(1) Points
Class fields marked private are most secure.(*)
Class fields marked private are visible to any class.
Class fields are typically marked private.(*)
Class fields are typically marked public.
Correct
28. Access and visibility of a class should be limited as much as possible.
Mark for Review

(1) Points
True (*)
False
Correct
Section 8
(Answer all questions in this section)
29. What is the output?

int[] array = {10, 20, 30};


int b = 0;
try{
   System.out.println("1");
   int c = (array[3] / b);
   System.out.println("2");
}
catch(ArithmeticException ex){
   System.out.println("Arithmetic Exception");
}
catch(ArrayIndexOutOfBoundsException ex){
   System.out.println("Array index out of bounds");
}
Mark for Review

(1) Points
1
Arithmetic Exception
1
Array index out of bounds (*)
1
2
Array index out of bounds
1
2
Array index out of bounds
Correct
30. If the try block succeeds then no exception has occurred.
Mark for Review

(1) Points
True (*)
False
Correct
31. A wrapper class encapsulates, or wraps, the primitive types within an
object.
Mark for Review

(1) Points
True (*)
False
Correct
32. Which is not used to traverse an ArrayList?
Mark for Review

(1) Points
ListIterator
iterator
for-each loop
do- while loop (*)
Incorrect. Refer to Section 8 Lesson 2.
33. Which is NOT a benefit of ArrayList class?
Mark for Review

(1) Points
An ArrayList grows as you add elements.
You can remove all of the elements of an ArrayList with a method.
An ArrayList shrinks as you remove elements.
You can use an ArrayList list to store Java primitive values (like int). (*)
Correct
34. Which two are limitations of an array of primitives (ie: int[] x)?
Mark for Review

(1) Points
The size of the array is fixed during array creation and cannot grow once initialized.(*)
You can create only one array in a class.
You cannot overwrite the contents of an array once initialized.
You need to create your own methods to manipulate array contents.(*)
Correct
35. What is an array?
Mark for Review

(1) Points
An array is an indexed container that holds a set of values of a multiple
types.
An array is an indexed container that holds a set of values of a single
type. (*)
An array is a Java primitive type.
An array is a way to create multiple copies of a single value.
Correct
36. The Java compiler does not check for an
ArrayIndexOutOfBoundsException during the compilation of a program
containing arrays.
Mark for Review

(1) Points
True (*)
False
Correct
37. What is the output?
int[] arr = new int[1];
arr[0] = 10;
System.out.println(arr[0]);

Mark for Review

(1) Points
ArrayIndexOutOfBoundsException
0
10 (*)
1
Correct
38. Which loop type is specially designed to traverse an array?
Mark for Review
(1) Points
for loop (*)
repeat loop
while loop
do while loop
Correct
39. What are two advantages of adding print statements for debugging?
Mark for Review

(1) Points
You can identify the order in which methods have been called.(*)
You can identify runtime errors.
You can identify which methods have been called.(*)
You can identify compilation errors.
Correct
40. What are two disadvantages of adding print statements for
debugging?
Mark for Review

(1) Points
It’s tedious to remove print statements.(*)
Too many print statements lead to information overload.(*)
Print statements cannot print the values of an object’s fields.
Print statements cannot print the values of variables.
Correct
41. Runtime errors can be caught by Java’s exception handling
mechanism.
Mark for Review

(1) Points
True (*)
False
Correct
42. Testing and debugging are important activities in software
development.
Mark for Review

(1) Points
True (*)
False
Correct
Section 9
(Answer all questions in this section)
43. Lambda Expressions provide much more effective and cleaner syntax
for working with GUI applications and sorting lists.
Mark for Review

(1) Points
True (*)
False
Correct
44. An Image is an object that describes the location of a graphics file.
Mark for Review

(1) Points
True (*)
False
Correct
45. When you write code for MouseEvents, you are telling a Node to listen
for a particular event.
Mark for Review

(1) Points
True (*)
False
Correct
46. Which is the correct syntax to instantiate a JavaFX Rectangle?
Mark for Review

(1) Points
Rectangle rect = new Rectangle(20, 20, 100, 200); (*)
Rectangle rect = Rectangle(20, 20, 100, 200);
Rectangle rect = new Rectangle(20, 20);
Rectangle rect = new Rectangle(20, 20, 100);
Correct
47. JavaFX Ensemble contains code examples of JavaFX features.
Mark for Review

(1) Points
True (*)
False
Correct
48. How would you set the title of the Stage primaryStage?
Mark for Review

(1) Points
primaryStage = "New Title!;
primaryStage("New Title!");
primaryStage.title = "New Title!";
primaryStage.setTitle("New Title!"); (*)
Correct
49. A layout Pane dictates how Nodes must be positioned
Mark for Review

(1) Points
True (*)
False
Correct
50. JavaFX is used to create GUI applications.
Mark for Review

(1) Points
True (*)
False
Correct

You might also like