1. What is true about the code below:
Car car1=new Car();
Car car2=new Car();
car2=car1;
Mark for Review
(1) Points
(Choose all correct answers)
There are no more Car objects in memory.
There is a Car object that car1 referenced that is now slated for removal by the garbage collector.
There is a Car object that car2 referenced that is now slated for removal by the garbage collector.
The reference car2 points to an exact copy of the Car Object that car1 references. (*)
The references car1 and car2 are pointing to two Car Objects in memory.
[Incorrect] Incorrect. Refer to Section 7 Lesson 1.
2. In Java, an instance field referenced using the this keyword generates a compilation error. True or false? Mark for Review
(1) Points
True
False (*)
[Incorrect] Incorrect. Refer to Section 7 Lesson 1.
3. Java's garbage collection is when all references to an object are gone, the memory used by the object is automatically reclaimed. True or false? Mark for Review
(1) Points
True (*)
False
[Correct] Correct
4. If you inherit a class, you do not inherit the class' constructors. True or false? Mark for Review
(1) Points
True (*)
False
[Correct] Correct
5. 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?
Mark for Review
(1) Points
(*)
None of the above.
[Incorrect] Incorrect. Refer to Section 7 Lesson 4.
6. What is encapsulation? Mark for Review
(1) Points
A keyword that allows or restricts access to data and methods.
A programming philosophy that promotes simpler, more efficient coding by using exiting code for new applications.
A programming philosophy that promotes protecting data and hiding implementation in order to preserve the integrity of data and methods. (*)
A structure that categorizes and organizes relationships among ideas, concepts of things with the most general at the top and the most specific at the bottom.
[Incorrect] Incorrect. Refer to Section 7 Lesson 4.
7. Abstract classes can be instantiated. True or false? Mark for Review
(1) Points
True
False (*)
[Incorrect] Incorrect. Refer to Section 7 Lesson 5.
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? Mark for Review
(1) Points
Abstract classes
Polymorphism
Dynamic Method Dispatch (*)
Inheritance
[Incorrect] Incorrect. Refer to Section 7 Lesson 5.
9. Would this code be correct if a Dog is a HousePet? Why or Why not?
HousePet Scooby = new Dog(); Mark for Review
(1) Points
Yes, because polymorphism allows this since Dog is a subclass of HousePet. (*)
Yes, because it is an abstract class.
No, because ref must be declared either a HousePet or a Dog, but not both.
Maybe. There is no way to tell without seeing the methods for Dog and the methods for HousePet.
[Correct] Correct
10. Static methods can return any object type. True or false? Mark for Review
(1) Points
True (*)
False
[Correct] Correct
11. Static methods can write to instance variables. True or false? Mark for Review
(1) Points
True
False (*)
[Incorrect] Incorrect. Refer to Section 7 Lesson 3.
12. Static methods can read instance variables. True or false? Mark for Review
(1) Points
True
False (*)
[Incorrect] Incorrect. Refer to Section 7 Lesson 3.
13. It is possible to overload a method that is not a constructor. True or False? Mark for Review
(1) Points
True (*)
False
[Correct] Correct
14. 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 (*)
protected
private
default
All of the above
[Correct] Correct
15. Which segment of code correctly defines a method that contains two objects of class Tree as parameters? Mark for Review
(1) Points
void bloom(Tree pine, Tree oak) {//code here } (*)
Tree bloom (pine, oak) {//code here }
void bloom, Tree pine, Tree oak {//code here }
None of the above, objects cannot be passed as parameters.
[Correct] Correct
1. The three logic operators in Java are: Mark for Review
(1) Points
!=,=,==
&,|,=
&&, ||, ! (*)
&&,!=,=
[Incorrect] Incorrect. Refer to Section 5 Lesson 1.
2. Which of the following correctly matches the switch statement keyword to its function? Mark for Review
(1) Points
(Choose all correct answers)
switch: identifies what element will be compared to the element of the case statements to find a possible match (*)
if: records the user's input and sends it to the case statements to find a possible match
case: signals what code is executed if the user input matches the specified element (*)
switch: tells the compiler the value to compare the input against
default: signals what code to execute if the input does not match any of the cases (*)
[Incorrect] Incorrect. Refer to Section 5 Lesson 1.
3. This keyword is used to instruct specific code when the input for a switch statement that does not match any of the cases. Mark for Review
(1) Points
switch
case
break
default (*)
None of the above
[Incorrect] Incorrect. Refer to Section 5 Lesson 1.
4. How would you use the ternary operator to rewrite this if statement?
if (balance < 500)
fee = 10;
else
fee = 0; Mark for Review
(1) Points
fee = ( balance >= 5) ? 0 : 10;
fee = ( balance > 5) ? 10 : 0;
fee= ( balance < 500) ? 10 : 0; (*)
fee = ( balance < 500) ? 0 : 10;
fee = ( balance >= 500) ? 10 : 0;
[Incorrect] Incorrect. Refer to Section 5 Lesson 1.
5. In Java, each case seqment of a switch statement requires the keyword break to avoid "falling through". Mark for Review
(1) Points
True (*)
False
[Correct] Correct
6. 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"); Mark for Review
(1) Points
True
False (*)
[Incorrect] Incorrect. Refer to Section 5 Lesson 1.
7. Which of the two diagrams below illustrate the correct syntax for variables used in an if-else statement?
Mark for Review
(1) Points
Example A (*)
Example B
[Correct] Correct
8. The following prints Yes on the screen. True or false?
Mark for Review
(1) Points
True
False (*)
[Incorrect] Incorrect. Refer to Section 5 Lesson 1.
9. How many times will the following loop be executed?
What is the value of x after the loop has finished?
What is the value of count after the loop has finished?
int count = 17;
int x = 1;
while(count > x){
x*=3;
count-=3;
} Mark for Review
(1) Points
3; 27; 8 (*)
5; 27; 8
4; 8; 27
5; 30; 5
3; 9; 11
[Correct] Correct
10. A counter used in a for loop cannot be initialized within the For loop header. True or false? Mark for Review
(1) Points
True
False (*)
[Incorrect] Incorrect. Refer to Section 5 Lesson 2.
11. 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? Mark for Review
(1) Points
True (*)
False
[Correct] Correct
12. In a for loop the counter is not automatically incremented after each loop iteration. Code must be written to increment the counter. True or false? Mark for Review
(1) Points
True (*)
False
[Correct] Correct
13. For both the if-else construct and the for loop, it is true to say that when the condition statement is met, the construct is exited. True or False? Mark for Review
(1) Points
True
False (*)
[Incorrect] Incorrect. Refer to Section 5 Lesson 2.
14. In the code fragment below, the syntax for the for loop's initialization is correct. True or false?
public class ForLoop {
public static void main (String args[])
{
for ((int 1=10) (i<20) (i++))
{
System.out.println ("i: " + i);
}
}
}
Mark for Review
(1) Points
True
False (*)
[Incorrect] Incorrect. Refer to Section 5 Lesson 2.
15. Why are loops useful? Mark for Review
(1) Points
They save programmers from having to rewrite code.
They allow for repeating code a variable number of times.
They allow for repeating code until a certain argument is met.
All of the above. (*)
[Incorrect] Incorrect. Refer to Section 5 Lesson 2.
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.
• Public
• Protected
• Private (*)
• Default
• All of the above
2. 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?
• Public (*)
• Protected
• Private
• Default
• All of the above
3. Which of the following could be a reason to need to pass an object into a method?
• Easier access to the information contained within the object.
• The ability to make changes to an object inside of the method.
• Comparing two objects.
• All of the above. (*)
4. 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(dol1, dol2){ if(dol1 > dol2) return dol1; else return dol2;}
• int DolphinTalk(int,int){ if(dol1 > dol2) return dol1; else return dol2;}
• int DolphinTalk(int dol1,int dol2){ if(dol1 > dol2) return dol1; else return dol2;} (*)
• int DolphinTalk, int dol1,int dol2 { if(dol1 > dol2) return dol1; else return dol2;}
• All of the above
5. Cameron wishes to write a method that takes in two objects and returns the one with the greatest value. Is this possible?
• Yes, but he will have to use two different methods, one to take in the objects and the other to return an object.
• Yes, methods can take objects in as parameters and can also return objects all within the same method. (*)
• No, it is not possible to return objects.
• No, it is not possible to have objects as parameters or to return objects.
6. You are assigned to write a method that compares two objects of type Career. One requirement of your assignment is to have your method compare the "greatestPossibleSalary" instance data of Career objects. The "greatestPossibleSalary" field is data type int.
What would be the best return type from your compare method?
• Career, because if it returns the highest paying Career object it will be able to use the same method later to compare other aspects of Career objects. (*)
• Integer, because it is the easiest to code with.
• String, because is should return a string of the name of the career that is highest paying because none of the other information of the career matters.
• Array, because it can store the most information.
7. Consider the following: There is a method A that calls method B. Method B is a variable argument method. With this, which of the following are true?
• Method A can invoke method B twice, each time with a different number of arguments. (*)
• A compliler error will result since method B does not know how large an array to create when it is invoked by method A.
• When invoked, method B creates an array to store some or all of the arguments passed to it from method A. (*)
• All of the above.
8. What type(s) would work for a variable argument method?
Integers, Strings, and Booleans (*)
Constructors
Arrays (*)
Objects (*)
All of the above
9. It is possible to have more than one constructor with the same name in a class, but they must have different parameters. True or false? True (*) False
10. Which of the following is a possible way to overload constructors?
(*)
Quiz 2 Sectiunea 7
1. Static variables can't use which of the following specifiers?
• Public
• Protected
• Friendly (*)
• Default
• Private
2. You can assign new values to static variables by prefacing them with the this keyword and a dot or period. True or false? True (*) False
3. Which of the following statements about static methods is true?
• They exist once per class. (*)
• They exist once in each instance.
• They can be overridden by a subclass.
• They can access any instance variable.
• They cannot access static variables declared outside the method.
4. You can create static class methods inside any Java class. True or false? True (*) False
5. You can return an instance of a private class through a static method of a different class. True or false? True False (*)
6. You can create static classes as independent classes. True or false? True False (*)
7. You can use an inner static class to return an instance of its outer class container. True or false? True (*) False
8. A linear recursive method can call how many copies of itself?
• 1 (*)
• 2 or more
• None
9. Which case does a recursive method call last?
• Recursive Case
• Convergence Case
• Basic Case
• Base Case (*)
• None of the above
10. A non-linear recursive method can call how many copies of itself?
• 1
• 2 or more (*)
• None
Quiz 3 Sectiunea 7
1. What is a UML?
• Unidentified Molding Level, the level of access permitted by the default access specifier.
• Unified Modeling Language, a standardized language for modeling systems and structures in programming. (*)
• Universal Model Light, a program that reads the brightness of any given lightbulb.
• None of the above.
2. What does it mean to inherit a class?
• The subclass (or child class) gains access to any non-private methods and variables of the superclass (or parent class). (*)
• The access specifier has been set to private.
• A way of organizing the hierarchy of classes.
• Extending a method from a superclass.
3. Which of the following correctly defines a superclass (or parent class)?
• A class that inherits methods and fields from a more general class.
• The most specific class of a hierarchy system of classes.
• A class that passes down its methods to more specialized classes. (*)
• A keyword that allows or restricts access to data and methods.
4. Which of the following correctly defines a subclass (or child class)?
• A class that inherits methods and fields from a more general class. (*)
• A keyword that allows or restricts access to data and methods.
• A class that passes down its methods to more specialized classes.
• The most general class of a hierarchy system.
5. Methods are generally declared as public so other classes may use them. True or false?
True (*) False
6. Which is the most accurate description of the code reuse philosiphy?
• A programming philosophy that promotes stealing your classmates' code.
• A programming philosophy that promotes having no concern about the security of code.
• A programming philosophy that promotes protecting data and hiding implementation in order to preserve the integrity of data and methods.
• A programming philosophy that promotes simpler, more efficient coding by using existing code for new applications. (*)
7. Which of the following correctly describes the use of the keyword super?
• A keyword that restricts access to only inside the same class.
• A keyword that allows subclasses to access methods, data, and constructors from their parent class. (*)
• A keyword that signals the end of a program.
• A keyword that allows access from anywhere.
8. 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
• super.length(5)
• super.length = 5 (*)
• super(length = 5)
9. What is a hierarchy?
• A programming philosophy that promotes simpler, more efficient coding by using existing code for new applications.
• A programming philosophy that promotes protecting data and hiding implementation in order to preserve the integrity of data and methods.
• A keyword that allows subclasses to access methods, data, and constructors from their parent class.
• A structure that categorizes and organizes relationships among ideas and concepts of things with the most general at the top and the most specific at the bottom. (*)
10. It is possible to extend a class that already exists in Java, such as the Applet class. True or false? True (*) False
Quiz 4 Sectiunea 7
1. Why would a programmer use polymorphism rather than sticking to a standard array?
• Because arrays only work using the same object type and polymorphism provides a way around this. (*)
• Because it is easier to add or remove objects using polymorphism even when all of the objects are of the same type.
• Because arrays are more complex and polymorphism simplifies them by restricting them to only contain the same type objects.
• A programmer wouldn't use polymorphism over a standard array.
2. It is possible to override methods such as equals() and toString() in a subclass of Object to fit the needs of the objects of the subclass. True or false? True (*) False
3. Is there a difference between overriding a method and overloading a method?
• Yes. Overriding is done within a single class and overloading is done through a series of superclasses and their subclasses.
• Yes. Overriding allows for the creation of an array of different object types and overloading restricts an array to only contain the same object types.
• Yes. Overriding is done in the subclass and allows for redefining a method inherited from the superclass and overloading is done within a class and allows for multiple methods with the same name. (*)
• No, they are the same.
4. Which of the following is a goal of the object model?
• Providing modular code that can be reused by other programs or classes. (*)
• Concealing implementation. (*)
• Data abstraction. (*)
• Protecting information and limiting other classes' ability to change or corrupt data. (*)
5. If Sandal extends Shoe, it is possible to declare an object such that
Sandal s = new Shoe(); True False (*)
6. 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?
• Abstract classes
• Polymorphism
• Inheritance
• Dynamic Method Dispatch (*)
7. Which of the following are true about an abstract class?
• It is possible to create objects of this type.
• The Java Virtual Machine does not differentiate abstract classes from concrete classes.
• It is possible to create references of this type. (*)
• It is identified by the Java keyword abstract. (*)
1. What is a loop?
• A keyword used to skip over the remaining code.
• A set of logic that is repeatedly executed until a certain condition is met. (*)
• A segment of code that may only ever be executed once per call of the program.
• None of the above.
2. It is necessary to end all loops at some point in your Java program. True or false?
True (*) False
3. Which of the following are types of loops in Java?
• While (*)
• If/Else
• Do-While (*)
• For (*)
4. Identify which situation could be an example of a WHILE loop.
• Taking coins out of a pile one at a time and adding their value to the total until there are no more coins in the pile to add.
• Attending class while school is not over for the day.
• Petting each animal at the pet store one at a time until all the animals have been petted.
• All of the above. (*)
5. Which of the following correctly initializes a For loop that runs through 5 times?
• for(int i = 0; i == 6; i++)
• for(int i = 1; i < 6; i++) (*)
• for(int i = 0; i < 5; I++)
• for(int i = 1; i < 5; I++)
6. What is the function of the word "break" in Java?
• It exits the current loop or case statement. (*)
• It continues onto the next line of code.
• It stops the program from running.
• It does not exist in Java.
7. The following code fragment properly implements the switch statement. True or false?
default(input)
switch '+':
answer+=num;
break;
case '-':
answer-=num;
break;
!default
System.out.println("Invalid input"); True False (*)
8. What is one significant difference between a WHILE loop and a DO-WHILE loop?
• There is no difference between a DO-WHILE loop and a WHILE loop.
• A DO-WHILE loop does not exist in Java and a WHILE loop does.
• A DO-WHILE loop includes an int that serves as a counter and a WHILE loop does not.
• 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. (*)
9. Which of the following correctly initializes an instance of Scanner, called "in", that reads input from the console screen?
• Scanner in = new Scanner(System.in); (*)
• Scanner in = new Scanner("System.in");
• Scanner in = Scanner(System.in);
• System.in in = new Scanner();
Quiz 2 Sectiunea 5
1. Which of the following creates a class named Diver with one constructor, and 2 instance variables maxDepth and certified?
• (*)
•
•
•
2. The basic unit of encapsulation in Java is the:
class (*) classpath
method package
3. Which of the following creates a Object from the Animal class listed below?
• Animal dog=new Animal();
• Animal dog=new Animal(50,30); (*)
• Animal dog=Animal(50,30);
• Animal dog=new Animal(50);
4. Complete the sentence. A constructor...
• must have the same name as the class it is declared within.
• is used to create objects.
• may be declared public.
• is all of the above. (*)
5. The following code creates an Object of type Animal:
Animal a; True False (*)
6. Which of the following creates an object from the Car class listed below?
• Car c = new Car(3000, "Toyota"); (*)
• Car c=new Car;
• Car c=Car();
• Car c;
• Car c =new Car();
7. What is wrong with the following class declaration?
• Classes cannot include strings.
• Classes cannot include mixed data types.
• There is no constructor method and you have to make a constructor method.
• There is nothing wrong. (*)
8. Which of the following is true?
• In Java, a method declared public generates a compilation error.
• int is the name of a class available in the package java.lang.
• Instance variable names may only contain letters and digits.
• A class always has a constructor (possibly automatically supplied by the java compiler). (*)
• The more comments in a program, the faster the program runs.
9.What value will be returned when the setValue method is called?
• 35
• 36
• 37 (*)
• 38
10. The return value of a method can only be a primitive type and not an object. True or false?
True False (*)
11 The following statement compiles and executes. What can you say for sure?
submarine.dive(depth);
• Depth must be an int.
• Dive must be a method. (*)
• Dive must be the name of an instance field.
• Submarine must be the name of a class.
• Submarine must be a method.
12.Which of the following calls the method moveUp in the class below:
• Puzzle p=new Puzzle(); p.moveUp(3,4);
• PuzzlePiece p=new PuzzlePiece(); p.moveUp(3,4); (*)
• PuzzlePiece p=new PuzzlePiece(); p.moveUp(3,4,5);
• Puzzle p=new Puzzle(); p.moveUp(3,4,5);
13. A class can have multiple constructors. True or false? True (*) False
14. The constructor of a class has the same name as the class. True or false? True (*) False
15. Which of the following adds a constructor to the class below?
(*)
16. What operator do you use to call an object's constructor method and create a new object?
• new (*)
• class
• instanceOf
17. Consider: public class MyClass{ public MyClass(){/*code*/} // more code...}
To instantiate MyClass, what would you write?
MyClass m = new MyClass(); (*)
MyClass m = new MyClass;
MyClass m = MyClass;
MyClass m = MyClass();
18. Which of the following may be part of a class definition?
• instance variables
• instance methods
• constructors
• comments
• all of the above (*)
19. What is garbage collection in the context of Java?
• The operating system periodically deletes all of the Java files available on the system.
• Any package imported in a program and not used is automatically deleted.
• When all references to an object are gone, the memory used by the object is automatically reclaimed. (*)
• The JVM checks the output of any Java program and deletes anything that does not make sense.
20. Which of the following keywords are used to access the instance variables of an object from within the class code for that object?
• public
• private
• protected
• this (*)
21. Which constructor code populates the instance variables of the class correctly?
(*)
1. Which of the following statements is a valid array declaration?
• int number();
• float average[]; (*)
• double[] marks; (*)
• counter int[];
2.The following array declaration is valid: int[] y = new int[5]; True (*) False
3. Which of the following declares a one dimensional array named "score" of type int that can hold 9 values?
• int score;
• int[] score;
• int[] score=new int[9]; (*)
• int score=new int[9];
4. Which of the following declares and initializes a two dimensional array?
• int[][] array={{1,1,1},{1,1,1},{1,1,1}}; (*)
• int[] array={{1,1,1},{1,1,1},{1,1,1}};
• int[][] array={1,1,1},{1,1,1},{1,1,1};
• int[][] array={1,1,1,1,1,1,1,1,1};
5. Which of the following declares and initializes a one dimensional array named words of size 10 so that all entries can be Strings?
• String words=new String[10];
• char words=new char[10];
• char[] words=new char[10];
• String[] words=new String[10]; (*)
6. What is the output of the following segment of code?
• 222220
• 0 (*)
• 220
• 2
• This code does not compile.
7. What is the output of the following segment of code?
• 1286864
• 643432
• 262423242322
• 666666 (*)
• This code does not compile.
8. 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?
• String[][] values={"apples","oranges","pears"};
• String[][] values=new String[3][2];
• String[][] values=new String[2][3]; (*)
• String[][] values;
9. Which of the following declares and initializes a two dimensional array where each element is a reference type?
• String words=new String[10];
• char[][] words;
• char[][] words=new char[10][4];
• String[][] words=new String[10][3]; (*)
10. 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]);} (*)
• System.out.println(prices.length);
• for(int i=1; i <= prices.length; i++){System.out.println(prices[i]);}
• for(int i=0; i <= prices.length; i++){System.out.println(prices[i]);}
11. What is the output of the following segment of code?
• 753
• 6
• 7766554433221
• 7531 (*)
? This code does not compile.
12. The following creates a reference in memory named y that can refer to five different integers via an index. True or false? int[] y = new int[5]; True (*) False
13. The following creates a reference in memory named z that can refer to seven different doubles via an index. True or false? double z[] = new double[7]; True (*) False
14. What is the output of the following segment of code if the command line arguments are "apples oranges pears"?
• apples
• pears (*)
• oranges
• args
• This code doesn't compile.
15. What is the output of the following segment of code if the command line arguments are "apples oranges pears"?
• 0
• 1
• 2
• 3 (*)
• This code does not compile.
16. What will be the content of array variable table after executing the following code?
• 0 0 0
0 0 0
0 0 0
• 1 0 0
0 1 0
0 0 1 (*)
• 1 0 0
1 1 0
1 1 1
• 0 0 1
0 1 0
1 0 0
17.What is the output of the following segment of code?
• 1286864 (*)
• 643432
• 262423242322
• 666666
• This code does not compile.
18. After execution of the following statement, which of the following are true?
int number[] = new int[5];
• number[0] is undefined
• number[4] is null
• number[2] is 0 (*)
• number.length() is 6
19. The following array declaration is valid. True or false? int x[] = int[10]; True False (*)
Quiz 2 Sectiunea 6
1. Consider the following code snippet. What is printed?
String river = new String("Hudson"); System.out.println(river.length());
6 (*) 7 8 Hudson river
2. Which of the following creates a String reference named str and instantiates it?
• String str;
• str="str";
• String s="str";
• String str=new String("str"); (*)
3. 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 (*) False
4. Which of the following statements declares a String object called name?
• String name; (*)
• String name
• int name;
• double name;
5. Suppose that s1 and s2 are two strings. Which of the statements or expressions are valid?
• String s3 = s1 + s2; (*)
• String s3 = s1 - s2;
• s1 <= s2
• s1.compareTo(s2); (*)
• int m = s1.length(); (*)
6. Consider the following code snippet. What is printed?
• PoliiPolii (*)
• Polii
• auaacauaac
• auaac
• ArrayIndexOutofBoundsException is thrown
7. What will the following code segment output?
• "\\\\\"
• \"\\\\\"
• "\\" (*)
• "\\\"
8. What will the following code segment output?
• ""\\"
• ""\"
• ""\
" (*)
• """\
""
• ""\
""
9. The following program prints "Equal". True or false?
• True (*)
• False
10. Which of the following creates a String named string?
• char string;
• String s;
• String string; (*)
• String String;
• String char;
11. Given the code, which of the following would equate to true?
String s1 = "yes";
String s2 = "yes";
String s3 = new String(s1);
• s1 == s2 (*)
• s1 = s2
• s3 == s1
• s1.equals(s2) (*)
• s3.equals(s1) (*)
12. The String methods equals and compareTo perform the exact same function. True or false?
True False (*)
13. The == operator can be used to compare two String objects. The result is always true if the two strings are identical. True or false? True False (*)
14. The following program prints "Equal". True or false?
• True
• False (*)
15. Given the code below, which of the following calls are valid? String s = new String("abc");
• s.trim() (*)
• s.replace('a', 'A') (*)
• s.substring(2) (*)
• s.toUpperCase() (*)
• s.setCharAt(1,'A')
16. Consider the following code snippet. What is printed?
String ocean = new String("Atlantic Ocean"); System.out.println(ocean.indexOf('a'));
0 2 3 (*) 11 12
17. Consider the following code snippet. What is printed?
• 55555
• 87668 (*)
• AtlanticPacificIndianArcticSouthern
• The code does not compile.
• An ArrayIndexOutofBoundsException is thrown.
18. Consider the following code snippet. What is printed?
• 55555
• 87658
• AtlanticPacificIndianArcticSouthern
• The code does not compile.
• An ArrayIndexOutofBoundsException is thrown. (*)
19.How would you use the ternary operator to rewrite this if statement?
if (skillLevel > 5)
numberOfEnemies = 10;
else
numberOfEnemies = 5;
• numberOfEnemies = ( skillLevel > 5) ? 5 : 10;
• numberOfEnemies = ( skillLevel < 5) ? 10 : 5;
• numberOfEnemies = ( skillLevel >= 5) ? 5 : 10;
• numberOfEnemies = ( skillLevel >= 5) ? 10 : 5;
• numberOfEnemies = ( skillLevel > 5) ? 10 : 5; (*)
20. How would you use the ternary operator to rewrite this if statement?
if (gender == "male")
System.out.print("Mr.");
else
System.out.print("Ms.");
• System.out.print( (gender == "male") ? "Mr." : "Ms." ); (*)
• System.out.print( (gender == "male") ? "Ms." : "Mr." );
• (gender == "male") ? "Mr." : "Ms." ;
• (gender == "male") ? "Ms." : "Mr." ;
Quiz 3 Sectiunea 6
1. Which of the following would give you an array index out of bounds exception?
• Misspelling a variable name somewhere in your code.
• Refering to an element of an array that is at an index less than the length of the array minus one.
• Using a single equal symbol to compare the value of two integers.
• Refering to an element of an array that is at an index greater than the length of that array minus one. (*)
• Unintentionally placing a semicolon directly after initializing a for loop.
2. What exception message indicates that a variable may have been mispelled somewhere in the program?
• variableName cannot be resolved to a variable (*)
• method methodName(int) is undefined for the type className
• Syntax error, insert ";" to complete statement
• All of the Above
3. Which of the following defines an Exception?
• A very severe non-fixable problem with interpreting and running your code.
• Code that has no errors and therefore runs smothly.
• A problem that can be corrected or handled by your code. (*)
• An interpreter reading your code.
4. What do exceptions indicate in Java?
• The code has considered and dealt with all possible cases.
• A mistake was made in your code. (*)
• There are no errors in your code.
• Exceptions do not indicate anything, their only function is to be thrown.
• The code was not written to handle all possible conditions. (*)
5. Which line of code shows the correct way to throw an exception?
• new throw Exception("Array index is out of bounds");
• throw new Exception("Array index is out of bounds"); (*)
• throw Exception("Array index is out of bounds");
• throws new Exception("Array index is out of bounds");
6. What does the interpreter look for when an exception is thrown?
• It does not look for anything. It just keeps reading through your code.
• It does not look for anything. It stops interpreting your code.
• The end of the code.
• A catch statement in the code. (*)
7. Which of the following would be a correct way to handle an index out of bounds exception?
• Throw the exception and catch it. In the catch, set the index to the index of the array closest to the one that was out of bounds. (*)
• Do nothing, it will fix itself.
• Throw the exception that prints out an error message. There is no need to have the catch handle the exception if it has already been thrown.
• Rewrite your code to avoid the exception by not permititng the use of an index that is not inside the array. (*)
8. A computer company has one million dollars to give as a bonus to the employees, and they wish to distribute it evenly amongst them.
The company writes a program to calculate the amount each employee receives, given the number of employees.
Unfortunately, the employees all went on strike before they heard about the bonus. This means that the company has zero employees.
What will happen to the program if the company enters 0 into the employment number?
• An unfixable error will occur.
• The program will calculate that each employee will receive zero dollars because there are zero employees.
• An exception will occur because it is not possible to divide by zero. (*)
• The programmers will have proven their worth in the company because without them the company wrote faulty code.
1. The ______________ is the location into which you will store and save your files. Mark for Review
(1) Points
Perspective
Workspace (*)
Editor
None of the above
2. Multiple windows are used when more than one file is open in the edit area. True or False? Mark for Review
(1) Points
True
False (*)
3. A workspace can have one or more stored projects. True or false? Mark for Review
(1) Points
True (*)
False
4. Tabs are used when more than one file is open in the edit area. True or False? Mark for Review
(1) Points
True (*)
False
5. A _______________ is used to organize Java related files. Mark for Review
(1) Points
Collection
Project
Package (*)
Workspace
Section 4
(Answer all questions in this section)
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. The following defines a package keyword: Mark for Review
(1) Points
Provides the compiler information that identifies outside classes used within the current class.
Precedes the name of the class.
Defines where this class lives relative to other classes, and provides a level of access control. (*)
8. The following code is an example of a correct initialization statement:
char c="c"; Mark for Review
(1) Points
True
False (*)
9. Suppose that str1 and str2 are two strings. Which of the statements or expressions are valid? Mark for Review
(1) Points
Str1 -= str2;
str1 >= str2
str1 += str2; (*)
String str3 = str1 - str2;
10. Which of the following creates a String named string? Mark for Review
(1) Points
String String;
String s;
String char;
char string;
String string; (*)
Section 4
(Answer all questions in this section)
11. Which of the following creates a String reference named s and instantiates it? Mark for Review
(1) Points
(Choose all correct answers)
s="s";
String s;
String s=new String("s"); (*)
String s=""; (*)
12. Which of the following creates a String reference named str and instantiates it? Mark for Review
(1) Points
str="str";
String str=new String("str"); (*)
String str;
String s="str";
13. Which line of code does not assign 3.5 to the variable x? Mark for Review
(1) Points
x=7.0/2.0;
x=3.5;
double x=3.5
3.5=x; (*)
14. Which line of Java code assigns the value of 5 raised to the power of 8 to a? Mark for Review
(1) Points
int a=Math.pow(8,5);
double a=15^8;
double a=Math.pow(5,8); (*)
int a=Math.pow(5,8);
double a=pow(8,5);
15. What two values can a boolean variable have? Mark for Review
(1) Points
Integers and floating point types
Arithmetic and logic operators
True and false (*)
Relational and logic operators
Numbers and characters
Section 5
(Answer all questions in this section)
16. The following prints Yes on the screen. True or false?
Mark for Review
(1) Points
True
False (*)
17. How would you use the ternary operator to rewrite this if statement?
if (gender == "female") System.out.print("Ms.");
else
System.out.print("Mr."); Mark for Review
(1) Points
System.out.print( (gender == "female") ? "Mr." : "Ms." );
(gender == "female") ? "Ms." : "Mr." ;
System.out.print( (gender == "female") ? "Ms." : "Mr." ); (*)
(gender == "female") ? "Mr." : "Ms." ;
18. Which of the following expressions will evaluate to true when x and y are boolean variables with opposite values?
I. (x || y) && !(x && y)
II. (x && !y) || (!x && y)
III. (x || y) && (!x ||!y) Mark for Review
(1) Points
I only
II only
I and III
II and III
I, II, and III (*)
19. Why are loops useful? Mark for Review
(1) Points
They save programmers from having to rewrite code.
They allow for repeating code a variable number of times.
They allow for repeating code until a certain argument is met.
All of the above. (*)
20. A counter used in a for loop cannot be initialized within the For loop header. True or false? Mark for Review
(1) Points
True
False (*)
Section 5
(Answer all questions in this section)
21. Which of the following best describes a while loop? Mark for Review
(1) Points
A loop that contains a counter in parenthesis with the conditional statement.
A loop that executes the code at least one time even if the conditional statement is false.
A loop that is executed repeatedly until the conditional statement is false. (*)
A loop that contains a segment of code that is executed before the conditional statement is tested.
Section 6
(Answer all questions in this section)
22. 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? Mark for Review
(1) Points
True
False (*)
23. What does it mean to catch an exception? Mark for Review
(1) Points
It means you have fixed the error.
It means there was never an exception in your code.
It means to handle it. (*)
It means to throw it.
24. What do exceptions indicate in Java? Mark for Review
(1) Points
(Choose all correct answers)
The code has considered and dealt with all possible cases.
The code was not written to handle all possible conditions. (*)
A mistake was made in your code. (*)
Exceptions do not indicate anything, their only function is to be thrown.
There are no errors in your code.
25. Which of the following would be a correct way to handle an index out of bounds exception? Mark for Review
(1) Points
(Choose all correct answers)
Rewrite your code to avoid the exception by not permititng the use of an index that is not inside the array. (*)
Throw the exception that prints out an error message. There is no need to have the catch handle the exception if it has already been thrown.
Throw the exception and catch it. In the catch, set the index to the index of the array closest to the one that was out of bounds. (*)
Do nothing, it will fix itself.
Section 6
(Answer all questions in this section)
26. The following creates a reference in memory named k that can refer to six different integers via an index. True or false?
int k[]= int[6]; Mark for Review
(1) Points
True
False (*)
27. What is the output of the following segment of code?
int num[]={9,8,7,6,5,4,3,2,1};
for(int i=0;i<9 i="i+3)" o:p="">
System.out.print(num[i]); Mark for Review
(1) Points
97531
987654321
9630
963 (*)
This code doesn't compile.
28. What is the output of the following segment of code?
int array[][] = {{1,2,3},{3,2,1}};
for(int i=0;i<2 i="" o:p="">
for(int j=0;j<3 j="" o:p="">
System.out.print(2*array[1][1]); Mark for Review
(1) Points
246642
444444 (*)
222222
This code doesn't compile.
123321
29. What is the output of the following segment of code?
Mark for Review
(1) Points
1286864
643432
666666 (*)
262423242322
This code does not compile.
Section 7
(Answer all questions in this section)
30. Which of the following could be a reason to return an object? Mark for Review
(1) Points
Because you wish to be able to use that object inside of the method.
It has faster performance than returning a primitive type.
The method makes changes to the object and you wish to continue to use the updated object outside of the method. (*)
None of the above. It is not possible to return an object.
Section 7
(Answer all questions in this section)
31. Which of the following is the definition for a variable argument method? Mark for Review
(1) Points
A way to create a new class.
A type of argument that enables calling the same method with a different number of arguments. (*)
Having more than one constructor with the same name but different arguments.
Specifies accessibility to code.
32. You are assigned to write a method that compares two objects of type Career. One requirement of your assignment is to have your method compare the "greatestPossibleSalary" instance data of Career objects. The "greatestPossibleSalary" field is data type int.
What would be the best return type from your compare method? Mark for Review
(1) Points
Array, because it can store the most information.
Integer, because it is the easiest to code with.
Career, because if it returns the highest paying Career object it will be able to use the same method later to compare other aspects of Career objects. (*)
String, because is should return a string of the name of the career that is highest paying because none of the other information of the career matters.
33. What type(s) would work for a variable argument method? Mark for Review
(1) Points
(Choose all correct answers)
Integers, Strings, and Booleans (*)
Constructors
Arrays (*)
Objects (*)
All of the above
34. 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. Mark for Review
(1) Points
public
protected
private (*)
default
All of the above
35. Methods are generally declared as public so other classes may use them. True or false? Mark for Review
(1) Points
True (*)
False
Section 7
(Answer all questions in this section)
36. Which of the following correctly describes an "is-a" relationship? Mark for Review
(1) Points
A helpful term used to conceptualize the relationships among nodes or leaves in an inheritance hierarchy. (*)
A programming philosophy that promotes simpler, more efficient coding by using exiting code for new applications.
A programming philosophy that promotes protecting data and hiding implementation in order to preserve the integrity of data and methods.
It restricts access to a specified segment of code.
37. Which of the following demonstrates the correct way to create an applet Battlefield? Mark for Review
(1) Points
public class Applet extends Battlefield{...}
public class Battlefield(Applet){...}
public class Battlefield extends Applet{...} (*)
public Applet Battlefield{...}
38. What is encapsulation? Mark for Review
(1) Points
A structure that categorizes and organizes relationships among ideas, concepts of things with the most general at the top and the most specific at the bottom.
A keyword that allows or restricts access to data and methods.
A programming philosophy that promotes simpler, more efficient coding by using exiting code for new applications.
A programming philosophy that promotes protecting data and hiding implementation in order to preserve the integrity of data and methods. (*)
39. The following code creates an object of type Horse:
Whale a=new Whale(); Mark for Review
(1) Points
True
False (*)
40. Which of the following keywords are used to access the instance variables of an object from within the class code for that object? Mark for Review
(1) Points
private
protected
public
this (*)
Section 7
(Answer all questions in this section)
41. Which constructor code populates the instance variables of the class correctly? Mark for Review
(1) Points
(*)
42. Which of the following calls the method calculate correctly?
Mark for Review
(1) Points
ThisClass t=new ThisClass(); int x=t.calculate(3);
ThisClass t=new ThisClass(); int x=t.calculate(3,4); (*)
int x=calculate(3,4);
ThisClass t=new ThisClass(); int x=t.calculate();
43. Which of the following creates a method that returns a boolean value? Mark for Review
(1) Points
(*)
None of the above.
44. A constructor must have the same name as the class where it is declared. True or false? Mark for Review
(1) Points
True (*)
False
45. Which of the following is a goal of the object model? Mark for Review
(1) Points
(Choose all correct answers)
Concealing implementation. (*)
Providing modular code that can be reused by other programs or classes. (*)
Protecting information and limiting other classes' ability to change or corrupt data. (*)
Data abstraction. (*)
Section 7
(Answer all questions in this section)
46. Which of the following can be declared final? Mark for Review
(1) Points
Classes
Methods
Local variables
Method parameters
All of the above (*)
47. If we override the toString() method with the code below, what would be the result of printing?
Mark for Review
(1) Points
It would print the array backwards. The console screen would display: 42 11 64 215 18 0
It would print the string returned from the method. The console screen would display: {0, 18, 215, 64, 11, 42}
It would print the array one element at a time. The console screen would display: 0 18 215 64 11 42
It would print the string returned from the method. The console screen would display: [0,18,215,64,11,42,] (*)
48. You can use an inner static class to return an instance of its outer class container. True or false? Mark for Review
(1) Points
True (*)
False
49. You can return an instance of a private class through a static method of a different class. True or false? Mark for Review
(1) Points
True
False (*)
50. You can assign new values to static variables by prefacing them with the this keyword and a dot or period. True or false? Mark for Review
(1) Points
True (*)
False
1. The following segment of code initializes a 2 dimensional array of primitive data types. True or false?
double[][] a=new double[4][5]; Mark for Review
(1) Points
True (*)
False
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? Mark for Review
(1) Points
String[][] values={"apples","oranges","pears"};
String[][] values=new String[2][3]; (*)
String[][] values;
String[][] values=new String[3][2];
3. double array[] = new double[8];
After execution of this statement, which of the following are true? Mark for Review
(1) Points
array.length is 8 (*)
array[0] is undefined
array[2] is 8
array[4] is null
4. What is the output of the following segment of code if the command line arguments are "apples oranges pears"?
Mark for Review
(1) Points
oranges
This code does not compile.
args
pears (*)
apples
5. What is the output of the following segment of code if the command line arguments are "a b c d e f g"?
Mark for Review
(1) Points
c
e (*)
d
f
This code doesn't compile.
6. Which of the following statements is not a valid array declaration? Mark for Review
(1) Points
float []averages;
double marks[5];
counter int[]; (*)
int number[];
7. What is the output of the following segment of code?
Mark for Review
(1) Points
11 (*)
111
1111
321111
This code doesn't compile.
8. What is the output of the following segment of code?
Mark for Review
(1) Points
555555
777777 (*)
This code doesn't compile.
456789
987654
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]; Mark for Review
(1) Points
True (*)
False
10. What is the output of the following segment of code?
Mark for Review
(1) Points
666666 (*)
643432
262423242322
1286864
This code does not compile.
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? Mark for Review
(1) Points
(Choose all correct answers)
An error would occur. Errors cannot be handled by code.
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. (*)
An exception would occur but could not be handled inside your code. The user would have to restart the program and enter proper coordinates.
12. Which of the following defines an Exception? Mark for Review
(1) Points
A very severe non-fixable problem with interpreting and running your code.
An interpreter reading your code.
A problem that can be corrected or handled by your code. (*)
Code that has no errors and therefore runs smothly.
13. What do exceptions indicate in Java? Mark for Review
(1) Points
(Choose all correct answers)
The code was not written to handle all possible conditions. (*)
There are no errors in your code.
A mistake was made in your code. (*)
The code has considered and dealt with all possible cases.
Exceptions do not indicate anything, their only function is to be thrown.
14. It is possible to throw and catch a second exception inside a catch block of code. True or false? Mark for Review
(1) Points
True (*)
False
15. What exception message indicates that a variable may have been mispelled somewhere in the program? Mark for Review
(1) Points
variableName cannot be resolved to a variable (*)
method methodName(int) is undefined for the type className
Syntax error, insert ";" to complete statement
All of the above
1. What does the interpreter look for when an exception is thrown? Mark for Review
(1) Points
It does not look for anything. It stops interpreting your code.
The end of the code.
It does not look for anything. It just keeps reading through your code.
A catch statement in the code. (*)
2. Which of the following could be a reason to throw an exception? Mark for Review
(1) Points
To make the user interface harder to navigate.
To eliminate exceptions from disrupting your program. (*)
You have encountered a Stack Overflow Error.
You have a fatal error in your program.
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? Mark for Review
(1) Points
True
False (*)
4. Which line of code shows the correct way to throw an exception? Mark for Review
(1) Points
throw Exception("Array index is out of bounds");
throws new Exception("Array index is out of bounds");
new throw Exception("Array index is out of bounds");
throw new Exception("Array index is out of bounds"); (*)
5. If an exception has already been thrown, what will the interpreter read next in the program? Mark for Review
(1) Points
Where the program catches the exception. (*)
The next line of the program even if it is not the catch block of code.
The end of the program.
The user input.
Section 6 Quiz
(Answer all questions in this section)
6. What is the output of the following segment of code?
Mark for Review
(1) Points
987654
555555
777777 (*)
456789
This code doesn't compile.
7. Which of the following declares and initializes a two dimensional array where each element is a reference type? Mark for Review
(1) Points
String words=new String[10];
char[][] words;
String[][] words=new String[10][3]; (*)
char[][] words=new char[10][4];
8. Which of the following declares and initializes a two dimensional array? Mark for Review
(1) Points
int[][] array={1,1,1,1,1,1,1,1,1};
int[][] array={1,1,1},{1,1,1},{1,1,1};
int[][] array={{1,1,1},{1,1,1},{1,1,1}}; (*)
int[] array={{1,1,1},{1,1,1},{1,1,1}};
9. The following array declaration is valid:
int[] y = new int[5]; Mark for Review
(1) Points
True (*)
False
10. The following segment of code initializes a 2 dimensional array of primitive data types. True or false?
double[][] a=new double[4][5]; Mark for Review
(1) Points
True (*)
False
11. Which of the following statements is not a valid array declaration? Mark for Review
(1) Points
double marks[5];
int number[];
counter int[]; (*)
float []averages;
12. double array[] = new double[8];
After execution of this statement, which of the following are true? Mark for Review
(1) Points
array[0] is undefined
array.length is 8 (*)
array[4] is null
array[2] is 8
13. What is the output of the following segment of code?
Mark for Review
(1) Points
321123
312213
642246 (*)
642
This code doesn't compile.
14. The following segment of code prints all five of the command line arguments entered into this program. True or false?
Mark for Review
(1) Points
True
False (*)
15. Which of the following statements print every element of the one dimensional array prices to the screen? Mark for Review
(1) Points
System.out.println(prices.length);
for(int i=0; i <= prices.length; i++){System.out.println(prices[i]);}
for(int i=1; i <= prices.length; i++){System.out.println(prices[i]);}
for(int i=0; i < prices.length; i++){System.out.println(prices[i]);} (*)
1. What are Java's simple types?
boolean, byte, char, double, float, int, long, and short (*)
boolean, byte, string, thread, int, double, long and short
object, byte, string, char, float, int, long and short
boolean, thread, stringbuffer, char, int, float, long and short
boolean, thread, char, double, float, int, long and short
2. Which of the following are relational operators in Java? (Choose all correct answers)
< (*)
<= (*)
=
!= (*)
All of the above.
3. What is the output of the following lines of code?
int j=6,k=4,m=12,result;
result=j/m*k;
System.out.println(result);
2
0 (*)
48
24
4. A local variable has precedence over a global variable in a Java method. True or false?
True (*) False
5. What does the following program output?
total cost: + 40
total cost: 48
total cost: 40 (*)
"total cost: " 48
"total cost: " 40
6. What is the result when the following code segment is compiled and executed?
int x = 22, y = 10;
double p = Math.sqrt( ( x + y ) /2);
System.out.println(p);
Syntax error "sqrt(double) in java.lang.Math cannot be applied to int"
4.0 is displayed (*)
2.2 is displayed
5.656854249492381 is displayed
ClassCastException
7. Determine whether this boolean expression evaluates to true or false:
!(3<4&&6>6||6<=6&&7-2==6)
True (*) False
8. In an if-else construct the condition to be evaluated must end with a semi-colon. True or false?
True False (*)
9. Which of the two diagrams below illustrate the general form of a Java program?
Example A
Example B (*)
10. In a For loop the counter is not automatically incremented after each loop iteration. Code must be written to increment the counter. True or false?
True (*) False
11. When the For loop condition statement is met the construct is exited. True or false?
True False (*)
12. You can return to the Eclipse Welcome Page by choosing Welcome from what menu?
File
Edit
Help (*)
Close
13. In Eclipse, when you run a Java Application, where may the results display?
Editor Window
Console View (*)
Debug View
Task List
None of the above
14. A combination of views and editors are referred to as _______________.
A workspace
A physical location
A perspective (*)
All of the above
15. What are the Eclipse Editor Area and Views used for?(Choose all correct answers)
To modify elements. (*)
To navigate a hierarchy of information. (*)
To choose the file system location to delete a file.
16. What is the output of the following code segment:
int n = 13;
System.out.print(doNothing(n));
System.out.print(" ", n);
where the code from the function doNothin is:
public double doNothing(int n)
{
n = n + 8;
return (double) 12/n;
}
1.75, 13
0.571, 21
1.75, 21
0.571, 13 (*)
17. 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 (*) False
18. One advantage to using a WHILE loop over a FOR loop is that a WHILE loop always has a counter. True or false? True False (*)
19. Which of the following could be a reason to use a switch statement in a Java program?
Because it allows the code to be run through until a certain conditional statement is true.
Because it allows the program to run certain segments of code and neglect to run others based on the input given. (*)
Because it terminates the current loop.
Because it allows the user to enter an input in the console screen and prints out a message that the user input was successfully read in.
20. In Java, an instance field referenced using the this keyword generates a compilation error. True or false?
True False (*)
21. Consider
public class YourClass{ public YourClass(int i){/*code*/} // more code...}
To instantiate YourClass, what would you write?
YourClass y = new YourClass();
YourClass y = new YourClass(3); (*)
YourClass y = YourClass(3);
YourClass y = YourClass();
None of the above.
22. A constructor must have the same name as the class it is declared within. True or false?
True (*) False
23. Which of the following keywords are used to control access to the member of a class?
default
public (*)
class
All of the above.
None of the above.
24. Which of the following creates a method that compiles with no errors in the class?
(*)
All of the above.
None of the above
25. The following code creates an Object of type Horse. True or false?
Whale a=new Whale();
True False (*)
26. What operator do you use to call an object's constructor method and create a new object?
+
new (*)
instanceOf
27. Which of the following declares a one dimensional array name scores of type int that can hold 14 values?
int scores;
int[] scores=new int[14]; (*)
int[] scores=new int[14];
int score= new int[14]
28. Which of the following statements is not a valid array declaration?
int number[];
float []averages;
double marks[5];
counter int[]; (*)
29. What is the output of the following segment of code if the command line arguments are "a b c d e f"?
1
3
5
6 (*)
30. Which of the following declares a one dimensional array named names of size 8 so that all entries can be Strings?
String names=new String[8];
String[] name=new Strings[8];
String[] names=new String[8]; (*)
String[] name=String[8];
31. What will the following code segment output?
String s="\\\\\
System.out.println(s);
"\\\\\"
\\\\\\\\
\\
\\\\ (*)
32. Consider the following code snippet.
What is printed? Mark for Review
(1) Points
88888 (*)
88888888
1010778
101077810109
ArrayIndexOutofBoundsException is thrown
33. Given the code
String s1 = "abcdef";
String s2 = "abcdef";
String s3 = new String(s1);
Which of the following would equate to false?
s1 == s2
s1 = s2
s3 == s1 (*)
s1.equals(s2)
s3.equals(s1)
34. How would you use the ternary operator to rewrite this if statement?
if (balance < 500)
fee = 10;
else
fee = 0;
fee = ( balance < 500) ? 0 : 10;
fee= ( balance < 500) ? 10 : 0; (*)
fee = ( balance >= 5) ? 0 : 10;
fee = ( balance >= 500) ? 10 : 0;
fee = ( balance > 5) ? 10 : 0;
35. If an exception is thrown by a method, where can the catch for the exception be?
There does not need to be a catch in this situation.
The catch must be in the method that threw the exception.
The catch can be in the method that threw the exception or in any other method that called the method that threw the exception. (*)
The catch must be immediately after the throw.
36. Choose the best response to this statement: An error can be handled by throwing it and catching it just like an exception.
True. Errors and exceptions are the same objects and are interchangeable.
False. An error is much more severe than an exception and cannot be dealt with adequately in a program. (*
True. Although errors may be more severe than exceptions they can still be handled in code the same way exceptions are. False. Exceptions are caused by a mistake in the code and errors occur for no particular reason and therefore cannot be handled or avoided.
37. Which of the following could be a reason to throw an exception?
To eliminate exceptions from disrupting your program. (*)
You have a fatal error in your program.
You have encountered a Stack Overflow Error.
To make the user interface harder to navigate.
38. Suppose you misspell a method name when you call it in your program. Which of the following explains why this gives you an exception?
Because the parameters of the method were not met.
Because the interpreter does not recognize this method since it was never initialized, the correct spelling of the method was initialized.
Because the interpreter tries to read the method but when it finds the method you intended to use it crashes.
This will not give you an exception, it will give you an error when the program is compiled. (*)
39. Which of the following is the correct way to call an overriden method needOil() of a super class Robot in a subclass SqueakyRobot?
Robot.needOil(SqueakyRobot);
SqueakyRobot.needOil();
super.needOil(); (*)
needOil(Robot);
40. Why are hierarchies useful for inheritance?
They keep track of where you are in your program.
They restrict a superclass to only have one subclass.
They organize constructors and methods in a simplified fashion.
They are used to organize the relationship between a superclass and its subclasses. (*)
41. It is possible for a subclass to be a superclass. True or false?
True (*) False
42. Static methods can write to instance variables. True or false?
True False (*)
43. Static classes are designed as thread safe class instances. True or false?
True False (*)
44. Public static variables can't have their value reset by other classes. True or false?
True False (*)
45. Choose the correct implementation of a public access modifier for the method divide.
divide(int a, int b, public) {return a/b;}
public divide(int a, int b) {return a/b;} (*)
divide(int a, int b) {public return a/b;}
divide(public int a, public int b) {return a/b;}
46. Which of the following specifies accessibility to variables, methods, and classes?
Methods
Parameters
Overload constructors
Access specifiers (*)
47. 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(String a, int b);
counter(int[] numbers);
counter(1, 5, 8, 17, 11000005); (*)
counter("one","two",String[] nums);
48. Which of the following can be declared final?
Classes
Methods
Local variables
Method parameters
All of the above (*)
49. Which of the following would be most beneficial for this scenario?
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?
By ignoring the subject type and initializing all the book as objects of type Book.
By overriding the methods of Book.
Using polymorphism. (*)
This is not possible. Joe must find another way to collect the books.
50. What is Polymorphism?
A way of redefining methods with the same return type and parameters.
A way to create multiple methods with the same name but different parameters.
A class that cannot be initiated.
The concept that a variable or reference can hold multiple types of objects. (*)
Quiz 1 Sectiunea 6
1. Which of the following statements is a valid array declaration?
• int number();
• float average[]; (*)
• double[] marks; (*)
• counter int[];
2.The following array declaration is valid: int[] y = new int[5]; True (*) False
3. Which of the following declares a one dimensional array named "score" of type int that can hold 9 values?
• int score;
• int[] score;
• int[] score=new int[9]; (*)
• int score=new int[9];
4. Which of the following declares and initializes a two dimensional array?
• int[][] array={{1,1,1},{1,1,1},{1,1,1}}; (*)
• int[] array={{1,1,1},{1,1,1},{1,1,1}};
• int[][] array={1,1,1},{1,1,1},{1,1,1};
• int[][] array={1,1,1,1,1,1,1,1,1};
5. Which of the following declares and initializes a one dimensional array named words of size 10 so that all entries can be Strings?
• String words=new String[10];
• char words=new char[10];
• char[] words=new char[10];
• String[] words=new String[10]; (*)
6. What is the output of the following segment of code?
• 222220
• 0 (*)
• 220
• 2
• This code does not compile.
7. What is the output of the following segment of code?
• 1286864
• 643432
• 262423242322
• 666666 (*)
• This code does not compile.
8. 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?
• String[][] values={"apples","oranges","pears"};
• String[][] values=new String[3][2];
• String[][] values=new String[2][3]; (*)
• String[][] values;
9. Which of the following declares and initializes a two dimensional array where each element is a reference type?
• String words=new String[10];
• char[][] words;
• char[][] words=new char[10][4];
• String[][] words=new String[10][3]; (*)
10. 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]);} (*)
• System.out.println(prices.length);
• for(int i=1; i <= prices.length; i++){System.out.println(prices[i]);}
• for(int i=0; i <= prices.length; i++){System.out.println(prices[i]);}
11. What is the output of the following segment of code?
• 753
• 6
• 7766554433221
• 7531 (*)
? This code does not compile.
12. The following creates a reference in memory named y that can refer to five different integers via an index. True or false? int[] y = new int[5]; True (*) False
13. The following creates a reference in memory named z that can refer to seven different doubles via an index. True or false? double z[] = new double[7]; True (*) False
14. What is the output of the following segment of code if the command line arguments are "apples oranges pears"?
• apples
• pears (*)
• oranges
• args
• This code doesn't compile.
15. What is the output of the following segment of code if the command line arguments are "apples oranges pears"?
• 0
• 1
• 2
• 3 (*)
• This code does not compile.
16. What will be the content of array variable table after executing the following code?
• 0 0 0
0 0 0
0 0 0
• 1 0 0
0 1 0
0 0 1 (*)
• 1 0 0
1 1 0
1 1 1
• 0 0 1
0 1 0
1 0 0
17.What is the output of the following segment of code?
• 1286864 (*)
• 643432
• 262423242322
• 666666
• This code does not compile.
18. After execution of the following statement, which of the following are true?
int number[] = new int[5];
• number[0] is undefined
• number[4] is null
• number[2] is 0 (*)
• number.length() is 6
19. The following array declaration is valid. True or false? int x[] = int[10]; True False (*)
Quiz 2 Sectiunea 6
1. Consider the following code snippet. What is printed?
String river = new String("Hudson"); System.out.println(river.length());
6 (*) 7 8 Hudson river
2. Which of the following creates a String reference named str and instantiates it?
• String str;
• str="str";
• String s="str";
• String str=new String("str"); (*)
3. 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 (*) False
4. Which of the following statements declares a String object called name?
• String name; (*)
• String name
• int name;
• double name;
5. Suppose that s1 and s2 are two strings. Which of the statements or expressions are valid?
• String s3 = s1 + s2; (*)
• String s3 = s1 - s2;
• s1 <= s2
• s1.compareTo(s2); (*)
• int m = s1.length(); (*)
6. Consider the following code snippet. What is printed?
• PoliiPolii (*)
• Polii
• auaacauaac
• auaac
• ArrayIndexOutofBoundsException is thrown
7. What will the following code segment output?
• "\\\\\"
• \"\\\\\"
• "\\" (*)
• "\\\"
8. What will the following code segment output?
• ""\\"
• ""\"
• ""\
" (*)
• """\
""
• ""\
""
9. The following program prints "Equal". True or false?
• True (*)
• False
10. Which of the following creates a String named string?
• char string;
• String s;
• String string; (*)
• String String;
• String char;
11. Given the code, which of the following would equate to true?
String s1 = "yes";
String s2 = "yes";
String s3 = new String(s1);
• s1 == s2 (*)
• s1 = s2
• s3 == s1
• s1.equals(s2) (*)
• s3.equals(s1) (*)
12. The String methods equals and compareTo perform the exact same function. True or false?
True False (*)
13. The == operator can be used to compare two String objects. The result is always true if the two strings are identical. True or false? True False (*)
14. The following program prints "Equal". True or false?
• True
• False (*)
15. Given the code below, which of the following calls are valid? String s = new String("abc");
• s.trim() (*)
• s.replace('a', 'A') (*)
• s.substring(2) (*)
• s.toUpperCase() (*)
• s.setCharAt(1,'A')
16. Consider the following code snippet. What is printed?
String ocean = new String("Atlantic Ocean"); System.out.println(ocean.indexOf('a'));
0 2 3 (*) 11 12
17. Consider the following code snippet. What is printed?
• 55555
• 87668 (*)
• AtlanticPacificIndianArcticSouthern
• The code does not compile.
• An ArrayIndexOutofBoundsException is thrown.
18. Consider the following code snippet. What is printed?
• 55555
• 87658
• AtlanticPacificIndianArcticSouthern
• The code does not compile.
• An ArrayIndexOutofBoundsException is thrown. (*)
19.How would you use the ternary operator to rewrite this if statement?
if (skillLevel > 5)
numberOfEnemies = 10;
else
numberOfEnemies = 5;
• numberOfEnemies = ( skillLevel > 5) ? 5 : 10;
• numberOfEnemies = ( skillLevel < 5) ? 10 : 5;
• numberOfEnemies = ( skillLevel >= 5) ? 5 : 10;
• numberOfEnemies = ( skillLevel >= 5) ? 10 : 5;
• numberOfEnemies = ( skillLevel > 5) ? 10 : 5; (*)
20. How would you use the ternary operator to rewrite this if statement?
if (gender == "male")
System.out.print("Mr.");
else
System.out.print("Ms.");
• System.out.print( (gender == "male") ? "Mr." : "Ms." ); (*)
• System.out.print( (gender == "male") ? "Ms." : "Mr." );
• (gender == "male") ? "Mr." : "Ms." ;
• (gender == "male") ? "Ms." : "Mr." ;
Quiz 3 Sectiunea 6
1. Which of the following would give you an array index out of bounds exception?
• Misspelling a variable name somewhere in your code.
• Refering to an element of an array that is at an index less than the length of the array minus one.
• Using a single equal symbol to compare the value of two integers.
• Refering to an element of an array that is at an index greater than the length of that array minus one. (*)
• Unintentionally placing a semicolon directly after initializing a for loop.
2. What exception message indicates that a variable may have been mispelled somewhere in the program?
• variableName cannot be resolved to a variable (*)
• method methodName(int) is undefined for the type className
• Syntax error, insert ";" to complete statement
• All of the Above
3. Which of the following defines an Exception?
• A very severe non-fixable problem with interpreting and running your code.
• Code that has no errors and therefore runs smothly.
• A problem that can be corrected or handled by your code. (*)
• An interpreter reading your code.
4. What do exceptions indicate in Java?
• The code has considered and dealt with all possible cases.
• A mistake was made in your code. (*)
• There are no errors in your code.
• Exceptions do not indicate anything, their only function is to be thrown.
• The code was not written to handle all possible conditions. (*)
5. Which line of code shows the correct way to throw an exception?
• new throw Exception("Array index is out of bounds");
• throw new Exception("Array index is out of bounds"); (*)
• throw Exception("Array index is out of bounds");
• throws new Exception("Array index is out of bounds");
6. What does the interpreter look for when an exception is thrown?
• It does not look for anything. It just keeps reading through your code.
• It does not look for anything. It stops interpreting your code.
• The end of the code.
• A catch statement in the code. (*)
7. Which of the following would be a correct way to handle an index out of bounds exception?
• Throw the exception and catch it. In the catch, set the index to the index of the array closest to the one that was out of bounds. (*)
• Do nothing, it will fix itself.
• Throw the exception that prints out an error message. There is no need to have the catch handle the exception if it has already been thrown.
• Rewrite your code to avoid the exception by not permititng the use of an index that is not inside the array. (*)
8. A computer company has one million dollars to give as a bonus to the employees, and they wish to distribute it evenly amongst them.
The company writes a program to calculate the amount each employee receives, given the number of employees.
Unfortunately, the employees all went on strike before they heard about the bonus. This means that the company has zero employees.
What will happen to the program if the company enters 0 into the employment number?
• An unfixable error will occur.
• The program will calculate that each employee will receive zero dollars because there are zero employees.
• An exception will occur because it is not possible to divide by zero. (*)
• The programmers will have proven their worth in the company because without them the company wrote faulty code.
1. Select the statement that declares a number of type double and initializes it to 6 times 10 to the 5th power.
double number=6*10^5;
double number=6e5; (*)
double number=6(e5);
double number=6*10e5;
2. Which of the following expressions will evaluate to true when x and y are boolean variables with opposite values?
I. (x || y) && !(x && y)
II. (x && !y) || (!x && y)
III. (x || y) && (!x ||!y)
I only
II only
I and III
II and III
I, II, and III (*)
3. The six relational operators in Java are:
>,<,=,!,<=,>=
>,<,==,!=,<=,>= (*)
>,<,=,!=,<=,>=
>,<,=,!=,=<,=>
4. Examine the following code: What is the value of variable x?
2 (*)
2.5
6
14
5. What is the result when the following code segment is compiled and executed?
int x = 22, y = 10;
double p = Math.sqrt( ( x + y ) /2);
System.out.println(p);
Syntax error "sqrt(double) in java.lang.Math cannot be applied to int"
4.0 is displayed (*)
2.2 is displayed
5.656854249492381 is displayed
ClassCastException
6. Which of the following is not a legal name for a variable?
2bad (*)
zero
theLastValueButONe
year2000
7. What is the output of the following lines of code?
int j=6,k=8,m=2,result;
result=j-k%3*m;
System.out.println(result);
6
0
-42
2 (*)
8. 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?
True
False (*)
9. Which of the two diagrams below illustrate the correct syntax for variables used in an if-else statement?
Example A (*)
Example B
10. In an if-else construct the condition to be evaluated must end with a semi-colon. True or false?
True
False (*)
Incorrect. Refer to Section 4 Lesson 2.
11. In the code fragment below, the syntax for the for loop's initialization is correct. True or false?
public class ForLoop {
public static void main (String args[])
{
for ((int 1=10) (i<20) (i++))<
{System.out.Println ("i: "+i); }
}
}
True
False (*)
12. In Eclipse, when you run a Java Application, where may the results display?
Editor Window
Console View (*)
Debug View
Task List
None of the above
13. A combination of views and editors are referred to as _______________.
A workspace
A physical location
A perspective (*)
All of the above
14. You can return to the Eclipse Welcome Page by choosing Welcome from what menu?
File
Edit
Help (*)
Close
15. A workspace is:
The physical location onto which you will store and save your files.
The location where all projects are developed and modified.
The location where you can have one or more stored perspectives.
All of the above. (*)
16. What should replace the comment "//your answer here" in the code below if the code is meant to take no action when i % 2 is 0 (in other words when i is even)?
for(int i = 0; i < 10; i++){
if(i%2 == 0)
//your answer here
else
k+=3;
}
continue; (*)
break;
return;
k+=1;
17. One advantage to using a WHILE loop over a FOR loop is that a WHILE loop always has a counter. True or false?
True
False (*)
18. Consider that a Scanner has been initialized such that:
Scanner in = new Scanner(System.in);
Which of the following lines of code reads in the user's input and sets it equal to a new String called input?
String input = in.next(); (*)
String input = in.close();
String input = new String in.next();
String input = in.nextInt();
19. Switch statements work on all input types including, but not limited to, int, char, and String. True or false?
True
False (*)
20. What is wrong with the following class declaration?
class Account{ ;
privateint number;
privateString name;;
Account;;
}
Classes cannot include strings.
Classes cannot include mixed data types.
The constructor method has no definition. (*)
There is nothing wrong.
21. A class always has a constructor. True or false?
True (*)
False
22. Which of the following creates an instance of the class below?
ThisClass t=new ThisClass();
ThisClass t;
ThisClass t=new ThisClass(3,4);
ThisClass t=new ThisClass(5); (*)
Incorrect. Refer to Section 5 Lesson 2.
23. Which of the following creates a class named Student with one constructor, and 2 instance variables name and gpa?
public class Student { private String name; private float gpa; }
public class Student private String name; private float gpa; Student();
public class Student { private String name; private float gpa; Student(){ name="Jane Doe"; gpa=3.0;} } (*)
public class Student { private String name; Student{ name="Jane Doe"; float gpa=3.0; }
Incorrect. Refer to Section 5 Lesson 2.
24. Which of the following creates an object from the Animal class listed below:
Animal cat=new Animal();
Animal cat=Animal(50,30);
Animal cat=new Animal(50,30); (*)
Animal cat=new Animal(50);
25. What is true about the code below:
Car car1=new Car();
Car car2=new Car();
car2=car1;
The references car1 and car2 are pointing to two Car Objects in memory.
The reference car2 points to an exact copy of the Car Object that car1 references.
There are no more Car objects in memory.
There is a Car object that car1 referenced that is now slated for removal by the garbage collector. (*)
There is a Car object that car2 referenced that is now slated for removal by the garbage collector.
Incorrect. Refer to Section 5 Lesson 2.
26. A constructor must have the same name as the class it is declared within. True or false?
True (*)
False
Section 6
27. What is the output of the following segment of code?
int array[][] = {{1,2,3},{3,2,1}};
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
System.out.print(2*array[1][1]);
444444 (*)
123321
246642
222222
This code doesn't compile.
28. Which of the following statements adds 5 to every element of the one dimensional array prices and then prints it to the screen?
for(int i=0;i<prices.length;i++)
System.out.println(prices[i]+5);
System.out.println(prices[i]+5);
for(int i=1;i<prices.length;i++)
System.out.println(prices[i]+5);
for(int i=0;i<prices.length;i++)
System.out.println(prices[1]+5); (*)
Incorrect. Refer to Section 6 Lesson 1.
29. Which of the following declares and initializes a two dimensional array that can hold 6 Object reference types?
String[] array=new String[6];
Object array=new Object[6];
Object[][] array=new Object[2][3]; (*)
String[][] array=String[6];
30. Which of the following declares and initializes a one dimensional array that can hold 5 Object reference types?
String[] array=new String[5];
Object array=new Object[5]; (*)
Object[] array=new Object[4];
String[] array=String[4];
Incorrect. Refer to Section 6 Lesson 1.
31. Which of the following creates a String reference named s and instantiates it?
String s=""; (*)
s="s";
String s;
String s=new String("s"); (*)
32. What will the following code segment output?
String s="\\\n\"\n\\\n\"";
System.out.println(s);
\" \"
""\
""
\
""
\
"
\
" (*)
"
\
"
\
"
"
Incorrect. Refer to Section 6 Lesson 2.
33. The == operator can be used to compare two String objects. The result is always true if the two strings are have the exact same characters in each position of the String. True or false?
True
False (*)
34. Given the code
String s1 = "abcdef";
String s2 = "abcdef";
String s3 = new String(s1);
Which of the following would equate to false?
s1 == s2
s1 = s2
s3 == s1 (*)
s1.equals(s2)
s3.equals(s1)
35. 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?
True
False (*)
36. Which of the following correctly matches the symbol with its function?
== (two equal signs) compares values of primitive types such as int or char. (*)
== (two equal signs) compares the values of non-primitive objects.
== (two equal signs) compares the memory location of non-primitive objects. (*)
= (single equals sign) compares the value of primitive types such as int or char.
.equals() compares the value of non-primitive objects. (*)
37. If an exception is thrown by a method, where can the catch for the exception be?
There does not need to be a catch in this situation.
The catch must be in the method that threw the exception.
The catch can be in the method that threw the exception or in any other method that called the method that threw the exception. (*)
The catch must be immediately after the throw.
38. What does it mean to catch an exception?
It means you have fixed the error.
It means to throw it.
It means to handle it. (*)
It means there was never an exception in your code.
Section 7
39. Forward thinking helps when creating linear recursive methods. True or false?
True
False (*)
Incorrect. Refer to Section 7 Lesson 2.
40. Which case handles the last recursive call?
The base case (*)
The primary case
The secondary case
The convergence case
The recursive case
Incorrect. Refer to Section 7 Lesson 2.
41. Static methods can't change any class variable values at run-time. True or false?
True False (*)
Incorrect. Refer to Section 7 Lesson 2.
42. It is possible to return an object in a method. True or false?
True (*) False
43. Which segment of code represents a correct way to define a variable argument method?
String easyArray(String... elems) {//code} (*)
String easyArray(...String elems) {//code}
String... easyArray(String elems) {//code}
Integer easyArray... (int elems) {//code}
44. Choose the correct implementation of a public access modifier for the method divide.
divide(int a, int b, public) {return a/b;}
public divide(int a, int b) {return a/b;} (*)
divide(int a, int b) {public return a/b;}
divide(public int a, public int b) {return a/b;}
45. What is true about the Object class?
It is the highest superclass. (*)
It extends other classes.
Its methods can be overridden in subclasses. (*)
Its methods can be overloaded in subclasses. (*)
46. Would this code be correct if a Dog is a HousePet? Why or Why not?
HousePet Scooby = new Dog();
Yes, because it is an abstract class.
Yes, because polymorphism allows this since Dog is a subclass of HousePet. (*)
No, because ref must be declared either a HousePet or a Dog, but not both.
Maybe. There is no way to tell without seeing the methods for Dog and the methods for HousePet.
47. If it is possible to inherit from an abstract class, what must you do to prevent a compiler error from occurring?
It is not possible to inherit from an abstract class.
Create all new methods and variables different from the parent class.
Override all abstract methods from the parent class. (*)
Declare the child class as abstract. (*)
48. Which of the following correctly describes an Is-A relationship?
A helpful term used to conceptualize the relationships among nodes or leaves in an inheritance hierarchy. (*)
A programming philosophy that promotes simpler, more efficient coding by using exiting code for new applications.
It restricts access to a specified segment of code.
A programming philosophy that promotes protecting data and hiding implementation in order to preserve the integrity of data and methods.
49. An access modifier is a keyword that allows subclasses to access methods, data, and constructors from their parent class. True or false?
True (*)
False
50. Which of the following demonstrates the correct way to create an applet Battlefield?
public class Battlefield extends Applet{...} (*)
public Applet Battlefield{...}
public class Applet extends Battlefield{...}
public class Battlefield(Applet){...}
1. What is encapsulation? Mark for Review
(1) Points
A keyword that allows or restricts access to data and methods.
A programming philosophy that promotes simpler, more efficient coding by using exiting code for new applications.
A structure that categorizes and organizes relationships among ideas, concepts of things with the most general at the top and the most specific at the bottom.
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?
Mark for Review
(1) Points
(*)
3. Which of the following demonstrates the correct way to create an applet Battlefield? Mark for Review
(1) Points
public class Applet extends Battlefield{...}
public class Battlefield extends Applet{...} (*)
public class Battlefield(Applet){...}
public Applet Battlefield{...}
4. What is the Java keyword final used for in a program? Mark for Review
(1) Points
It permits access to the class variables and methods from anywhere.
It restricts a class from being extendable and restricts methods from being overridden. (*)
It permits redefining methods of a parent class inside the child class, with the same name, parameters, and return type.
There is no such keyword in Java.
It terminates the program.
5. Which of the following are true about abstract methods? Mark for Review
(1) Points
(Choose all correct answers)
They may contain implementation.
They must be overloaded.
They must be overridden in a non-abstract subclass. (*)
They must be declared in an abstract class. (*)
They cannot have a method body. (*)
6. Which of the following would be most beneficial for this scenario?
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?
Mark for Review
(1) Points
By overriding the methods of Book.
Using polymorphism. (*)
This is not possible. Joe must find another way to collect the books.
By ignoring the subject type and initializing all the book as objects of type Book.
7. Consider
public class YourClass{
public YourClass(int i)
{/*code*/}
// more code...}
To instantiate YourClass, what would you write? Mark for Review
(1) Points
YourClass y = new YourClass();
YourClass y = new YourClass(3); (*)
YourClass y = YourClass(3);
YourClass y = YourClass();
None of the above.
8. The following code creates an object of type Animal:
Animal a; Mark for Review
(1) Points
True
False (*)
9. The basic unit of encapsulation in Java is the primitive data type. True or false? Mark for Review
(1) Points
True
False (*)
10. You can create static class methods inside any Java class. True or false? Mark for Review
(1) Points
True (*)
False
11. Any instance of the same class can assign a new value to a static variable. True or false? Mark for Review
(1) Points
True (*)
False
12. Which of the following access modifiers doesn't work with a static variable? Mark for Review
(1) Points
private
default
protected
public
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? Mark for Review
(1) Points
int DolphinTalk(dol1, dol2){ if(dol1 > dol2) return dol1; else return dol2;}
int DolphinTalk(int,int){ if(dol1 > dol2) return dol1; else return dol2;}
int DolphinTalk(int dol1,int dol2){ if(dol1 > dol2) return dol1; else return dol2;} (*)
int DolphinTalk, int dol1,int dol2 { if(dol1 > dol2) return dol1; else return dol2;}
All of the above
14. Which of the following could be a reason to return an object? Mark for Review
(1) Points
Because you wish to be able to use that object inside of the method.
It has faster performance than returning a primitive type.
The method makes changes to the object and you wish to continue to use the updated object outside of the method. (*)
None of the above. It is not possible to return an object.
15. Which of the following specifies accessibility to variables, methods, and classes? Mark for Review
(1) Points
Overload constructors
Methods
Parameters
Access modifiers (*)
Car car1=new Car();
Car car2=new Car();
car2=car1;
Mark for Review
(1) Points
(Choose all correct answers)
There are no more Car objects in memory.
There is a Car object that car1 referenced that is now slated for removal by the garbage collector.
There is a Car object that car2 referenced that is now slated for removal by the garbage collector.
The reference car2 points to an exact copy of the Car Object that car1 references. (*)
The references car1 and car2 are pointing to two Car Objects in memory.
[Incorrect] Incorrect. Refer to Section 7 Lesson 1.
2. In Java, an instance field referenced using the this keyword generates a compilation error. True or false? Mark for Review
(1) Points
True
False (*)
[Incorrect] Incorrect. Refer to Section 7 Lesson 1.
3. Java's garbage collection is when all references to an object are gone, the memory used by the object is automatically reclaimed. True or false? Mark for Review
(1) Points
True (*)
False
[Correct] Correct
4. If you inherit a class, you do not inherit the class' constructors. True or false? Mark for Review
(1) Points
True (*)
False
[Correct] Correct
5. 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?
Mark for Review
(1) Points
(*)
None of the above.
[Incorrect] Incorrect. Refer to Section 7 Lesson 4.
6. What is encapsulation? Mark for Review
(1) Points
A keyword that allows or restricts access to data and methods.
A programming philosophy that promotes simpler, more efficient coding by using exiting code for new applications.
A programming philosophy that promotes protecting data and hiding implementation in order to preserve the integrity of data and methods. (*)
A structure that categorizes and organizes relationships among ideas, concepts of things with the most general at the top and the most specific at the bottom.
[Incorrect] Incorrect. Refer to Section 7 Lesson 4.
7. Abstract classes can be instantiated. True or false? Mark for Review
(1) Points
True
False (*)
[Incorrect] Incorrect. Refer to Section 7 Lesson 5.
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? Mark for Review
(1) Points
Abstract classes
Polymorphism
Dynamic Method Dispatch (*)
Inheritance
[Incorrect] Incorrect. Refer to Section 7 Lesson 5.
9. Would this code be correct if a Dog is a HousePet? Why or Why not?
HousePet Scooby = new Dog(); Mark for Review
(1) Points
Yes, because polymorphism allows this since Dog is a subclass of HousePet. (*)
Yes, because it is an abstract class.
No, because ref must be declared either a HousePet or a Dog, but not both.
Maybe. There is no way to tell without seeing the methods for Dog and the methods for HousePet.
[Correct] Correct
10. Static methods can return any object type. True or false? Mark for Review
(1) Points
True (*)
False
[Correct] Correct
11. Static methods can write to instance variables. True or false? Mark for Review
(1) Points
True
False (*)
[Incorrect] Incorrect. Refer to Section 7 Lesson 3.
12. Static methods can read instance variables. True or false? Mark for Review
(1) Points
True
False (*)
[Incorrect] Incorrect. Refer to Section 7 Lesson 3.
13. It is possible to overload a method that is not a constructor. True or False? Mark for Review
(1) Points
True (*)
False
[Correct] Correct
14. 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 (*)
protected
private
default
All of the above
[Correct] Correct
15. Which segment of code correctly defines a method that contains two objects of class Tree as parameters? Mark for Review
(1) Points
void bloom(Tree pine, Tree oak) {//code here } (*)
Tree bloom (pine, oak) {//code here }
void bloom, Tree pine, Tree oak {//code here }
None of the above, objects cannot be passed as parameters.
[Correct] Correct
1. The three logic operators in Java are: Mark for Review
(1) Points
!=,=,==
&,|,=
&&, ||, ! (*)
&&,!=,=
[Incorrect] Incorrect. Refer to Section 5 Lesson 1.
2. Which of the following correctly matches the switch statement keyword to its function? Mark for Review
(1) Points
(Choose all correct answers)
switch: identifies what element will be compared to the element of the case statements to find a possible match (*)
if: records the user's input and sends it to the case statements to find a possible match
case: signals what code is executed if the user input matches the specified element (*)
switch: tells the compiler the value to compare the input against
default: signals what code to execute if the input does not match any of the cases (*)
[Incorrect] Incorrect. Refer to Section 5 Lesson 1.
3. This keyword is used to instruct specific code when the input for a switch statement that does not match any of the cases. Mark for Review
(1) Points
switch
case
break
default (*)
None of the above
[Incorrect] Incorrect. Refer to Section 5 Lesson 1.
4. How would you use the ternary operator to rewrite this if statement?
if (balance < 500)
fee = 10;
else
fee = 0; Mark for Review
(1) Points
fee = ( balance >= 5) ? 0 : 10;
fee = ( balance > 5) ? 10 : 0;
fee= ( balance < 500) ? 10 : 0; (*)
fee = ( balance < 500) ? 0 : 10;
fee = ( balance >= 500) ? 10 : 0;
[Incorrect] Incorrect. Refer to Section 5 Lesson 1.
5. In Java, each case seqment of a switch statement requires the keyword break to avoid "falling through". Mark for Review
(1) Points
True (*)
False
[Correct] Correct
6. 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"); Mark for Review
(1) Points
True
False (*)
[Incorrect] Incorrect. Refer to Section 5 Lesson 1.
7. Which of the two diagrams below illustrate the correct syntax for variables used in an if-else statement?
Mark for Review
(1) Points
Example A (*)
Example B
[Correct] Correct
8. The following prints Yes on the screen. True or false?
Mark for Review
(1) Points
True
False (*)
[Incorrect] Incorrect. Refer to Section 5 Lesson 1.
9. How many times will the following loop be executed?
What is the value of x after the loop has finished?
What is the value of count after the loop has finished?
int count = 17;
int x = 1;
while(count > x){
x*=3;
count-=3;
} Mark for Review
(1) Points
3; 27; 8 (*)
5; 27; 8
4; 8; 27
5; 30; 5
3; 9; 11
[Correct] Correct
10. A counter used in a for loop cannot be initialized within the For loop header. True or false? Mark for Review
(1) Points
True
False (*)
[Incorrect] Incorrect. Refer to Section 5 Lesson 2.
11. 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? Mark for Review
(1) Points
True (*)
False
[Correct] Correct
12. In a for loop the counter is not automatically incremented after each loop iteration. Code must be written to increment the counter. True or false? Mark for Review
(1) Points
True (*)
False
[Correct] Correct
13. For both the if-else construct and the for loop, it is true to say that when the condition statement is met, the construct is exited. True or False? Mark for Review
(1) Points
True
False (*)
[Incorrect] Incorrect. Refer to Section 5 Lesson 2.
14. In the code fragment below, the syntax for the for loop's initialization is correct. True or false?
public class ForLoop {
public static void main (String args[])
{
for ((int 1=10) (i<20) (i++))
{
System.out.println ("i: " + i);
}
}
}
Mark for Review
(1) Points
True
False (*)
[Incorrect] Incorrect. Refer to Section 5 Lesson 2.
15. Why are loops useful? Mark for Review
(1) Points
They save programmers from having to rewrite code.
They allow for repeating code a variable number of times.
They allow for repeating code until a certain argument is met.
All of the above. (*)
[Incorrect] Incorrect. Refer to Section 5 Lesson 2.
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.
• Public
• Protected
• Private (*)
• Default
• All of the above
2. 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?
• Public (*)
• Protected
• Private
• Default
• All of the above
3. Which of the following could be a reason to need to pass an object into a method?
• Easier access to the information contained within the object.
• The ability to make changes to an object inside of the method.
• Comparing two objects.
• All of the above. (*)
4. 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(dol1, dol2){ if(dol1 > dol2) return dol1; else return dol2;}
• int DolphinTalk(int,int){ if(dol1 > dol2) return dol1; else return dol2;}
• int DolphinTalk(int dol1,int dol2){ if(dol1 > dol2) return dol1; else return dol2;} (*)
• int DolphinTalk, int dol1,int dol2 { if(dol1 > dol2) return dol1; else return dol2;}
• All of the above
5. Cameron wishes to write a method that takes in two objects and returns the one with the greatest value. Is this possible?
• Yes, but he will have to use two different methods, one to take in the objects and the other to return an object.
• Yes, methods can take objects in as parameters and can also return objects all within the same method. (*)
• No, it is not possible to return objects.
• No, it is not possible to have objects as parameters or to return objects.
6. You are assigned to write a method that compares two objects of type Career. One requirement of your assignment is to have your method compare the "greatestPossibleSalary" instance data of Career objects. The "greatestPossibleSalary" field is data type int.
What would be the best return type from your compare method?
• Career, because if it returns the highest paying Career object it will be able to use the same method later to compare other aspects of Career objects. (*)
• Integer, because it is the easiest to code with.
• String, because is should return a string of the name of the career that is highest paying because none of the other information of the career matters.
• Array, because it can store the most information.
7. Consider the following: There is a method A that calls method B. Method B is a variable argument method. With this, which of the following are true?
• Method A can invoke method B twice, each time with a different number of arguments. (*)
• A compliler error will result since method B does not know how large an array to create when it is invoked by method A.
• When invoked, method B creates an array to store some or all of the arguments passed to it from method A. (*)
• All of the above.
8. What type(s) would work for a variable argument method?
Integers, Strings, and Booleans (*)
Constructors
Arrays (*)
Objects (*)
All of the above
9. It is possible to have more than one constructor with the same name in a class, but they must have different parameters. True or false? True (*) False
10. Which of the following is a possible way to overload constructors?
(*)
Quiz 2 Sectiunea 7
1. Static variables can't use which of the following specifiers?
• Public
• Protected
• Friendly (*)
• Default
• Private
2. You can assign new values to static variables by prefacing them with the this keyword and a dot or period. True or false? True (*) False
3. Which of the following statements about static methods is true?
• They exist once per class. (*)
• They exist once in each instance.
• They can be overridden by a subclass.
• They can access any instance variable.
• They cannot access static variables declared outside the method.
4. You can create static class methods inside any Java class. True or false? True (*) False
5. You can return an instance of a private class through a static method of a different class. True or false? True False (*)
6. You can create static classes as independent classes. True or false? True False (*)
7. You can use an inner static class to return an instance of its outer class container. True or false? True (*) False
8. A linear recursive method can call how many copies of itself?
• 1 (*)
• 2 or more
• None
9. Which case does a recursive method call last?
• Recursive Case
• Convergence Case
• Basic Case
• Base Case (*)
• None of the above
10. A non-linear recursive method can call how many copies of itself?
• 1
• 2 or more (*)
• None
Quiz 3 Sectiunea 7
1. What is a UML?
• Unidentified Molding Level, the level of access permitted by the default access specifier.
• Unified Modeling Language, a standardized language for modeling systems and structures in programming. (*)
• Universal Model Light, a program that reads the brightness of any given lightbulb.
• None of the above.
2. What does it mean to inherit a class?
• The subclass (or child class) gains access to any non-private methods and variables of the superclass (or parent class). (*)
• The access specifier has been set to private.
• A way of organizing the hierarchy of classes.
• Extending a method from a superclass.
3. Which of the following correctly defines a superclass (or parent class)?
• A class that inherits methods and fields from a more general class.
• The most specific class of a hierarchy system of classes.
• A class that passes down its methods to more specialized classes. (*)
• A keyword that allows or restricts access to data and methods.
4. Which of the following correctly defines a subclass (or child class)?
• A class that inherits methods and fields from a more general class. (*)
• A keyword that allows or restricts access to data and methods.
• A class that passes down its methods to more specialized classes.
• The most general class of a hierarchy system.
5. Methods are generally declared as public so other classes may use them. True or false?
True (*) False
6. Which is the most accurate description of the code reuse philosiphy?
• A programming philosophy that promotes stealing your classmates' code.
• A programming philosophy that promotes having no concern about the security of code.
• A programming philosophy that promotes protecting data and hiding implementation in order to preserve the integrity of data and methods.
• A programming philosophy that promotes simpler, more efficient coding by using existing code for new applications. (*)
7. Which of the following correctly describes the use of the keyword super?
• A keyword that restricts access to only inside the same class.
• A keyword that allows subclasses to access methods, data, and constructors from their parent class. (*)
• A keyword that signals the end of a program.
• A keyword that allows access from anywhere.
8. 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
• super.length(5)
• super.length = 5 (*)
• super(length = 5)
9. What is a hierarchy?
• A programming philosophy that promotes simpler, more efficient coding by using existing code for new applications.
• A programming philosophy that promotes protecting data and hiding implementation in order to preserve the integrity of data and methods.
• A keyword that allows subclasses to access methods, data, and constructors from their parent class.
• A structure that categorizes and organizes relationships among ideas and concepts of things with the most general at the top and the most specific at the bottom. (*)
10. It is possible to extend a class that already exists in Java, such as the Applet class. True or false? True (*) False
Quiz 4 Sectiunea 7
1. Why would a programmer use polymorphism rather than sticking to a standard array?
• Because arrays only work using the same object type and polymorphism provides a way around this. (*)
• Because it is easier to add or remove objects using polymorphism even when all of the objects are of the same type.
• Because arrays are more complex and polymorphism simplifies them by restricting them to only contain the same type objects.
• A programmer wouldn't use polymorphism over a standard array.
2. It is possible to override methods such as equals() and toString() in a subclass of Object to fit the needs of the objects of the subclass. True or false? True (*) False
3. Is there a difference between overriding a method and overloading a method?
• Yes. Overriding is done within a single class and overloading is done through a series of superclasses and their subclasses.
• Yes. Overriding allows for the creation of an array of different object types and overloading restricts an array to only contain the same object types.
• Yes. Overriding is done in the subclass and allows for redefining a method inherited from the superclass and overloading is done within a class and allows for multiple methods with the same name. (*)
• No, they are the same.
4. Which of the following is a goal of the object model?
• Providing modular code that can be reused by other programs or classes. (*)
• Concealing implementation. (*)
• Data abstraction. (*)
• Protecting information and limiting other classes' ability to change or corrupt data. (*)
5. If Sandal extends Shoe, it is possible to declare an object such that
Sandal s = new Shoe(); True False (*)
6. 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?
• Abstract classes
• Polymorphism
• Inheritance
• Dynamic Method Dispatch (*)
7. Which of the following are true about an abstract class?
• It is possible to create objects of this type.
• The Java Virtual Machine does not differentiate abstract classes from concrete classes.
• It is possible to create references of this type. (*)
• It is identified by the Java keyword abstract. (*)
1. What is a loop?
• A keyword used to skip over the remaining code.
• A set of logic that is repeatedly executed until a certain condition is met. (*)
• A segment of code that may only ever be executed once per call of the program.
• None of the above.
2. It is necessary to end all loops at some point in your Java program. True or false?
True (*) False
3. Which of the following are types of loops in Java?
• While (*)
• If/Else
• Do-While (*)
• For (*)
4. Identify which situation could be an example of a WHILE loop.
• Taking coins out of a pile one at a time and adding their value to the total until there are no more coins in the pile to add.
• Attending class while school is not over for the day.
• Petting each animal at the pet store one at a time until all the animals have been petted.
• All of the above. (*)
5. Which of the following correctly initializes a For loop that runs through 5 times?
• for(int i = 0; i == 6; i++)
• for(int i = 1; i < 6; i++) (*)
• for(int i = 0; i < 5; I++)
• for(int i = 1; i < 5; I++)
6. What is the function of the word "break" in Java?
• It exits the current loop or case statement. (*)
• It continues onto the next line of code.
• It stops the program from running.
• It does not exist in Java.
7. The following code fragment properly implements the switch statement. True or false?
default(input)
switch '+':
answer+=num;
break;
case '-':
answer-=num;
break;
!default
System.out.println("Invalid input"); True False (*)
8. What is one significant difference between a WHILE loop and a DO-WHILE loop?
• There is no difference between a DO-WHILE loop and a WHILE loop.
• A DO-WHILE loop does not exist in Java and a WHILE loop does.
• A DO-WHILE loop includes an int that serves as a counter and a WHILE loop does not.
• 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. (*)
9. Which of the following correctly initializes an instance of Scanner, called "in", that reads input from the console screen?
• Scanner in = new Scanner(System.in); (*)
• Scanner in = new Scanner("System.in");
• Scanner in = Scanner(System.in);
• System.in in = new Scanner();
Quiz 2 Sectiunea 5
1. Which of the following creates a class named Diver with one constructor, and 2 instance variables maxDepth and certified?
• (*)
•
•
•
2. The basic unit of encapsulation in Java is the:
class (*) classpath
method package
3. Which of the following creates a Object from the Animal class listed below?
• Animal dog=new Animal();
• Animal dog=new Animal(50,30); (*)
• Animal dog=Animal(50,30);
• Animal dog=new Animal(50);
4. Complete the sentence. A constructor...
• must have the same name as the class it is declared within.
• is used to create objects.
• may be declared public.
• is all of the above. (*)
5. The following code creates an Object of type Animal:
Animal a; True False (*)
6. Which of the following creates an object from the Car class listed below?
• Car c = new Car(3000, "Toyota"); (*)
• Car c=new Car;
• Car c=Car();
• Car c;
• Car c =new Car();
7. What is wrong with the following class declaration?
• Classes cannot include strings.
• Classes cannot include mixed data types.
• There is no constructor method and you have to make a constructor method.
• There is nothing wrong. (*)
8. Which of the following is true?
• In Java, a method declared public generates a compilation error.
• int is the name of a class available in the package java.lang.
• Instance variable names may only contain letters and digits.
• A class always has a constructor (possibly automatically supplied by the java compiler). (*)
• The more comments in a program, the faster the program runs.
9.What value will be returned when the setValue method is called?
• 35
• 36
• 37 (*)
• 38
10. The return value of a method can only be a primitive type and not an object. True or false?
True False (*)
11 The following statement compiles and executes. What can you say for sure?
submarine.dive(depth);
• Depth must be an int.
• Dive must be a method. (*)
• Dive must be the name of an instance field.
• Submarine must be the name of a class.
• Submarine must be a method.
12.Which of the following calls the method moveUp in the class below:
• Puzzle p=new Puzzle(); p.moveUp(3,4);
• PuzzlePiece p=new PuzzlePiece(); p.moveUp(3,4); (*)
• PuzzlePiece p=new PuzzlePiece(); p.moveUp(3,4,5);
• Puzzle p=new Puzzle(); p.moveUp(3,4,5);
13. A class can have multiple constructors. True or false? True (*) False
14. The constructor of a class has the same name as the class. True or false? True (*) False
15. Which of the following adds a constructor to the class below?
(*)
16. What operator do you use to call an object's constructor method and create a new object?
• new (*)
• class
• instanceOf
17. Consider: public class MyClass{ public MyClass(){/*code*/} // more code...}
To instantiate MyClass, what would you write?
MyClass m = new MyClass(); (*)
MyClass m = new MyClass;
MyClass m = MyClass;
MyClass m = MyClass();
18. Which of the following may be part of a class definition?
• instance variables
• instance methods
• constructors
• comments
• all of the above (*)
19. What is garbage collection in the context of Java?
• The operating system periodically deletes all of the Java files available on the system.
• Any package imported in a program and not used is automatically deleted.
• When all references to an object are gone, the memory used by the object is automatically reclaimed. (*)
• The JVM checks the output of any Java program and deletes anything that does not make sense.
20. Which of the following keywords are used to access the instance variables of an object from within the class code for that object?
• public
• private
• protected
• this (*)
21. Which constructor code populates the instance variables of the class correctly?
(*)
1. Which of the following statements is a valid array declaration?
• int number();
• float average[]; (*)
• double[] marks; (*)
• counter int[];
2.The following array declaration is valid: int[] y = new int[5]; True (*) False
3. Which of the following declares a one dimensional array named "score" of type int that can hold 9 values?
• int score;
• int[] score;
• int[] score=new int[9]; (*)
• int score=new int[9];
4. Which of the following declares and initializes a two dimensional array?
• int[][] array={{1,1,1},{1,1,1},{1,1,1}}; (*)
• int[] array={{1,1,1},{1,1,1},{1,1,1}};
• int[][] array={1,1,1},{1,1,1},{1,1,1};
• int[][] array={1,1,1,1,1,1,1,1,1};
5. Which of the following declares and initializes a one dimensional array named words of size 10 so that all entries can be Strings?
• String words=new String[10];
• char words=new char[10];
• char[] words=new char[10];
• String[] words=new String[10]; (*)
6. What is the output of the following segment of code?
• 222220
• 0 (*)
• 220
• 2
• This code does not compile.
7. What is the output of the following segment of code?
• 1286864
• 643432
• 262423242322
• 666666 (*)
• This code does not compile.
8. 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?
• String[][] values={"apples","oranges","pears"};
• String[][] values=new String[3][2];
• String[][] values=new String[2][3]; (*)
• String[][] values;
9. Which of the following declares and initializes a two dimensional array where each element is a reference type?
• String words=new String[10];
• char[][] words;
• char[][] words=new char[10][4];
• String[][] words=new String[10][3]; (*)
10. 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]);} (*)
• System.out.println(prices.length);
• for(int i=1; i <= prices.length; i++){System.out.println(prices[i]);}
• for(int i=0; i <= prices.length; i++){System.out.println(prices[i]);}
11. What is the output of the following segment of code?
• 753
• 6
• 7766554433221
• 7531 (*)
? This code does not compile.
12. The following creates a reference in memory named y that can refer to five different integers via an index. True or false? int[] y = new int[5]; True (*) False
13. The following creates a reference in memory named z that can refer to seven different doubles via an index. True or false? double z[] = new double[7]; True (*) False
14. What is the output of the following segment of code if the command line arguments are "apples oranges pears"?
• apples
• pears (*)
• oranges
• args
• This code doesn't compile.
15. What is the output of the following segment of code if the command line arguments are "apples oranges pears"?
• 0
• 1
• 2
• 3 (*)
• This code does not compile.
16. What will be the content of array variable table after executing the following code?
• 0 0 0
0 0 0
0 0 0
• 1 0 0
0 1 0
0 0 1 (*)
• 1 0 0
1 1 0
1 1 1
• 0 0 1
0 1 0
1 0 0
17.What is the output of the following segment of code?
• 1286864 (*)
• 643432
• 262423242322
• 666666
• This code does not compile.
18. After execution of the following statement, which of the following are true?
int number[] = new int[5];
• number[0] is undefined
• number[4] is null
• number[2] is 0 (*)
• number.length() is 6
19. The following array declaration is valid. True or false? int x[] = int[10]; True False (*)
Quiz 2 Sectiunea 6
1. Consider the following code snippet. What is printed?
String river = new String("Hudson"); System.out.println(river.length());
6 (*) 7 8 Hudson river
2. Which of the following creates a String reference named str and instantiates it?
• String str;
• str="str";
• String s="str";
• String str=new String("str"); (*)
3. 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 (*) False
4. Which of the following statements declares a String object called name?
• String name; (*)
• String name
• int name;
• double name;
5. Suppose that s1 and s2 are two strings. Which of the statements or expressions are valid?
• String s3 = s1 + s2; (*)
• String s3 = s1 - s2;
• s1 <= s2
• s1.compareTo(s2); (*)
• int m = s1.length(); (*)
6. Consider the following code snippet. What is printed?
• PoliiPolii (*)
• Polii
• auaacauaac
• auaac
• ArrayIndexOutofBoundsException is thrown
7. What will the following code segment output?
• "\\\\\"
• \"\\\\\"
• "\\" (*)
• "\\\"
8. What will the following code segment output?
• ""\\"
• ""\"
• ""\
" (*)
• """\
""
• ""\
""
9. The following program prints "Equal". True or false?
• True (*)
• False
10. Which of the following creates a String named string?
• char string;
• String s;
• String string; (*)
• String String;
• String char;
11. Given the code, which of the following would equate to true?
String s1 = "yes";
String s2 = "yes";
String s3 = new String(s1);
• s1 == s2 (*)
• s1 = s2
• s3 == s1
• s1.equals(s2) (*)
• s3.equals(s1) (*)
12. The String methods equals and compareTo perform the exact same function. True or false?
True False (*)
13. The == operator can be used to compare two String objects. The result is always true if the two strings are identical. True or false? True False (*)
14. The following program prints "Equal". True or false?
• True
• False (*)
15. Given the code below, which of the following calls are valid? String s = new String("abc");
• s.trim() (*)
• s.replace('a', 'A') (*)
• s.substring(2) (*)
• s.toUpperCase() (*)
• s.setCharAt(1,'A')
16. Consider the following code snippet. What is printed?
String ocean = new String("Atlantic Ocean"); System.out.println(ocean.indexOf('a'));
0 2 3 (*) 11 12
17. Consider the following code snippet. What is printed?
• 55555
• 87668 (*)
• AtlanticPacificIndianArcticSouthern
• The code does not compile.
• An ArrayIndexOutofBoundsException is thrown.
18. Consider the following code snippet. What is printed?
• 55555
• 87658
• AtlanticPacificIndianArcticSouthern
• The code does not compile.
• An ArrayIndexOutofBoundsException is thrown. (*)
19.How would you use the ternary operator to rewrite this if statement?
if (skillLevel > 5)
numberOfEnemies = 10;
else
numberOfEnemies = 5;
• numberOfEnemies = ( skillLevel > 5) ? 5 : 10;
• numberOfEnemies = ( skillLevel < 5) ? 10 : 5;
• numberOfEnemies = ( skillLevel >= 5) ? 5 : 10;
• numberOfEnemies = ( skillLevel >= 5) ? 10 : 5;
• numberOfEnemies = ( skillLevel > 5) ? 10 : 5; (*)
20. How would you use the ternary operator to rewrite this if statement?
if (gender == "male")
System.out.print("Mr.");
else
System.out.print("Ms.");
• System.out.print( (gender == "male") ? "Mr." : "Ms." ); (*)
• System.out.print( (gender == "male") ? "Ms." : "Mr." );
• (gender == "male") ? "Mr." : "Ms." ;
• (gender == "male") ? "Ms." : "Mr." ;
Quiz 3 Sectiunea 6
1. Which of the following would give you an array index out of bounds exception?
• Misspelling a variable name somewhere in your code.
• Refering to an element of an array that is at an index less than the length of the array minus one.
• Using a single equal symbol to compare the value of two integers.
• Refering to an element of an array that is at an index greater than the length of that array minus one. (*)
• Unintentionally placing a semicolon directly after initializing a for loop.
2. What exception message indicates that a variable may have been mispelled somewhere in the program?
• variableName cannot be resolved to a variable (*)
• method methodName(int) is undefined for the type className
• Syntax error, insert ";" to complete statement
• All of the Above
3. Which of the following defines an Exception?
• A very severe non-fixable problem with interpreting and running your code.
• Code that has no errors and therefore runs smothly.
• A problem that can be corrected or handled by your code. (*)
• An interpreter reading your code.
4. What do exceptions indicate in Java?
• The code has considered and dealt with all possible cases.
• A mistake was made in your code. (*)
• There are no errors in your code.
• Exceptions do not indicate anything, their only function is to be thrown.
• The code was not written to handle all possible conditions. (*)
5. Which line of code shows the correct way to throw an exception?
• new throw Exception("Array index is out of bounds");
• throw new Exception("Array index is out of bounds"); (*)
• throw Exception("Array index is out of bounds");
• throws new Exception("Array index is out of bounds");
6. What does the interpreter look for when an exception is thrown?
• It does not look for anything. It just keeps reading through your code.
• It does not look for anything. It stops interpreting your code.
• The end of the code.
• A catch statement in the code. (*)
7. Which of the following would be a correct way to handle an index out of bounds exception?
• Throw the exception and catch it. In the catch, set the index to the index of the array closest to the one that was out of bounds. (*)
• Do nothing, it will fix itself.
• Throw the exception that prints out an error message. There is no need to have the catch handle the exception if it has already been thrown.
• Rewrite your code to avoid the exception by not permititng the use of an index that is not inside the array. (*)
8. A computer company has one million dollars to give as a bonus to the employees, and they wish to distribute it evenly amongst them.
The company writes a program to calculate the amount each employee receives, given the number of employees.
Unfortunately, the employees all went on strike before they heard about the bonus. This means that the company has zero employees.
What will happen to the program if the company enters 0 into the employment number?
• An unfixable error will occur.
• The program will calculate that each employee will receive zero dollars because there are zero employees.
• An exception will occur because it is not possible to divide by zero. (*)
• The programmers will have proven their worth in the company because without them the company wrote faulty code.
1. The ______________ is the location into which you will store and save your files. Mark for Review
(1) Points
Perspective
Workspace (*)
Editor
None of the above
2. Multiple windows are used when more than one file is open in the edit area. True or False? Mark for Review
(1) Points
True
False (*)
3. A workspace can have one or more stored projects. True or false? Mark for Review
(1) Points
True (*)
False
4. Tabs are used when more than one file is open in the edit area. True or False? Mark for Review
(1) Points
True (*)
False
5. A _______________ is used to organize Java related files. Mark for Review
(1) Points
Collection
Project
Package (*)
Workspace
Section 4
(Answer all questions in this section)
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. The following defines a package keyword: Mark for Review
(1) Points
Provides the compiler information that identifies outside classes used within the current class.
Precedes the name of the class.
Defines where this class lives relative to other classes, and provides a level of access control. (*)
8. The following code is an example of a correct initialization statement:
char c="c"; Mark for Review
(1) Points
True
False (*)
9. Suppose that str1 and str2 are two strings. Which of the statements or expressions are valid? Mark for Review
(1) Points
Str1 -= str2;
str1 >= str2
str1 += str2; (*)
String str3 = str1 - str2;
10. Which of the following creates a String named string? Mark for Review
(1) Points
String String;
String s;
String char;
char string;
String string; (*)
Section 4
(Answer all questions in this section)
11. Which of the following creates a String reference named s and instantiates it? Mark for Review
(1) Points
(Choose all correct answers)
s="s";
String s;
String s=new String("s"); (*)
String s=""; (*)
12. Which of the following creates a String reference named str and instantiates it? Mark for Review
(1) Points
str="str";
String str=new String("str"); (*)
String str;
String s="str";
13. Which line of code does not assign 3.5 to the variable x? Mark for Review
(1) Points
x=7.0/2.0;
x=3.5;
double x=3.5
3.5=x; (*)
14. Which line of Java code assigns the value of 5 raised to the power of 8 to a? Mark for Review
(1) Points
int a=Math.pow(8,5);
double a=15^8;
double a=Math.pow(5,8); (*)
int a=Math.pow(5,8);
double a=pow(8,5);
15. What two values can a boolean variable have? Mark for Review
(1) Points
Integers and floating point types
Arithmetic and logic operators
True and false (*)
Relational and logic operators
Numbers and characters
Section 5
(Answer all questions in this section)
16. The following prints Yes on the screen. True or false?
Mark for Review
(1) Points
True
False (*)
17. How would you use the ternary operator to rewrite this if statement?
if (gender == "female") System.out.print("Ms.");
else
System.out.print("Mr."); Mark for Review
(1) Points
System.out.print( (gender == "female") ? "Mr." : "Ms." );
(gender == "female") ? "Ms." : "Mr." ;
System.out.print( (gender == "female") ? "Ms." : "Mr." ); (*)
(gender == "female") ? "Mr." : "Ms." ;
18. Which of the following expressions will evaluate to true when x and y are boolean variables with opposite values?
I. (x || y) && !(x && y)
II. (x && !y) || (!x && y)
III. (x || y) && (!x ||!y) Mark for Review
(1) Points
I only
II only
I and III
II and III
I, II, and III (*)
19. Why are loops useful? Mark for Review
(1) Points
They save programmers from having to rewrite code.
They allow for repeating code a variable number of times.
They allow for repeating code until a certain argument is met.
All of the above. (*)
20. A counter used in a for loop cannot be initialized within the For loop header. True or false? Mark for Review
(1) Points
True
False (*)
Section 5
(Answer all questions in this section)
21. Which of the following best describes a while loop? Mark for Review
(1) Points
A loop that contains a counter in parenthesis with the conditional statement.
A loop that executes the code at least one time even if the conditional statement is false.
A loop that is executed repeatedly until the conditional statement is false. (*)
A loop that contains a segment of code that is executed before the conditional statement is tested.
Section 6
(Answer all questions in this section)
22. 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? Mark for Review
(1) Points
True
False (*)
23. What does it mean to catch an exception? Mark for Review
(1) Points
It means you have fixed the error.
It means there was never an exception in your code.
It means to handle it. (*)
It means to throw it.
24. What do exceptions indicate in Java? Mark for Review
(1) Points
(Choose all correct answers)
The code has considered and dealt with all possible cases.
The code was not written to handle all possible conditions. (*)
A mistake was made in your code. (*)
Exceptions do not indicate anything, their only function is to be thrown.
There are no errors in your code.
25. Which of the following would be a correct way to handle an index out of bounds exception? Mark for Review
(1) Points
(Choose all correct answers)
Rewrite your code to avoid the exception by not permititng the use of an index that is not inside the array. (*)
Throw the exception that prints out an error message. There is no need to have the catch handle the exception if it has already been thrown.
Throw the exception and catch it. In the catch, set the index to the index of the array closest to the one that was out of bounds. (*)
Do nothing, it will fix itself.
Section 6
(Answer all questions in this section)
26. The following creates a reference in memory named k that can refer to six different integers via an index. True or false?
int k[]= int[6]; Mark for Review
(1) Points
True
False (*)
27. What is the output of the following segment of code?
int num[]={9,8,7,6,5,4,3,2,1};
for(int i=0;i<9 i="i+3)" o:p="">
System.out.print(num[i]); Mark for Review
(1) Points
97531
987654321
9630
963 (*)
This code doesn't compile.
28. What is the output of the following segment of code?
int array[][] = {{1,2,3},{3,2,1}};
for(int i=0;i<2 i="" o:p="">
for(int j=0;j<3 j="" o:p="">
System.out.print(2*array[1][1]); Mark for Review
(1) Points
246642
444444 (*)
222222
This code doesn't compile.
123321
29. What is the output of the following segment of code?
Mark for Review
(1) Points
1286864
643432
666666 (*)
262423242322
This code does not compile.
Section 7
(Answer all questions in this section)
30. Which of the following could be a reason to return an object? Mark for Review
(1) Points
Because you wish to be able to use that object inside of the method.
It has faster performance than returning a primitive type.
The method makes changes to the object and you wish to continue to use the updated object outside of the method. (*)
None of the above. It is not possible to return an object.
Section 7
(Answer all questions in this section)
31. Which of the following is the definition for a variable argument method? Mark for Review
(1) Points
A way to create a new class.
A type of argument that enables calling the same method with a different number of arguments. (*)
Having more than one constructor with the same name but different arguments.
Specifies accessibility to code.
32. You are assigned to write a method that compares two objects of type Career. One requirement of your assignment is to have your method compare the "greatestPossibleSalary" instance data of Career objects. The "greatestPossibleSalary" field is data type int.
What would be the best return type from your compare method? Mark for Review
(1) Points
Array, because it can store the most information.
Integer, because it is the easiest to code with.
Career, because if it returns the highest paying Career object it will be able to use the same method later to compare other aspects of Career objects. (*)
String, because is should return a string of the name of the career that is highest paying because none of the other information of the career matters.
33. What type(s) would work for a variable argument method? Mark for Review
(1) Points
(Choose all correct answers)
Integers, Strings, and Booleans (*)
Constructors
Arrays (*)
Objects (*)
All of the above
34. 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. Mark for Review
(1) Points
public
protected
private (*)
default
All of the above
35. Methods are generally declared as public so other classes may use them. True or false? Mark for Review
(1) Points
True (*)
False
Section 7
(Answer all questions in this section)
36. Which of the following correctly describes an "is-a" relationship? Mark for Review
(1) Points
A helpful term used to conceptualize the relationships among nodes or leaves in an inheritance hierarchy. (*)
A programming philosophy that promotes simpler, more efficient coding by using exiting code for new applications.
A programming philosophy that promotes protecting data and hiding implementation in order to preserve the integrity of data and methods.
It restricts access to a specified segment of code.
37. Which of the following demonstrates the correct way to create an applet Battlefield? Mark for Review
(1) Points
public class Applet extends Battlefield{...}
public class Battlefield(Applet){...}
public class Battlefield extends Applet{...} (*)
public Applet Battlefield{...}
38. What is encapsulation? Mark for Review
(1) Points
A structure that categorizes and organizes relationships among ideas, concepts of things with the most general at the top and the most specific at the bottom.
A keyword that allows or restricts access to data and methods.
A programming philosophy that promotes simpler, more efficient coding by using exiting code for new applications.
A programming philosophy that promotes protecting data and hiding implementation in order to preserve the integrity of data and methods. (*)
39. The following code creates an object of type Horse:
Whale a=new Whale(); Mark for Review
(1) Points
True
False (*)
40. Which of the following keywords are used to access the instance variables of an object from within the class code for that object? Mark for Review
(1) Points
private
protected
public
this (*)
Section 7
(Answer all questions in this section)
41. Which constructor code populates the instance variables of the class correctly? Mark for Review
(1) Points
(*)
42. Which of the following calls the method calculate correctly?
Mark for Review
(1) Points
ThisClass t=new ThisClass(); int x=t.calculate(3);
ThisClass t=new ThisClass(); int x=t.calculate(3,4); (*)
int x=calculate(3,4);
ThisClass t=new ThisClass(); int x=t.calculate();
43. Which of the following creates a method that returns a boolean value? Mark for Review
(1) Points
(*)
None of the above.
44. A constructor must have the same name as the class where it is declared. True or false? Mark for Review
(1) Points
True (*)
False
45. Which of the following is a goal of the object model? Mark for Review
(1) Points
(Choose all correct answers)
Concealing implementation. (*)
Providing modular code that can be reused by other programs or classes. (*)
Protecting information and limiting other classes' ability to change or corrupt data. (*)
Data abstraction. (*)
Section 7
(Answer all questions in this section)
46. Which of the following can be declared final? Mark for Review
(1) Points
Classes
Methods
Local variables
Method parameters
All of the above (*)
47. If we override the toString() method with the code below, what would be the result of printing?
Mark for Review
(1) Points
It would print the array backwards. The console screen would display: 42 11 64 215 18 0
It would print the string returned from the method. The console screen would display: {0, 18, 215, 64, 11, 42}
It would print the array one element at a time. The console screen would display: 0 18 215 64 11 42
It would print the string returned from the method. The console screen would display: [0,18,215,64,11,42,] (*)
48. You can use an inner static class to return an instance of its outer class container. True or false? Mark for Review
(1) Points
True (*)
False
49. You can return an instance of a private class through a static method of a different class. True or false? Mark for Review
(1) Points
True
False (*)
50. You can assign new values to static variables by prefacing them with the this keyword and a dot or period. True or false? Mark for Review
(1) Points
True (*)
False
1. The following segment of code initializes a 2 dimensional array of primitive data types. True or false?
double[][] a=new double[4][5]; Mark for Review
(1) Points
True (*)
False
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? Mark for Review
(1) Points
String[][] values={"apples","oranges","pears"};
String[][] values=new String[2][3]; (*)
String[][] values;
String[][] values=new String[3][2];
3. double array[] = new double[8];
After execution of this statement, which of the following are true? Mark for Review
(1) Points
array.length is 8 (*)
array[0] is undefined
array[2] is 8
array[4] is null
4. What is the output of the following segment of code if the command line arguments are "apples oranges pears"?
Mark for Review
(1) Points
oranges
This code does not compile.
args
pears (*)
apples
5. What is the output of the following segment of code if the command line arguments are "a b c d e f g"?
Mark for Review
(1) Points
c
e (*)
d
f
This code doesn't compile.
6. Which of the following statements is not a valid array declaration? Mark for Review
(1) Points
float []averages;
double marks[5];
counter int[]; (*)
int number[];
7. What is the output of the following segment of code?
Mark for Review
(1) Points
11 (*)
111
1111
321111
This code doesn't compile.
8. What is the output of the following segment of code?
Mark for Review
(1) Points
555555
777777 (*)
This code doesn't compile.
456789
987654
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]; Mark for Review
(1) Points
True (*)
False
10. What is the output of the following segment of code?
Mark for Review
(1) Points
666666 (*)
643432
262423242322
1286864
This code does not compile.
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? Mark for Review
(1) Points
(Choose all correct answers)
An error would occur. Errors cannot be handled by code.
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. (*)
An exception would occur but could not be handled inside your code. The user would have to restart the program and enter proper coordinates.
12. Which of the following defines an Exception? Mark for Review
(1) Points
A very severe non-fixable problem with interpreting and running your code.
An interpreter reading your code.
A problem that can be corrected or handled by your code. (*)
Code that has no errors and therefore runs smothly.
13. What do exceptions indicate in Java? Mark for Review
(1) Points
(Choose all correct answers)
The code was not written to handle all possible conditions. (*)
There are no errors in your code.
A mistake was made in your code. (*)
The code has considered and dealt with all possible cases.
Exceptions do not indicate anything, their only function is to be thrown.
14. It is possible to throw and catch a second exception inside a catch block of code. True or false? Mark for Review
(1) Points
True (*)
False
15. What exception message indicates that a variable may have been mispelled somewhere in the program? Mark for Review
(1) Points
variableName cannot be resolved to a variable (*)
method methodName(int) is undefined for the type className
Syntax error, insert ";" to complete statement
All of the above
1. What does the interpreter look for when an exception is thrown? Mark for Review
(1) Points
It does not look for anything. It stops interpreting your code.
The end of the code.
It does not look for anything. It just keeps reading through your code.
A catch statement in the code. (*)
2. Which of the following could be a reason to throw an exception? Mark for Review
(1) Points
To make the user interface harder to navigate.
To eliminate exceptions from disrupting your program. (*)
You have encountered a Stack Overflow Error.
You have a fatal error in your program.
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? Mark for Review
(1) Points
True
False (*)
4. Which line of code shows the correct way to throw an exception? Mark for Review
(1) Points
throw Exception("Array index is out of bounds");
throws new Exception("Array index is out of bounds");
new throw Exception("Array index is out of bounds");
throw new Exception("Array index is out of bounds"); (*)
5. If an exception has already been thrown, what will the interpreter read next in the program? Mark for Review
(1) Points
Where the program catches the exception. (*)
The next line of the program even if it is not the catch block of code.
The end of the program.
The user input.
Section 6 Quiz
(Answer all questions in this section)
6. What is the output of the following segment of code?
Mark for Review
(1) Points
987654
555555
777777 (*)
456789
This code doesn't compile.
7. Which of the following declares and initializes a two dimensional array where each element is a reference type? Mark for Review
(1) Points
String words=new String[10];
char[][] words;
String[][] words=new String[10][3]; (*)
char[][] words=new char[10][4];
8. Which of the following declares and initializes a two dimensional array? Mark for Review
(1) Points
int[][] array={1,1,1,1,1,1,1,1,1};
int[][] array={1,1,1},{1,1,1},{1,1,1};
int[][] array={{1,1,1},{1,1,1},{1,1,1}}; (*)
int[] array={{1,1,1},{1,1,1},{1,1,1}};
9. The following array declaration is valid:
int[] y = new int[5]; Mark for Review
(1) Points
True (*)
False
10. The following segment of code initializes a 2 dimensional array of primitive data types. True or false?
double[][] a=new double[4][5]; Mark for Review
(1) Points
True (*)
False
11. Which of the following statements is not a valid array declaration? Mark for Review
(1) Points
double marks[5];
int number[];
counter int[]; (*)
float []averages;
12. double array[] = new double[8];
After execution of this statement, which of the following are true? Mark for Review
(1) Points
array[0] is undefined
array.length is 8 (*)
array[4] is null
array[2] is 8
13. What is the output of the following segment of code?
Mark for Review
(1) Points
321123
312213
642246 (*)
642
This code doesn't compile.
14. The following segment of code prints all five of the command line arguments entered into this program. True or false?
Mark for Review
(1) Points
True
False (*)
15. Which of the following statements print every element of the one dimensional array prices to the screen? Mark for Review
(1) Points
System.out.println(prices.length);
for(int i=0; i <= prices.length; i++){System.out.println(prices[i]);}
for(int i=1; i <= prices.length; i++){System.out.println(prices[i]);}
for(int i=0; i < prices.length; i++){System.out.println(prices[i]);} (*)
1. What are Java's simple types?
boolean, byte, char, double, float, int, long, and short (*)
boolean, byte, string, thread, int, double, long and short
object, byte, string, char, float, int, long and short
boolean, thread, stringbuffer, char, int, float, long and short
boolean, thread, char, double, float, int, long and short
2. Which of the following are relational operators in Java? (Choose all correct answers)
< (*)
<= (*)
=
!= (*)
All of the above.
3. What is the output of the following lines of code?
int j=6,k=4,m=12,result;
result=j/m*k;
System.out.println(result);
2
0 (*)
48
24
4. A local variable has precedence over a global variable in a Java method. True or false?
True (*) False
5. What does the following program output?
total cost: + 40
total cost: 48
total cost: 40 (*)
"total cost: " 48
"total cost: " 40
6. What is the result when the following code segment is compiled and executed?
int x = 22, y = 10;
double p = Math.sqrt( ( x + y ) /2);
System.out.println(p);
Syntax error "sqrt(double) in java.lang.Math cannot be applied to int"
4.0 is displayed (*)
2.2 is displayed
5.656854249492381 is displayed
ClassCastException
7. Determine whether this boolean expression evaluates to true or false:
!(3<4&&6>6||6<=6&&7-2==6)
True (*) False
8. In an if-else construct the condition to be evaluated must end with a semi-colon. True or false?
True False (*)
9. Which of the two diagrams below illustrate the general form of a Java program?
Example A
Example B (*)
10. In a For loop the counter is not automatically incremented after each loop iteration. Code must be written to increment the counter. True or false?
True (*) False
11. When the For loop condition statement is met the construct is exited. True or false?
True False (*)
12. You can return to the Eclipse Welcome Page by choosing Welcome from what menu?
File
Edit
Help (*)
Close
13. In Eclipse, when you run a Java Application, where may the results display?
Editor Window
Console View (*)
Debug View
Task List
None of the above
14. A combination of views and editors are referred to as _______________.
A workspace
A physical location
A perspective (*)
All of the above
15. What are the Eclipse Editor Area and Views used for?(Choose all correct answers)
To modify elements. (*)
To navigate a hierarchy of information. (*)
To choose the file system location to delete a file.
16. What is the output of the following code segment:
int n = 13;
System.out.print(doNothing(n));
System.out.print(" ", n);
where the code from the function doNothin is:
public double doNothing(int n)
{
n = n + 8;
return (double) 12/n;
}
1.75, 13
0.571, 21
1.75, 21
0.571, 13 (*)
17. 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 (*) False
18. One advantage to using a WHILE loop over a FOR loop is that a WHILE loop always has a counter. True or false? True False (*)
19. Which of the following could be a reason to use a switch statement in a Java program?
Because it allows the code to be run through until a certain conditional statement is true.
Because it allows the program to run certain segments of code and neglect to run others based on the input given. (*)
Because it terminates the current loop.
Because it allows the user to enter an input in the console screen and prints out a message that the user input was successfully read in.
20. In Java, an instance field referenced using the this keyword generates a compilation error. True or false?
True False (*)
21. Consider
public class YourClass{ public YourClass(int i){/*code*/} // more code...}
To instantiate YourClass, what would you write?
YourClass y = new YourClass();
YourClass y = new YourClass(3); (*)
YourClass y = YourClass(3);
YourClass y = YourClass();
None of the above.
22. A constructor must have the same name as the class it is declared within. True or false?
True (*) False
23. Which of the following keywords are used to control access to the member of a class?
default
public (*)
class
All of the above.
None of the above.
24. Which of the following creates a method that compiles with no errors in the class?
(*)
All of the above.
None of the above
25. The following code creates an Object of type Horse. True or false?
Whale a=new Whale();
True False (*)
26. What operator do you use to call an object's constructor method and create a new object?
+
new (*)
instanceOf
27. Which of the following declares a one dimensional array name scores of type int that can hold 14 values?
int scores;
int[] scores=new int[14]; (*)
int[] scores=new int[14];
int score= new int[14]
28. Which of the following statements is not a valid array declaration?
int number[];
float []averages;
double marks[5];
counter int[]; (*)
29. What is the output of the following segment of code if the command line arguments are "a b c d e f"?
1
3
5
6 (*)
30. Which of the following declares a one dimensional array named names of size 8 so that all entries can be Strings?
String names=new String[8];
String[] name=new Strings[8];
String[] names=new String[8]; (*)
String[] name=String[8];
31. What will the following code segment output?
String s="\\\\\
System.out.println(s);
"\\\\\"
\\\\\\\\
\\
\\\\ (*)
32. Consider the following code snippet.
What is printed? Mark for Review
(1) Points
88888 (*)
88888888
1010778
101077810109
ArrayIndexOutofBoundsException is thrown
33. Given the code
String s1 = "abcdef";
String s2 = "abcdef";
String s3 = new String(s1);
Which of the following would equate to false?
s1 == s2
s1 = s2
s3 == s1 (*)
s1.equals(s2)
s3.equals(s1)
34. How would you use the ternary operator to rewrite this if statement?
if (balance < 500)
fee = 10;
else
fee = 0;
fee = ( balance < 500) ? 0 : 10;
fee= ( balance < 500) ? 10 : 0; (*)
fee = ( balance >= 5) ? 0 : 10;
fee = ( balance >= 500) ? 10 : 0;
fee = ( balance > 5) ? 10 : 0;
35. If an exception is thrown by a method, where can the catch for the exception be?
There does not need to be a catch in this situation.
The catch must be in the method that threw the exception.
The catch can be in the method that threw the exception or in any other method that called the method that threw the exception. (*)
The catch must be immediately after the throw.
36. Choose the best response to this statement: An error can be handled by throwing it and catching it just like an exception.
True. Errors and exceptions are the same objects and are interchangeable.
False. An error is much more severe than an exception and cannot be dealt with adequately in a program. (*
True. Although errors may be more severe than exceptions they can still be handled in code the same way exceptions are. False. Exceptions are caused by a mistake in the code and errors occur for no particular reason and therefore cannot be handled or avoided.
37. Which of the following could be a reason to throw an exception?
To eliminate exceptions from disrupting your program. (*)
You have a fatal error in your program.
You have encountered a Stack Overflow Error.
To make the user interface harder to navigate.
38. Suppose you misspell a method name when you call it in your program. Which of the following explains why this gives you an exception?
Because the parameters of the method were not met.
Because the interpreter does not recognize this method since it was never initialized, the correct spelling of the method was initialized.
Because the interpreter tries to read the method but when it finds the method you intended to use it crashes.
This will not give you an exception, it will give you an error when the program is compiled. (*)
39. Which of the following is the correct way to call an overriden method needOil() of a super class Robot in a subclass SqueakyRobot?
Robot.needOil(SqueakyRobot);
SqueakyRobot.needOil();
super.needOil(); (*)
needOil(Robot);
40. Why are hierarchies useful for inheritance?
They keep track of where you are in your program.
They restrict a superclass to only have one subclass.
They organize constructors and methods in a simplified fashion.
They are used to organize the relationship between a superclass and its subclasses. (*)
41. It is possible for a subclass to be a superclass. True or false?
True (*) False
42. Static methods can write to instance variables. True or false?
True False (*)
43. Static classes are designed as thread safe class instances. True or false?
True False (*)
44. Public static variables can't have their value reset by other classes. True or false?
True False (*)
45. Choose the correct implementation of a public access modifier for the method divide.
divide(int a, int b, public) {return a/b;}
public divide(int a, int b) {return a/b;} (*)
divide(int a, int b) {public return a/b;}
divide(public int a, public int b) {return a/b;}
46. Which of the following specifies accessibility to variables, methods, and classes?
Methods
Parameters
Overload constructors
Access specifiers (*)
47. 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(String a, int b);
counter(int[] numbers);
counter(1, 5, 8, 17, 11000005); (*)
counter("one","two",String[] nums);
48. Which of the following can be declared final?
Classes
Methods
Local variables
Method parameters
All of the above (*)
49. Which of the following would be most beneficial for this scenario?
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?
By ignoring the subject type and initializing all the book as objects of type Book.
By overriding the methods of Book.
Using polymorphism. (*)
This is not possible. Joe must find another way to collect the books.
50. What is Polymorphism?
A way of redefining methods with the same return type and parameters.
A way to create multiple methods with the same name but different parameters.
A class that cannot be initiated.
The concept that a variable or reference can hold multiple types of objects. (*)
Quiz 1 Sectiunea 6
1. Which of the following statements is a valid array declaration?
• int number();
• float average[]; (*)
• double[] marks; (*)
• counter int[];
2.The following array declaration is valid: int[] y = new int[5]; True (*) False
3. Which of the following declares a one dimensional array named "score" of type int that can hold 9 values?
• int score;
• int[] score;
• int[] score=new int[9]; (*)
• int score=new int[9];
4. Which of the following declares and initializes a two dimensional array?
• int[][] array={{1,1,1},{1,1,1},{1,1,1}}; (*)
• int[] array={{1,1,1},{1,1,1},{1,1,1}};
• int[][] array={1,1,1},{1,1,1},{1,1,1};
• int[][] array={1,1,1,1,1,1,1,1,1};
5. Which of the following declares and initializes a one dimensional array named words of size 10 so that all entries can be Strings?
• String words=new String[10];
• char words=new char[10];
• char[] words=new char[10];
• String[] words=new String[10]; (*)
6. What is the output of the following segment of code?
• 222220
• 0 (*)
• 220
• 2
• This code does not compile.
7. What is the output of the following segment of code?
• 1286864
• 643432
• 262423242322
• 666666 (*)
• This code does not compile.
8. 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?
• String[][] values={"apples","oranges","pears"};
• String[][] values=new String[3][2];
• String[][] values=new String[2][3]; (*)
• String[][] values;
9. Which of the following declares and initializes a two dimensional array where each element is a reference type?
• String words=new String[10];
• char[][] words;
• char[][] words=new char[10][4];
• String[][] words=new String[10][3]; (*)
10. 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]);} (*)
• System.out.println(prices.length);
• for(int i=1; i <= prices.length; i++){System.out.println(prices[i]);}
• for(int i=0; i <= prices.length; i++){System.out.println(prices[i]);}
11. What is the output of the following segment of code?
• 753
• 6
• 7766554433221
• 7531 (*)
? This code does not compile.
12. The following creates a reference in memory named y that can refer to five different integers via an index. True or false? int[] y = new int[5]; True (*) False
13. The following creates a reference in memory named z that can refer to seven different doubles via an index. True or false? double z[] = new double[7]; True (*) False
14. What is the output of the following segment of code if the command line arguments are "apples oranges pears"?
• apples
• pears (*)
• oranges
• args
• This code doesn't compile.
15. What is the output of the following segment of code if the command line arguments are "apples oranges pears"?
• 0
• 1
• 2
• 3 (*)
• This code does not compile.
16. What will be the content of array variable table after executing the following code?
• 0 0 0
0 0 0
0 0 0
• 1 0 0
0 1 0
0 0 1 (*)
• 1 0 0
1 1 0
1 1 1
• 0 0 1
0 1 0
1 0 0
17.What is the output of the following segment of code?
• 1286864 (*)
• 643432
• 262423242322
• 666666
• This code does not compile.
18. After execution of the following statement, which of the following are true?
int number[] = new int[5];
• number[0] is undefined
• number[4] is null
• number[2] is 0 (*)
• number.length() is 6
19. The following array declaration is valid. True or false? int x[] = int[10]; True False (*)
Quiz 2 Sectiunea 6
1. Consider the following code snippet. What is printed?
String river = new String("Hudson"); System.out.println(river.length());
6 (*) 7 8 Hudson river
2. Which of the following creates a String reference named str and instantiates it?
• String str;
• str="str";
• String s="str";
• String str=new String("str"); (*)
3. 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 (*) False
4. Which of the following statements declares a String object called name?
• String name; (*)
• String name
• int name;
• double name;
5. Suppose that s1 and s2 are two strings. Which of the statements or expressions are valid?
• String s3 = s1 + s2; (*)
• String s3 = s1 - s2;
• s1 <= s2
• s1.compareTo(s2); (*)
• int m = s1.length(); (*)
6. Consider the following code snippet. What is printed?
• PoliiPolii (*)
• Polii
• auaacauaac
• auaac
• ArrayIndexOutofBoundsException is thrown
7. What will the following code segment output?
• "\\\\\"
• \"\\\\\"
• "\\" (*)
• "\\\"
8. What will the following code segment output?
• ""\\"
• ""\"
• ""\
" (*)
• """\
""
• ""\
""
9. The following program prints "Equal". True or false?
• True (*)
• False
10. Which of the following creates a String named string?
• char string;
• String s;
• String string; (*)
• String String;
• String char;
11. Given the code, which of the following would equate to true?
String s1 = "yes";
String s2 = "yes";
String s3 = new String(s1);
• s1 == s2 (*)
• s1 = s2
• s3 == s1
• s1.equals(s2) (*)
• s3.equals(s1) (*)
12. The String methods equals and compareTo perform the exact same function. True or false?
True False (*)
13. The == operator can be used to compare two String objects. The result is always true if the two strings are identical. True or false? True False (*)
14. The following program prints "Equal". True or false?
• True
• False (*)
15. Given the code below, which of the following calls are valid? String s = new String("abc");
• s.trim() (*)
• s.replace('a', 'A') (*)
• s.substring(2) (*)
• s.toUpperCase() (*)
• s.setCharAt(1,'A')
16. Consider the following code snippet. What is printed?
String ocean = new String("Atlantic Ocean"); System.out.println(ocean.indexOf('a'));
0 2 3 (*) 11 12
17. Consider the following code snippet. What is printed?
• 55555
• 87668 (*)
• AtlanticPacificIndianArcticSouthern
• The code does not compile.
• An ArrayIndexOutofBoundsException is thrown.
18. Consider the following code snippet. What is printed?
• 55555
• 87658
• AtlanticPacificIndianArcticSouthern
• The code does not compile.
• An ArrayIndexOutofBoundsException is thrown. (*)
19.How would you use the ternary operator to rewrite this if statement?
if (skillLevel > 5)
numberOfEnemies = 10;
else
numberOfEnemies = 5;
• numberOfEnemies = ( skillLevel > 5) ? 5 : 10;
• numberOfEnemies = ( skillLevel < 5) ? 10 : 5;
• numberOfEnemies = ( skillLevel >= 5) ? 5 : 10;
• numberOfEnemies = ( skillLevel >= 5) ? 10 : 5;
• numberOfEnemies = ( skillLevel > 5) ? 10 : 5; (*)
20. How would you use the ternary operator to rewrite this if statement?
if (gender == "male")
System.out.print("Mr.");
else
System.out.print("Ms.");
• System.out.print( (gender == "male") ? "Mr." : "Ms." ); (*)
• System.out.print( (gender == "male") ? "Ms." : "Mr." );
• (gender == "male") ? "Mr." : "Ms." ;
• (gender == "male") ? "Ms." : "Mr." ;
Quiz 3 Sectiunea 6
1. Which of the following would give you an array index out of bounds exception?
• Misspelling a variable name somewhere in your code.
• Refering to an element of an array that is at an index less than the length of the array minus one.
• Using a single equal symbol to compare the value of two integers.
• Refering to an element of an array that is at an index greater than the length of that array minus one. (*)
• Unintentionally placing a semicolon directly after initializing a for loop.
2. What exception message indicates that a variable may have been mispelled somewhere in the program?
• variableName cannot be resolved to a variable (*)
• method methodName(int) is undefined for the type className
• Syntax error, insert ";" to complete statement
• All of the Above
3. Which of the following defines an Exception?
• A very severe non-fixable problem with interpreting and running your code.
• Code that has no errors and therefore runs smothly.
• A problem that can be corrected or handled by your code. (*)
• An interpreter reading your code.
4. What do exceptions indicate in Java?
• The code has considered and dealt with all possible cases.
• A mistake was made in your code. (*)
• There are no errors in your code.
• Exceptions do not indicate anything, their only function is to be thrown.
• The code was not written to handle all possible conditions. (*)
5. Which line of code shows the correct way to throw an exception?
• new throw Exception("Array index is out of bounds");
• throw new Exception("Array index is out of bounds"); (*)
• throw Exception("Array index is out of bounds");
• throws new Exception("Array index is out of bounds");
6. What does the interpreter look for when an exception is thrown?
• It does not look for anything. It just keeps reading through your code.
• It does not look for anything. It stops interpreting your code.
• The end of the code.
• A catch statement in the code. (*)
7. Which of the following would be a correct way to handle an index out of bounds exception?
• Throw the exception and catch it. In the catch, set the index to the index of the array closest to the one that was out of bounds. (*)
• Do nothing, it will fix itself.
• Throw the exception that prints out an error message. There is no need to have the catch handle the exception if it has already been thrown.
• Rewrite your code to avoid the exception by not permititng the use of an index that is not inside the array. (*)
8. A computer company has one million dollars to give as a bonus to the employees, and they wish to distribute it evenly amongst them.
The company writes a program to calculate the amount each employee receives, given the number of employees.
Unfortunately, the employees all went on strike before they heard about the bonus. This means that the company has zero employees.
What will happen to the program if the company enters 0 into the employment number?
• An unfixable error will occur.
• The program will calculate that each employee will receive zero dollars because there are zero employees.
• An exception will occur because it is not possible to divide by zero. (*)
• The programmers will have proven their worth in the company because without them the company wrote faulty code.
1. Select the statement that declares a number of type double and initializes it to 6 times 10 to the 5th power.
double number=6*10^5;
double number=6e5; (*)
double number=6(e5);
double number=6*10e5;
2. Which of the following expressions will evaluate to true when x and y are boolean variables with opposite values?
I. (x || y) && !(x && y)
II. (x && !y) || (!x && y)
III. (x || y) && (!x ||!y)
I only
II only
I and III
II and III
I, II, and III (*)
3. The six relational operators in Java are:
>,<,=,!,<=,>=
>,<,==,!=,<=,>= (*)
>,<,=,!=,<=,>=
>,<,=,!=,=<,=>
4. Examine the following code: What is the value of variable x?
2 (*)
2.5
6
14
5. What is the result when the following code segment is compiled and executed?
int x = 22, y = 10;
double p = Math.sqrt( ( x + y ) /2);
System.out.println(p);
Syntax error "sqrt(double) in java.lang.Math cannot be applied to int"
4.0 is displayed (*)
2.2 is displayed
5.656854249492381 is displayed
ClassCastException
6. Which of the following is not a legal name for a variable?
2bad (*)
zero
theLastValueButONe
year2000
7. What is the output of the following lines of code?
int j=6,k=8,m=2,result;
result=j-k%3*m;
System.out.println(result);
6
0
-42
2 (*)
8. 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?
True
False (*)
9. Which of the two diagrams below illustrate the correct syntax for variables used in an if-else statement?
Example A (*)
Example B
10. In an if-else construct the condition to be evaluated must end with a semi-colon. True or false?
True
False (*)
Incorrect. Refer to Section 4 Lesson 2.
11. In the code fragment below, the syntax for the for loop's initialization is correct. True or false?
public class ForLoop {
public static void main (String args[])
{
for ((int 1=10) (i<20) (i++))<
{System.out.Println ("i: "+i); }
}
}
True
False (*)
12. In Eclipse, when you run a Java Application, where may the results display?
Editor Window
Console View (*)
Debug View
Task List
None of the above
13. A combination of views and editors are referred to as _______________.
A workspace
A physical location
A perspective (*)
All of the above
14. You can return to the Eclipse Welcome Page by choosing Welcome from what menu?
File
Edit
Help (*)
Close
15. A workspace is:
The physical location onto which you will store and save your files.
The location where all projects are developed and modified.
The location where you can have one or more stored perspectives.
All of the above. (*)
16. What should replace the comment "//your answer here" in the code below if the code is meant to take no action when i % 2 is 0 (in other words when i is even)?
for(int i = 0; i < 10; i++){
if(i%2 == 0)
//your answer here
else
k+=3;
}
continue; (*)
break;
return;
k+=1;
17. One advantage to using a WHILE loop over a FOR loop is that a WHILE loop always has a counter. True or false?
True
False (*)
18. Consider that a Scanner has been initialized such that:
Scanner in = new Scanner(System.in);
Which of the following lines of code reads in the user's input and sets it equal to a new String called input?
String input = in.next(); (*)
String input = in.close();
String input = new String in.next();
String input = in.nextInt();
19. Switch statements work on all input types including, but not limited to, int, char, and String. True or false?
True
False (*)
20. What is wrong with the following class declaration?
class Account{ ;
privateint number;
privateString name;;
Account;;
}
Classes cannot include strings.
Classes cannot include mixed data types.
The constructor method has no definition. (*)
There is nothing wrong.
21. A class always has a constructor. True or false?
True (*)
False
22. Which of the following creates an instance of the class below?
ThisClass t=new ThisClass();
ThisClass t;
ThisClass t=new ThisClass(3,4);
ThisClass t=new ThisClass(5); (*)
Incorrect. Refer to Section 5 Lesson 2.
23. Which of the following creates a class named Student with one constructor, and 2 instance variables name and gpa?
public class Student { private String name; private float gpa; }
public class Student private String name; private float gpa; Student();
public class Student { private String name; private float gpa; Student(){ name="Jane Doe"; gpa=3.0;} } (*)
public class Student { private String name; Student{ name="Jane Doe"; float gpa=3.0; }
Incorrect. Refer to Section 5 Lesson 2.
24. Which of the following creates an object from the Animal class listed below:
Animal cat=new Animal();
Animal cat=Animal(50,30);
Animal cat=new Animal(50,30); (*)
Animal cat=new Animal(50);
25. What is true about the code below:
Car car1=new Car();
Car car2=new Car();
car2=car1;
The references car1 and car2 are pointing to two Car Objects in memory.
The reference car2 points to an exact copy of the Car Object that car1 references.
There are no more Car objects in memory.
There is a Car object that car1 referenced that is now slated for removal by the garbage collector. (*)
There is a Car object that car2 referenced that is now slated for removal by the garbage collector.
Incorrect. Refer to Section 5 Lesson 2.
26. A constructor must have the same name as the class it is declared within. True or false?
True (*)
False
Section 6
27. What is the output of the following segment of code?
int array[][] = {{1,2,3},{3,2,1}};
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
System.out.print(2*array[1][1]);
444444 (*)
123321
246642
222222
This code doesn't compile.
28. Which of the following statements adds 5 to every element of the one dimensional array prices and then prints it to the screen?
for(int i=0;i<prices.length;i++)
System.out.println(prices[i]+5);
System.out.println(prices[i]+5);
for(int i=1;i<prices.length;i++)
System.out.println(prices[i]+5);
for(int i=0;i<prices.length;i++)
System.out.println(prices[1]+5); (*)
Incorrect. Refer to Section 6 Lesson 1.
29. Which of the following declares and initializes a two dimensional array that can hold 6 Object reference types?
String[] array=new String[6];
Object array=new Object[6];
Object[][] array=new Object[2][3]; (*)
String[][] array=String[6];
30. Which of the following declares and initializes a one dimensional array that can hold 5 Object reference types?
String[] array=new String[5];
Object array=new Object[5]; (*)
Object[] array=new Object[4];
String[] array=String[4];
Incorrect. Refer to Section 6 Lesson 1.
31. Which of the following creates a String reference named s and instantiates it?
String s=""; (*)
s="s";
String s;
String s=new String("s"); (*)
32. What will the following code segment output?
String s="\\\n\"\n\\\n\"";
System.out.println(s);
\" \"
""\
""
\
""
\
"
\
" (*)
"
\
"
\
"
"
Incorrect. Refer to Section 6 Lesson 2.
33. The == operator can be used to compare two String objects. The result is always true if the two strings are have the exact same characters in each position of the String. True or false?
True
False (*)
34. Given the code
String s1 = "abcdef";
String s2 = "abcdef";
String s3 = new String(s1);
Which of the following would equate to false?
s1 == s2
s1 = s2
s3 == s1 (*)
s1.equals(s2)
s3.equals(s1)
35. 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?
True
False (*)
36. Which of the following correctly matches the symbol with its function?
== (two equal signs) compares values of primitive types such as int or char. (*)
== (two equal signs) compares the values of non-primitive objects.
== (two equal signs) compares the memory location of non-primitive objects. (*)
= (single equals sign) compares the value of primitive types such as int or char.
.equals() compares the value of non-primitive objects. (*)
37. If an exception is thrown by a method, where can the catch for the exception be?
There does not need to be a catch in this situation.
The catch must be in the method that threw the exception.
The catch can be in the method that threw the exception or in any other method that called the method that threw the exception. (*)
The catch must be immediately after the throw.
38. What does it mean to catch an exception?
It means you have fixed the error.
It means to throw it.
It means to handle it. (*)
It means there was never an exception in your code.
Section 7
39. Forward thinking helps when creating linear recursive methods. True or false?
True
False (*)
Incorrect. Refer to Section 7 Lesson 2.
40. Which case handles the last recursive call?
The base case (*)
The primary case
The secondary case
The convergence case
The recursive case
Incorrect. Refer to Section 7 Lesson 2.
41. Static methods can't change any class variable values at run-time. True or false?
True False (*)
Incorrect. Refer to Section 7 Lesson 2.
42. It is possible to return an object in a method. True or false?
True (*) False
43. Which segment of code represents a correct way to define a variable argument method?
String easyArray(String... elems) {//code} (*)
String easyArray(...String elems) {//code}
String... easyArray(String elems) {//code}
Integer easyArray... (int elems) {//code}
44. Choose the correct implementation of a public access modifier for the method divide.
divide(int a, int b, public) {return a/b;}
public divide(int a, int b) {return a/b;} (*)
divide(int a, int b) {public return a/b;}
divide(public int a, public int b) {return a/b;}
45. What is true about the Object class?
It is the highest superclass. (*)
It extends other classes.
Its methods can be overridden in subclasses. (*)
Its methods can be overloaded in subclasses. (*)
46. Would this code be correct if a Dog is a HousePet? Why or Why not?
HousePet Scooby = new Dog();
Yes, because it is an abstract class.
Yes, because polymorphism allows this since Dog is a subclass of HousePet. (*)
No, because ref must be declared either a HousePet or a Dog, but not both.
Maybe. There is no way to tell without seeing the methods for Dog and the methods for HousePet.
47. If it is possible to inherit from an abstract class, what must you do to prevent a compiler error from occurring?
It is not possible to inherit from an abstract class.
Create all new methods and variables different from the parent class.
Override all abstract methods from the parent class. (*)
Declare the child class as abstract. (*)
48. Which of the following correctly describes an Is-A relationship?
A helpful term used to conceptualize the relationships among nodes or leaves in an inheritance hierarchy. (*)
A programming philosophy that promotes simpler, more efficient coding by using exiting code for new applications.
It restricts access to a specified segment of code.
A programming philosophy that promotes protecting data and hiding implementation in order to preserve the integrity of data and methods.
49. An access modifier is a keyword that allows subclasses to access methods, data, and constructors from their parent class. True or false?
True (*)
False
50. Which of the following demonstrates the correct way to create an applet Battlefield?
public class Battlefield extends Applet{...} (*)
public Applet Battlefield{...}
public class Applet extends Battlefield{...}
public class Battlefield(Applet){...}
1. What is encapsulation? Mark for Review
(1) Points
A keyword that allows or restricts access to data and methods.
A programming philosophy that promotes simpler, more efficient coding by using exiting code for new applications.
A structure that categorizes and organizes relationships among ideas, concepts of things with the most general at the top and the most specific at the bottom.
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?
Mark for Review
(1) Points
(*)
3. Which of the following demonstrates the correct way to create an applet Battlefield? Mark for Review
(1) Points
public class Applet extends Battlefield{...}
public class Battlefield extends Applet{...} (*)
public class Battlefield(Applet){...}
public Applet Battlefield{...}
4. What is the Java keyword final used for in a program? Mark for Review
(1) Points
It permits access to the class variables and methods from anywhere.
It restricts a class from being extendable and restricts methods from being overridden. (*)
It permits redefining methods of a parent class inside the child class, with the same name, parameters, and return type.
There is no such keyword in Java.
It terminates the program.
5. Which of the following are true about abstract methods? Mark for Review
(1) Points
(Choose all correct answers)
They may contain implementation.
They must be overloaded.
They must be overridden in a non-abstract subclass. (*)
They must be declared in an abstract class. (*)
They cannot have a method body. (*)
6. Which of the following would be most beneficial for this scenario?
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?
Mark for Review
(1) Points
By overriding the methods of Book.
Using polymorphism. (*)
This is not possible. Joe must find another way to collect the books.
By ignoring the subject type and initializing all the book as objects of type Book.
7. Consider
public class YourClass{
public YourClass(int i)
{/*code*/}
// more code...}
To instantiate YourClass, what would you write? Mark for Review
(1) Points
YourClass y = new YourClass();
YourClass y = new YourClass(3); (*)
YourClass y = YourClass(3);
YourClass y = YourClass();
None of the above.
8. The following code creates an object of type Animal:
Animal a; Mark for Review
(1) Points
True
False (*)
9. The basic unit of encapsulation in Java is the primitive data type. True or false? Mark for Review
(1) Points
True
False (*)
10. You can create static class methods inside any Java class. True or false? Mark for Review
(1) Points
True (*)
False
11. Any instance of the same class can assign a new value to a static variable. True or false? Mark for Review
(1) Points
True (*)
False
12. Which of the following access modifiers doesn't work with a static variable? Mark for Review
(1) Points
private
default
protected
public
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? Mark for Review
(1) Points
int DolphinTalk(dol1, dol2){ if(dol1 > dol2) return dol1; else return dol2;}
int DolphinTalk(int,int){ if(dol1 > dol2) return dol1; else return dol2;}
int DolphinTalk(int dol1,int dol2){ if(dol1 > dol2) return dol1; else return dol2;} (*)
int DolphinTalk, int dol1,int dol2 { if(dol1 > dol2) return dol1; else return dol2;}
All of the above
14. Which of the following could be a reason to return an object? Mark for Review
(1) Points
Because you wish to be able to use that object inside of the method.
It has faster performance than returning a primitive type.
The method makes changes to the object and you wish to continue to use the updated object outside of the method. (*)
None of the above. It is not possible to return an object.
15. Which of the following specifies accessibility to variables, methods, and classes? Mark for Review
(1) Points
Overload constructors
Methods
Parameters
Access modifiers (*)
30 komentar
Thanks for sharing valuable information and very well explained. Keep posting.
mulesoft training in bangalore
mulesoft training hyderabad
Very nice information. Thanks For sharing good article.I have written java8 blog visit Java8 Tutorials
kayseriescortu.com - alacam.org - xescortun.com
mmorpg oyunlar
instagram takipçi satın al
tiktok jeton hilesi
Tiktok Jeton Hilesi
antalya saç ekimi
TAKİPÇİ SATIN AL
instagram takipçi satın al
MT2 PVP SERVERLAR
takipçi
YENİ PERDE MODELLERİ
sms onay
mobil ödeme bozdurma
nft nasıl alınır
ankara evden eve nakliyat
trafik sigortası
Dedektör
web sitesi kurma
aşk kitapları
en son çıkan perde modelleri
yurtdışı kargo
nft nasıl alınır
en son çıkan perde modelleri
minecraft premium
uc satın al
lisans satın al
özel ambulans
Nice Blog
Google'a link vermeyi denedim ben bu deneme yorumu ...
.
Müşteri Hizmetleri Direk Bağlanma Garanti Bankası, Vakıfbank, Türk Telekom, Ziraat Bankası Müşteri Hizmetleri Direk Bağlanma.
Garanti Bankası Müşteri Hizmetleri İletişim Çağrı Merkezi Telefon Numarası Garanti Bankası Müşteri Hizmetleri Direk Bağlanma
Vakıfbank Müşteri Hizmetlerine Direk Bağlanma 2022 (3 Yol Var) Vakıfbank Müşteri Hizmetleri Direk Bağlanma.
Konya Perde Yıkama konusunda bir numara hizmet almak istiyorsanız doğru adres. Fazla aramanıza gerek yok.
Windows 11 Pro Key Satın Al
Good content. You write beautiful things.
hacklink
mrbahis
korsan taksi
sportsbet
mrbahis
sportsbet
hacklink
vbet
vbet
5 axis Machine Manufacturers
Dengebahis için güncel adres arıyorsanız bu adresi kullanabilirsiniz. Dengebahis
Hullbet için güncel adres arıyorsanız bu adresi kullanabilirsiniz.
Hullbet
Kriterbahis için güncel adres arıyorsanız bu adresi kullanabilirsiniz.
Kriter Bahis
https://cartadeconducaoportuguesaexpressa.com00
https://permisdeconducereromanesc.com4
https://expresszmagyarjogositvany.comWSFFSD
https://expresszmagyarjogositvany.com454
https://carnetdeconducirespanolexpres.comO
hatay
kars
mardin
samsun
urfa
İV53G4
whatsapp görüntülü show
ücretli.show
TKDGO0
보성콜걸
여수콜걸
익산콜걸
정읍콜걸
남원콜걸
군산콜걸
전주콜걸
김제콜걸
공주콜걸
https://inmatricularepermisdeconducereromaneasca.com/
Cumpărați permisul de conducere românesc fără a susține niciun test sau examene
Thanks a bunch for sharing your content! I always love diving into exceptional material that provides valuable insights. The concepts you present are truly extraordinary and incredibly engaging, making your post a genuine source of delight. Keep up the fantastic work
visit- Python for Blockchain and Cryptocurrency Development
Casino
Casino
Bahis
Vbet giriş vbet
شركة مكافحة النمل الابيض بالاحساء KbueHOirvpشركة مكافحة النمل الابيض بالاحساء NGycJrFa5F
EmoticonEmoticon