Quiz 1 Sectiunea 5

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?

/ (*)

Sectiunea 5 2