GABUNGAN
GABUNGAN
GABUNGAN
Section 4 Quiz
(Answer all questions in this section)
"\\" (*)
2. The String methods equals and compareTo perform similar functions and differ in
their return type. True or false? Mark for Review
True (*)
6. Which of the two diagrams below illustrate the general form of a Java program?
Mark for Review.
(1) Points
Example A
Example B (*)
7. When importing another package into a class you must import only the package
classes that will be called and not the entire package. True or false?
False (*)
8. What two values can a boolean variable have? Mark for Review
(1) Points
True and false (*)
9. Which line of Java code assigns the value of 5 raised to the power of 8 to a?
double a=Math.pow(5,8); (*)
int j=6,k=8,m=2,result;
result=j-k%3*m;
System.out.println(result);
2 (*)
11. What will the method methodA print to the screen?
18 (*)
12. Write a declaration statement that will hold a number like 2.541.
float number; (*)
13. For every opening curly brace { there must be a closing curly brace} or the
program will not compile without error. True or False?
False (*)
14. You can return to the Eclipse Welcome Page by choosing Welcome from what
menu?
Help (*)
15. Tabs are used when more than one file is open in the edit area. True or False?
True (*)
You are writing a class and are using a global variable. Inside a method you declare a
local variable with the same name as the global variable.
This programming style is poor because inside the method the global variable will have
precedence over the local variable with the same name.
2. Which of the following statements correctly assigns "3 times 10 to the 4th power"
to the variable number?double number=3e4; (*)
3. Which line of code does not assign 3.5 to the variable x? Mark for Review
(1) Points
3.5=x; (*)
4. A local variable has precedence over a global variable in a Java method. True or
false?
True (*)
5. What does the following program output?
6. For every opening curly brace { there must be a closing curly brace} or the
program will not compile without error. True or False?
True (*)
7. The ______________ is the location into which you will store and save your files.
Workspace (*)
False (*)
10. Suppose that s1 and s2 are two strings. Which of the statements or expressions are
valid?
(Choose all correct answers)
String s3 = s1 + s2; (*)
s1.compareTo(s2); (*)
int m = s1.length(); (*)
11. Declaring and instantiating a String is much like any other type of variable.
However, once instantiated, they are final and cannot be changed. True or false?
True (*)
1. Match each of the following literals ('x', 10, 10.2, 100L, "hello") with its
respective data type.
char, int, double, long, String (*)
2. Which line of Java code will assign the value of the square root of 11 to a variable
named a?
double a=Math.sqrt(11); (*)
You are writing a class and are using a global variable. Inside a method you declare a
local variable with the same name as the global variable.
This programming style is poor because inside the method the global variable will have
precedence over the local variable with the same name.
True or false?
False (*)
5. Select the declaration and initialization statement that will hold the letter J.
char letter='J'; (*)
6. For every opening curly brace { there does not need to be a closing curly brace}
for the program to compile without error. True or False?
False (*)
13. Which of the two diagrams below illustrate the general form of a Java program?
1. In Eclipse, when you run a Java Application, the results are displayed in a new
window. True or False?
False (*)
2. Multiple windows are used when more than one file is open in the edit area. True
or False?
False (*)
10. Given the code below, which of the following would equate to true?
String s1 = "yes";
String s2 = "yes";
String s3 = new String(s1);
Mark for Review
(1) Points
11. Which line of Java code will assign the value of the square root of 11 to a variable
named a?
double a=Math.sqrt(11); (*)
12. Which line of Java code assigns the value of 5 raised to the power of 8 to a?
double a=Math.pow(5,8); (*)
13. Which line of Java code properly calculates the area of a triangle using
A=1/2(b)(h) where b and h are Java primitive integers?
double A=(double)1/(double)2*b*h; (*)
int j=7,k=5,m=8,result;
result=j/m*k;
System.out.println(result);
0 (*)
4. Which of the two diagrams below illustrate the general form of a Java program?
7. Which of the two diagrams below illustrate the general form of a Java program?
int j=7,k=5,m=8,result;
result=j/m*k;
System.out.println(result);
0 (*)
int j=6,k=8,m=2,result;
result=j-k%3*m;
System.out.println(result);
2 (*)
\\\\\\\ (*)
14. Declaring and instantiating a String is much like any other type of variable.
However, once instantiated, they are final and cannot be changed. True or false?
True (*)
What is printed?
ArrayIndexOutofBoundsException is thrown (*)
1. Which the line of code does not assign 3.5 to the variable x?
3.5 = X(*).
2. A workspace is :
All of the Above(*)
88888(*)
SECTION 5
1. Which of the following could be a reason to use a switch statement in a Java program
Because it allows the program to run certain segments of code and neglect to run others
based on the input given. (*)
3. Which of the two diagrams below illustrate the correct syntax for variables used in an if-
else statement?
Example A (*)
Example B
6. How would you use the ternary operator to rewrite this if statement?
if (skillLevel > 5)
numberOfEnemies = 10;
else
numberOfEnemies = 5;
numberOfEnemies = ( skillLevel > 5) ? 10 : 5; (*)
7. What will print if the following Java code is executed?
if ((5.1 > 4.3 && 6.2 < 8.4) && !(7.2 < 3.5 || 1.2 == 2.1 || 2.2 != 2.25))
System.out.print("TRUE");
else
System.out.print("FALSE);
False (*)
10. In a for loop, the counter is automatically incremented after each loop iteration. True or
False?
False (*)
13. When the for loop condition statement is met the construct is exited. True or false?
False (*)
14. Updating the input of a loop allows you to implement the code with the next element
rather than repeating the code always with the same element. True or false?
True (*)
15. What is one significant difference between a while loop and a do-while loop?
A DO-WHILE loop will always execute the code at least once, even if the conditional
statement for the WHILE is never true. A WHILE loop is only executed if the conditional
statement is true. (*)
SECTION 6
2. Which of the following declares and initializes a two dimensional array named values with 2
rows and 3 columns where each element is a reference to an Object?
array.length is 8 (*)
4. What is the output of the following segment of code if the command line arguments are
"apples oranges pears"?
pears (*)
5. What is the output of the following segment of code if the command line arguments are "a b
c d e f g"?
e (*)
11 (*)
777777 (*)
9. The following creates a reference in memory named q that can refer to eight different
doubles via an index. True or false?
double[] q = new double[8];
True (*)
666666 (*)
11. Suppose you are writing a program where the user is prompted to the give coordinates
where they believe the princess is inside of the castle.
Your program moves the prince to the coordinates that the user specified. If the princess is not
found at those coordinates, the user is given a clue that helps them guess coordinates closer to the
princess. The user is allowed to enter their new guess of where the princess is.
Assume your program does not take into consideration the possibility that the user may enter
coordinates outside of the castle where the princess could not be. What would be the result of the
user entering coordinates outside of the castle? How could this be handled in your code?
(Choose all correct answers)
An exception would occur. This could be handled by throwing the exception in your code if
the user enters invalid coordinates. When the exception is caught, the prince could be
moved to the coordinate inside the castle that is closest to those that the user specified. (*)
An exception would occur. This could be handled by throwing an exception in your code if
the user enters invalid coordinates. When the exception is caught, the user could be
prompted to enter coordinates within the given range of the castle. (*)
12. Which of the following defines an Exception?
A problem that can be corrected or handled by your code. (*)
14. It is possible to throw and catch a second exception inside a catch block of code. True or
false?
True (*)
\
15. What exception message indicates that a variable may have been mispelled somewhere in
the program?
variableName cannot be resolved to a variable (*)
3. A logic error occurs if an unintentional semicolon is placed at the end of a loop initiation
because the interpreter reads this as the only line inside the loop, a line that does nothing.
Everything that follows the semicolon is interpreted as code outside of the loop. True or false?
False (*)
5. If an exception has already been thrown, what will the interpreter read next in the
program?
Where the program catches the exception. (*)
777777 (*)
7. Which of the following declares and initializes a two dimensional array where each element
is a reference type?
String[][] words=new String[10][3]; (*)
8. Which of the following declares and initializes a two dimensional array?
int[][] array={{1,1,1},{1,1,1},{1,1,1}}; (*)
10. The following segment of code initializes a 2 dimensional array of primitive data types.
True or false?
double[][] a=new double[4][5];
True (*)
642246 (*)
14. The following segment of code prints all five of the command line arguments entered into
this program. True or false?
False (*)
15. Which of the following statements print every element of the one dimensional array
prices to the screen?
for(int i=0; i < prices.length; i++){System.out.println(prices[i]);} (*)
SECTION 7
1. What is encapsulation?
A programming philosophy that promotes protecting data and hiding
implementation in order to preserve the integrity of data and methods. (*)
2. Consider creating a class Square that extends the Rectangle class provided below.
Knowing that a square always has the same width and length, which of the following best
represents a constructor for the Square class?
JAWABAN :
(*)
3. Which of the following demonstrates the correct way to create an applet Battlefield?
public class Battlefield extends Applet{...} (*)
Joe is a college student who has a tendency to lose his books. Replacing them is getting costly. In
an attempt to get organized, Joe wants to create a program that will store his textbooks in one
group of books, but he wants to make each book type the subject of the book (i.e. MathBook is a
book). How could he store these different subject books into a single array?
Using polymorphism. (*)
7. Consider
9. The basic unit of encapsulation in Java is the primitive data type. True or false?
False (*)
10. You can create static class methods inside any Java class. True or false?
True (*)
11. Any instance of the same class can assign a new value to a static variable. True or false?
True (*)
12. Which of the following access modifiers doesn't work with a static variable?
friendly (*)
13. Which of the following shows the correct way to initialize a method DolphinTalk that
takes in 2 integers, dol1 and dol2, and returns the greater int between the two?
int DolphinTalk(int dol1,int dol2){ if(dol1 > dol2) return dol1; else return dol2;} (*)
15. Which of the following specifies accessibility to variables, methods, and classes?
Access modifiers (*)
2. Static classes can have different access specifiers than the parent class. True or false?
True (*)
6. Which of the following correctly describes the use of the keyword super?
A keyword that allows subclasses to access methods, data, and constructors from
their parent class. (*)
7. What is Polymorphism?
The concept that a variable or reference can hold multiple types of objects. (*)
10. If the return type from a method is boolean then 2.5 is a valid return value. True or false?
False (*)
11. The basic unit of encapsulation in Java is the primitive data type. True or false?
False (*)
12. Consider:
15. Identify the error(s) in the class below. Choose all that apply
Joe is a college student who has a tendency to lose his books. Replacing them is getting costly. In
an attempt to get organized, Joe wants to create a program that will store his textbooks in one
group of books, but he wants to make each book type the subject of the book (i.e. MathBook is a
book). How could he store these different subject books into a single array?
Using polymorphism. (*)
True or false?
True (*)
4. Which segment of code correctly defines a method that contains two objects of class Tree
as parameters?
void bloom(Tree pine, Tree oak) {//code here } (*)
6. Which of the following is a possible way to overload constructors? Mark for Review
(1) Points
(*)
7. Identify the driver class that correctly initializes employees Jane and Brandon. The
Employee class is below.
class Account{
private int number;
private String name;
public Account;
}
The constructor method has no definition. (*)
9. The basic unit of encapsulation in Java is the primitive data type. True or false?
False (*)
10. Static methods can return any object type. True or false?
True (*)
11. Static classes can extend their parent class. True or false?
True (*)
12. Non-final static class variables should be private to prevent changes from other classes.
True or false?
True (*)
15. Which of the following demonstrates the correct way to create an applet Battlefield?
public class Battlefield extends Applet{...} (*)
2. It is possible to extend a class that already exists in Java, such as the Applet class. True or
false?
True (*)
7. Static classes are designed as thread safe class instances. True or false?
False (*)
8. Static classes can extend any class in their class path. True or false?
True (*)
10. Which segment of code correctly defines a method that contains two objects of class Tree
as parameters?
void bloom(Tree pine, Tree oak) {//code here } (*)
11. Which of the following is the correct way to code a method with a return type an object
Automobile?
12. Choose the correct implementation of a public access modifier for the method divide.
public int divide(int a, int b) {return a/b;} (*)
13. Which constructor code populates the instance variables of the class correctly?
(*)
14. What value will return for j when the setValue method is called?
11 (*)
15. What is wrong with the following class declaration?
1. Which of the following keywords are used to access the instance variables of an object
from within the class code for that object?
this (*)
class Account{
private int number;
private String name;
public Account;
}
The constructor method has no definition. (*)
5. Which of the following is the proper way to set the public variable length of the super
class equal to 5 from inside the subclass?
super.length = 5 (*)
6. Which of the following correctly describes the use of the keyword super?
A keyword that allows subclasses to access methods, data, and constructors from
their parent class. (*)
10. There is only one copy a static class variable in the JVM. True or false?
True (*)
12. Which of the following access modifiers doesn't work with a static variable?
friendly (*)
15. Identify the error(s) in the class below. Choose all that apply
1. Following good programming guidelines, what access modifier should be used for the
class fields in the following situation?
A car insurance company wants to create a class named Customer that stores all data for a
specified customer including the fields: vehicle information, policy information, and a credit card
number.
private (*)
2. Identify the error(s) in the class below. Choose all that apply
3. Which segment of code represents a correct way to call a variable argument method
counter that takes in integers as its variable argument parameter?
counter(1, 5, 8, 17, 11000005); (*)
4. The return value of a method can only be a primitive type and not an object. True or
false? False (*)
8. What allows Java to correctly and automatically determine which method to invoke based
on the type of object being referred to at the time the method is called?
Dynamic Method Dispatch (*)
10. Which of the following correctly defines a superclass (or parent class)?
A class that passes down its methods to more specialized classes. (*)
14. Static classes can't return instances of the parent class when the parent class uses a private
constructor. True or false?
False (*)
15. Static classes can have different access specifiers than the parent class. True or false?
True (*)
1. According to the following class declaration, runSpeed can be modified in class Cat. True
or false
3. It is possible to extend a class that already exists in Java, such as the Applet class. True or
false? True (*)
4. If the return type from a method is boolean then 2.5 is a valid return value. True or false?
False (*)
5. The following statement compiles and executes. What do you know for certain?
tree.grows(numFeet);
grows must be the name of a method. (*)
6. A constructor must have the same name as the class where it is declared. True or false?
True (*)
9. Identify the step (s) in creating a Triangle Applet that displays two triangles.
(Choose all correct answers)
Draw the triangle using the inherited fillPolygon method. (*)
Draw the 2nd triangle using the inherited fillPolygon method. (*)
Extend Applet class to inherit all methods including paint. (*)
Override the paint method to include the triangles. (*)
Run and compile your code. (*)
11. A team is working on a coding project. They desire that all portions of their code should
have access to the classes that they write. What access modifier should be used for each
class? Mark for Review
(1) Points
public (*)
15. Static classes can exist as stand alone classes. True or false?
False (*)