Review Questions 2  – Building Blocks – 1Z0-829 Study Guide

  • Which of the following code snippets about var compile without issue when used in a method? (Choose all that apply.)
  1. var spring = null;
  • var fall = “leaves”;
  • var evening = 2; evening = null;
  • var night = Integer.valueOf(3);
  • var day = 1/0;
  • var winter = 12, cold;
  • var fall = 2, autumn = 2;
  • var morning = “”; morning = null;
  • Which of the following are correct? (Choose all that apply.)
  1. An instance variable of type float defaults to 0.
  • An instance variable of type char defaults to null.
  • A local variable of type double defaults to 0.0.
  • A local variable of type int defaults to null.
  • A class variable of type String defaults to null.
  • A class variable of type String defaults to the empty string “”.
  • None of the above.
  1. Which of the following expressions, when inserted independently into the blank line, allow the code to compile? (Choose all that apply.)

public void printMagicData() {

var magic = ;

System.out.println(magic);

}

  1. 3_1
  • 1_329_.0
  • 3_13.0_
  • 5_291._2
  • 2_234.0_0
  • 9___6
  • _1_3_5_0
  1. Given the following two class files, what is the maximum number of imports that can be removed and have the code still compile?
  • Water.java package aquarium; public class Water { }
  • Tank.java package aquarium; import java.lang.*; import java.lang.System; import aquarium.Water; import aquarium.*; public class Tank {

public void print(Water water) { System.out.println(water); } }

  1. 0
  1. 1
  • 2
  • 3
  • 4
  • Does not compile
  1. Which statements about the following class are correct? (Choose all that apply.)
  1. public class ClownFish {
  • int gills = 0, double weight=2;
  • { int fins = gills; }
  • void print(int length = 3) {
  • System.out.println(gills);
  • System.out.println(weight);
  • System.out.println(fins);
  • System.out.println(length);
  • } }
  1. Line 2 generates a compiler error.
  • Line 3 generates a compiler error.
  • Line 4 generates a compiler error.
  • Line 7 generates a compiler error.
  • The code prints 0.
  • The code prints 2.0.
  • The code prints 2.
  • The code prints 3.
  1. Given the following classes, which of the following snippets can independently be inserted in place of INSERT IMPORTS HERE and have the code compile? (Choose all that apply.)

package aquarium;

public class Water {

boolean salty = false;

}

package aquarium.jellies;

public class Water {

boolean salty = true;

}

package employee;

INSERT IMPORTS HERE

public class WaterFiller {

Water water;

}

  1. import aquarium.*;
  • import aquarium.Water; import aquarium.jellies.*;
  • import aquarium.*;

import aquarium.jellies.Water;

  • import aquarium.*;

import aquarium.jellies.*;

  • import aquarium.Water;

import aquarium.jellies.Water;

  • None of these imports can make the code compile.
  1. Which of the following statements about the code snippet are true? (Choose all that apply.)
  • short numPets = 5L;
  • int numGrains = 2.0;
  • String name = “Scruffy”;
  • int d = numPets.length();
  • int e = numGrains.length;
  • int f = name.length();
  1. Line 3 generates a compiler error.
  • Line 4 generates a compiler error.
  • Line 5 generates a compiler error.
  • Line 6 generates a compiler error.
  • Line 7 generates a compiler error.
  • Line 8 generates a compiler error.
  1. Which of the following statements about garbage collection are correct? (Choose all that apply.)
  1. Calling System.gc() is guaranteed to free up memory by destroying objects eligible for garbage collection.
  • Garbage collection runs on a set schedule.
  • Garbage collection allows the JVM to reclaim memory for other objects.
  • Garbage collection runs when your program has used up half the available memory.
  • An object may be eligible for garbage collection but never removed from the heap.
  • An object is eligible for garbage collection once no references to it are accessible in the program.
  • Marking a variable final means its associated object will never be garbage collected.