Java Certification Test Paper On Java Building Block

Java Certification Test Paper on Java Building Block topic expect the candidates to be aware of building blocks of java. This article not only covers the exam essential but also give the test papers along with explanation. Candidates are requested to attempt those questions without looking into the answers and then read the explanation in detail. If explanation is does not give enough understanding, then read the java building block topic once again.

Java Building Block Exam Essential

Be able to write code using a main() method. A main() method is usually written as publicstatic void main(String[] args). Arguments are referenced starting with args[0]. Accessingan argument that wasnt passed in will cause the code to throw an exception.

Understand the effect of using packages and imports. Packages contain Java classes.Classes can be imported by class name or wildcard. Wildcards do not look at subdirectories.In the event of a confl ict, class name imports take precedence.

Be able to recognize a constructor. A constructor has the same name as the class. It looks like a method without a return type.Be able to identify legal and illegal declarations and initialization. Multiple variables canbe declared and initialized in the same statement when they share a type. Local variables require an explicit initialization; others use the default value for that type. Identifiers maycontain letters, numbers, $, or _. Identifi ers may not begin with numbers. Numeric literalsmay contain underscores between two digits and begin with 19, 0, 0x, 0X, 0b, and 0B.

Be able to determine where variables go into and out of scope. All variables go into scopewhen they are declared. Local variables go out of scope when the block they are declaredin ends. Instance variables go out of scope when the object is garbage collected. Class variablesremain in scope as long as the program is running.

Be able to recognize misplaced statements in a class. Package and import statements areoptional. If present, both go before the class declaration in that order. Fields and methodsare also optional and are allowed in any order within the class declaration.

Know how to identify when an object is eligible for garbage collection. Draw a diagramto keep track of references and objects as you trace the code. When no arrows point to abox (object), it is eligible for garbage collection.

Java Certification Test Paper on Java Building Block

1. Which of the following are valid Java identifiers? (Choose all that apply)
A. A$B
B. _helloWorld
C. true
D. java.lang

E. Public F. 1980_s 2. What is the output of the following program?

1: public class WaterBottle {
2: private String brand;
3: private boolean empty;
4: public static void main(String[] args) {
5: WaterBottle wb = new WaterBottle();
6: System.out.print("Empty = " + wb.empty);
7: System.out.print(", Brand = " + wb.brand);
8: } }

A. Line 6 generates a compiler error.
B. Line 7 generates a compiler error.
C. There is no output.
D. Empty = false, Brand = null

E. Empty = false, Brand = F. Empty = null, Brand = null 3. Which of the following are true? (Choose all that apply)

4: short numPets = 5;
5: int numGrains = 5.6;
6: String name = "Scruffy";
7: numPets.length();
8: numGrains.length();
9: name.length();

A. Line 4 generates a compiler error.
B. Line 5 generates a compiler error.
C. Line 6 generates a compiler error.
D. Line 7 generates a compiler error.

E. Line 8 generates a compiler error. F. Line 9 generates a compiler error. G. The code compiles as is. 4. Given the following class, which of the following is true? (Choose all that apply)

1: public class Snake {
2:
3: public void shed(boolean time) {
4:
5: if (time) {
6:
7: }
8: System.out.println(result);
9:
10: }
11: }

A. If String result = "done"; is inserted on line 2, the code will compile.
B. If String result = "done"; is inserted on line 4, the code will compile.
C. If String result = "done"; is inserted on line 6, the code will compile.
D. If String result = "done"; is inserted on line 9, the code will compile.

E. None of the above changes will make the code compile. 5. Given the following classes, which of the following can independently replace INSERTIMPORTS HERE to make the code compile? (Choose all that apply)

package aquarium;
public class Tank { }
package aquarium.jellies;
public class Jelly { }
package visitor;
INSERT IMPORTS HERE
public class AquariumVisitor {
public void admire(Jelly jelly) { } }

A. import aquarium.*;
B. import aquarium.*.Jelly;
C. import aquarium.jellies.Jelly;
D. import aquarium.jellies.*;

E. import aquarium.jellies.Jelly.*; F. None of these can make the code compile. 6. Given the following classes, what is the maximum number of imports that can be removedand have the code still compile?

package aquarium; public class Water { }
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); } }

A. 0
B. 1
C. 2
D. 3

E. 4 F. Does not compile. 7. Given the following classes, which of the following snippets can be inserted in place ofINSERT 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;
}

A. import aquarium.*;
B. import aquarium.Water; import aquarium.jellies.*;
C. import aquarium.*; import aquarium.jellies.Water;
D. import aquarium.*;

import aquarium.jellies.*; E. import aquarium.Water; import aquarium.jellies.Water; F. None of these imports can make the code compile. 8. Given the following class, which of the following calls print out Blue Jay? (Choose all that apply)

public class BirdDisplay {
public static void main(String[] name) {
System.out.println(name[1]);
} }

A. java BirdDisplay Sparrow Blue Jay
B. java BirdDisplay Sparrow "Blue Jay"
C. java BirdDisplay Blue Jay Sparrow
D. java BirdDisplay "Blue Jay" Sparrow

E. java BirdDisplay.class Sparrow "Blue Jay" F. java BirdDisplay.class "Blue Jay" Sparrow G. Does not compile. 9. Which of the following legally fill in the blank so you can run the main() method from thecommand line? (Choose all that apply)

public static void main( )

A. String[] _names
B. String[] 123
C. String abc[]
D. String _Names[]

E. String... $n F. String names G. None of the above. 10. Which of the following are legal entry point methods that can be run from the commandline? (Choose all that apply)
A. private static void main(String[] args)
B. public static final main(String[] args)
C. public void main(String[] args)
D. public static void test(String[] args)

E. public static void main(String[] args) F. public static main(String[] args) G. None of the above. 11. Which of the following are true? (Choose all that apply)
A. An instance variable of type double defaults to null.
B. An instance variable of type int defaults to null.
C. An instance variable of type String defaults to null.
D. An instance variable of type double defaults to 0.0.

E. An instance variable of type int defaults to 0.0. F. An instance variable of type String defaults to 0.0. G. None of the above. 12. Which of the following are true? (Choose all that apply)
A. A local variable of type boolean defaults to null.
B. A local variable of type float defaults to 0.
C. A local variable of type Object defaults to null.
D. A local variable of type boolean defaults to false.

E. A local variable of type boolean defaults to true. F. A local variable of type float defaults to 0.0. G. None of the above. 13. Which of the following are true? (Choose all that apply)
A. An instance variable of type boolean defaults to false.
B. An instance variable of type boolean defaults to true.
C. An instance variable of type boolean defaults to null.
D. An instance variable of type int defaults to 0.

E. An instance variable of type int defaults to 0.0. F. An instance variable of type int defaults to null. G. None of the above. 14. Given the following class in the file /my/directory/named/A/Bird.java:

INSERT CODE HERE
public class Bird { }
Which of the following replaces INSERT CODE HERE if we compile from /my/directory?(Choose all that apply)
A. package my.directory.named.a;
B. package my.directory.named.A;
C. package named.a;
D. package named.A;

E. package a; F. package A; G. Does not compile. 15. Which of the following lines of code compile? (Choose all that apply)
A. int i1 = 1_234;
B. double d1 = 1_234_.0;
C. double d2 = 1_234._0;
D. double d3 = 1_234.0_;

E. double d4 = 1_234.0; F. None of the above. 16. Given the following class, which of the following lines of code can replace INSERT CODEHERE to make the code compile? (Choose all that apply)

public class Price {
public void admission() {
INSERT CODE HERE
System.out.println(amount);
} }

A. int amount = 9L;
B. int amount = 0b101;
C. int amount = 0xE;
D. double amount = 0xE;

E. double amount = 1_2_.0_0; F. int amount = 1_2_; G. None of the above. 17. Which of the following are true? (Choose all that apply)

public class Bunny {
public static void main(String[] args) {
Bunny bun = new Bunny();
} }

A. Bunny is a class.
B. bun is a class.
C. main is a class.
D. Bunny is a reference to an object.

E. bun is a reference to an object. F. main is a reference to an object. G. None of the above. 18. Which represent the order in which the following statements can be assembled into a programthat will compile successfully? (Choose all that apply)

A: class Rabbit {}
B: import java.util.*;
C: package animals;

A. A, B, C
B. B, C, A
C. C, B, A
D. B, A

E. C, A F. A, C G. A, B 19. Suppose we have a class named Rabbit. Which of the following statements are true?(Choose all that apply)

1: public class Rabbit {
2: public static void main(String[] args) {
3: Rabbit one = new Rabbit();
4: Rabbit two = new Rabbit();
5: Rabbit three = one;
6: one = null;
7: Rabbit four = one;
8: three = null;
9: two = null;
10: two = new Rabbit();
11: System.gc();
12: } }

A. The Rabbit object from line 3 is first eligible for garbage collection immediatelyfollowing line 6.
B. The Rabbit object from line 3 is first eligible for garbage collection immediatelyfollowing line 8.
C. The Rabbit object from line 3 is first eligible for garbage collection immediatelyfollowing line 12.
D. The Rabbit object from line 4 is first eligible for garbage collection immediatelyfollowing line 9.

E. The Rabbit object from line 4 is first eligible for garbage collection immediatelyfollowing line 11. F. The Rabbit object from line 4 is first eligible for garbage collection immediatelyfollowing line 12. 20. What is true about the following code? (Choose all that apply)

public class Bear {
protected void finalize() {
System.out.println("Roar!");
}
Review Questions 49
public static void main(String[] args) {
Bear bear = new Bear();
bear = null;
System.gc();
} }}

A. finalize() is guaranteed to be called.
B. finalize() might or might not be called
C. finalize() is guaranteed not to be called.
D. Garbage collection is guaranteed to run.

E. Garbage collection might or might not run. F. Garbage collection is guaranteed not to run. G. The code does not compile. 21. What does the following code output?

1: public class Salmon {
2: int count;
3: public void Salmon() {
4: count = 4;
5: }
6: public static void main(String[] args) {
7: Salmon s = new Salmon();
8: System.out.println(s.count);
9: } }

A. 0
B. 4
C. Compilation fails on line 3.
D. Compilation fails on line 4.

E. Compilation fails on line 7. F. Compilation fails on line 8. 22. Which of the following are true statements? (Choose all that apply)
A. Java allows operator overloading.
B. Java code compiled on Windows can run on Linux.
C. Java has pointers to specific locations in memory.
D. Java is a procedural language.

E. Java is an object-oriented language. F. Java is a functional programming language. 23. Which of the following are true? (Choose all that apply)
A. javac compiles a .class file into a .java file.
B. javac compiles a .java file into a .bytecode file.
C. javac compiles a .java file into a .class file.
D. Java takes the name of the class as a parameter.

E. Java takes the name of the .bytecode file as a parameter. F. Java takes the name of the .class file as a parameter.

Test Paper Answers & Explanation

1. A, B, E. Option A is valid because you can use the dollar sign in identifiers. Option B isvalid because you can use an underscore in identifiers. Option C is not a valid identifierbecause true is a Java reserved word. Option D is not valid because the dot (.) is notallowed in identifiers. Option E is valid because Java is case sensitive, so Public is not a reserved word and therefore a valid identifier. Option F is not valid because the firstcharacter is not a letter, $, or _. 2.
D. Boolean fields initialize to false and references initialize to null, so empty is falseand brand is null. Brand = null is output. 3. B, D, E. Option A (line 4) compiles because short is an integral type. Option B (line5) generates a compiler error because int is an integral type, but 5.6 is a floating-pointtype. Option C (line 6) compiles because it is assigned a String. Options D and E (lines7 and 8) do not compile because short and int are primitives. Primitives do not allow methods to be called on them. Option F (line 9) compiles because length() is definedon String. 4. A,
B. Adding the variable at line 2 makes result an instance variable. Since instancevariables are in scope for the entire life of the object, option A is correct. Option B iscorrect because adding the variable at line 4 makes result a local variable with a scope of the whole method. Adding the variable at line 6 makes result a local variable with a scope of lines 67. Since it is out of scope on line 8, the println does not compile andoption C is incorrect. Adding the variable at line 9 makes result a local variable witha scope of lines 9 and 10. Since line 8 is before the declaration, it does not compile andoption D is incorrect. Finally, option E is incorrect because the code can be made to compile. 5. C,
D. Option C is correct because it imports Jelly by classname. Option D is correctbecause it imports all the classes in the jellies package, which includes Jelly.Option A is incorrect because it only imports classes in the aquarium packageTankin this caseand not those in lower-level packages. Option B is incorrect because you cannot use wildcards anyplace other than the end of an import statement. Option E isincorrect because you cannot import parts of a class with a regular import statement.Option F is incorrect because options C and D do make the code compile. 6. E. The first two imports can be removed because java.lang is automatically imported.The second two imports can be removed because Tank and Water are in the same package,making the correct answer E. If Tank and Water were in different packages, one ofthese two imports could be removed. In that case, the answer would be option D. 7. A, B,
C. Option A is correct because it imports all the classes in the aquarium packageincluding aquarium.Water. Options B and C are correct because they import Water byclassname. Since importing by classname takes precedence over wildcards, these compile.Option D is incorrect because Java doesnt know which of the two wildcard Waterclasses to use. Option E is incorrect because you cannot specify the same classname intwo imports. 8.
B. Option B is correct because arrays start counting from zero and strings with spacesmust be in quotes. Option A is incorrect because it outputs Blue. C is incorrect becauseit outputs Jay. Option D is incorrect because it outputs Sparrow. Options E and F are incorrect because they output Error: Could not find or load main class Bird- Display.class. 9. A, C, D, E. Option A is correct because it is the traditional main() method signature and variables may begin with underscores. Options C and D are correct because the array operator may appear after the variable name. Option E is correct because varargs are allowed in place of an array. Option B is incorrect because variables are not allowed to begin with a digit. Option F is incorrect because the argument must bean array or varargs. Option F is a perfectly good method. However, it is not one thatcan be run from the command line because it has the wrong parameter type. 10. E. Option E is the canonical main() method signature. You need to memorize it.Option A is incorrect because the main() method must be public. Options B and Fare incorrect because the main() method must have a void return type. Option C isincorrect because the main() method must be static. Option D is incorrect because the main() method must be named main. 11. C,
D. Option C is correct because all non-primitive values default to null. Option D iscorrect because float and double primitives default to 0.0. Options B and E are incorrectbecause int primitives default to 0. 12. G. Option G is correct because local variables do not get assigned default values. Thecode fails to compile if a local variable is not explicitly initialized. If this question were about instance variables, options D and F would be correct. A boolean primitivedefaults to false and a float primitive defaults to 0.0. 13. A,
D. Options A and D are correct because boolean primitives default to false andint primitives default to 0. 14.
D. The package name represents any folders underneath the current path, which is named.A in this case. Option B is incorrect because package names are case sensitive, just like variable names and other identifiers. 15. A, E. Underscores are allowed as long as they are directly between two other digits.This means options A and E are correct. Options B and C are incorrect because theunderscore is adjacent to the decimal point. Option D is incorrect because the underscoreis the last character. 16. B, C,
D. 0b is the prefix for a binary value and is correct. 0x is the prefix for a hexadecimalvalue. This value can be assigned to many primitive types, including int anddouble, making options C and D correct. Option A is incorrect because 9L is a longvalue. long amount = 9L would be allowed. Option E is incorrect because the underscore is immediately before the decimal. Option F is incorrect because the underscore isthe very last character. 17. A, E. Bunny is a class, which can be seen from the declaration: public class Bunny. bunis a reference to an object. main() is a method. 18. C, D, E. package and import are both optional. If both are present, the order mustbe package, then import, then class. Option A is incorrect because class is beforepackage and import. Option B is incorrect because import is before package. OptionF is incorrect because class is before package. Option G is incorrect because class is before import. 19. B,
D. The Rabbit object from line 3 has two references to it: one and three. The referencesare nulled out on lines 6 and 8, respectively. Option B is correct because thismakes the object eligible for garbage collection after line 8. Line 7 sets the reference four to the now null one, which means it has no effect on garbage collection. The Rabbit object from line 4 only has a single reference to it: two. Option D is correct becausethis single reference becomes null on line 9. The Rabbit object declared on line 10becomes eligible for garbage collection at the end of the method on line 12. CallingSystem.gc() has no effect on eligibility for garbage collection. 20. B, E. Calling System.gc() suggests that Java might wish to run the garbage collector.Java is free to ignore the request, making option E correct. finalize() runs if an objectattempts to be garbage collected, making option B correct. 21.
A. While the code on line 3 does compile, it is not a constructor because it has a returntype. It is a method that happens to have the same name as the class. When the coderuns, the default constructor is called and count has the default value (0) for an int. 22. B, E. C++ has operator overloading and pointers. Java made a point of not havingeither. Java does have references to objects, but these are pointing to an object that canmove around in memory. Option B is correct because Java is platform independent.Option E is correct because Java is object oriented. While it does support some parts offunctional programming, these occur within a class. 23. C,
D. Java puts source code in .java files and bytecode in .class files. It does not usea .bytecode file. When running a Java program, you pass just the name of the classwithout the .class extension.