Quiz
Chapter 1 Java Building Blocks
Create a free account to finish this quiz
Save your progress and see your final score.
Already have an account? Sign in
Questions in this quiz (23)
1Which of the following are valid Java identifiers? (Choose all that apply)
- AA$B
- B_helloWorld
- Ctrue
- Djava.lang
- EPublic
- F1980_s
2What is the output of the following program?
- ALine 6 generates a compiler error.
- BLine 7 generates a compiler error.
- CThere is no output.
- DEmpty = false, Brand = null
- EEmpty = false, Brand =
- FEmpty = null, Brand = null
3Which 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();- ALine 4 generates a compiler error.
- BLine 5 generates a compiler error.
- CLine 6 generates a compiler error.
- DLine 7 generates a compiler error.
- ELine 8 generates a compiler error.
- FLine 9 generates a compiler error.
- GThe code compiles as is.
4Given 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: }- AIf String result = "done"; is inserted on line 2, the code will compile.
- BIf String result = "done"; is inserted on line 4, the code will compile.
- CIf String result = "done"; is inserted on line 6, the code will compile.
- DIf String result = "done"; is inserted on line 9, the code will compile.
- ENone of the above changes will make the code compile.
5Given the following classes, which of the following can independently replace INSERT
IMPORTS 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) { } }- Aimport aquarium.*;
- Bimport aquarium.*.Jelly;
- Cimport aquarium.jellies.Jelly;
- Dimport aquarium.jellies.*;
- Eimport aquarium.jellies.Jelly.*;
- FNone of these can make the code compile.
6Given the following classes, what is the maximum number of imports that can be removed
and 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); } }- A0
- B1
- C2
- D3
- E4
- FDoes not compile.
7Given the following classes, which of the following snippets can 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;
}- Aimport aquarium.*;
- Bimport aquarium.Water;
import aquarium.jellies.*; - Cimport aquarium.*;
import aquarium.jellies.Water; - Dimport aquarium.*;
import aquarium.jellies.*; - Eimport aquarium.Water;
import aquarium.jellies.Water; - FNone of these imports can make the code compile.
8Given 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]);
} }- Ajava BirdDisplay Sparrow Blue Jay
- Bjava BirdDisplay Sparrow "Blue Jay"
- Cjava BirdDisplay Blue Jay Sparrow
- Djava BirdDisplay "Blue Jay" Sparrow
- Ejava BirdDisplay.class Sparrow "Blue Jay"
- Fjava BirdDisplay.class "Blue Jay" Sparrow
- GDoes not compile.
9Which of the following legally fill in the blank so you can run the main() method from the
command line? (Choose all that apply)
public static void main(
)- AString[] _names
- BString[] 123
- CString abc[]
- DString _Names[]
- EString... $n
- FString names
- GNone of the above.
10Which of the following are legal entry point methods that can be run from the command
line? (Choose all that apply)- Aprivate static void main(String[] args)
- Bpublic static final main(String[] args)
- Cpublic void main(String[] args)
- Dpublic static void test(String[] args)
- Epublic static void main(String[] args)
- Fpublic static main(String[] args)
- GNone of the above.
11Which of the following are true? (Choose all that apply)
- AAn instance variable of type double defaults to null.
- BAn instance variable of type int defaults to null.
- CAn instance variable of type String defaults to null.
- DAn instance variable of type double defaults to 0.0.
- EAn instance variable of type int defaults to 0.0.
- FAn instance variable of type String defaults to 0.0.
- GNone of the above.
12Which of the following are true? (Choose all that apply)
- AA local variable of type boolean defaults to null.
- BA local variable of type float defaults to 0.
- CA local variable of type Object defaults to null.
- DA local variable of type boolean defaults to false.
- EA local variable of type boolean defaults to true.
- FA local variable of type float defaults to 0.0.
- GNone of the above.
13Which of the following are true? (Choose all that apply)
- AAn instance variable of type boolean defaults to false.
- BAn instance variable of type boolean defaults to true.
- CAn instance variable of type boolean defaults to null.
- DAn instance variable of type int defaults to 0.
- EAn instance variable of type int defaults to 0.0.
- FAn instance variable of type int defaults to null.
- GNone of the above.
14Given 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)- Apackage my.directory.named.a;
- Bpackage my.directory.named.A;
- Cpackage named.a;
- Dpackage named.A;
- Epackage a;
- Fpackage A;
- GDoes not compile.
15Which of the following lines of code compile? (Choose all that apply)
- Aint i1 = 1_234;
- Bdouble d1 = 1_234_.0;
- Cdouble d2 = 1_234._0;
- Ddouble d3 = 1_234.0_;
- Edouble d4 = 1_234.0;
- FNone of the above.
16Given the following class, which of the following lines of code can replace INSERT CODE
HERE to make the code compile? (Choose all that apply)
public class Price {
public void admission() {
INSERT CODE HERE
System.out.println(amount);
} }- Aint amount = 9L;
- Bint amount = 0b101;
- Cint amount = 0xE;
- Ddouble amount = 0xE;
- Edouble amount = 1_2_.0_0;
- Fint amount = 1_2_;
- GNone of the above.
17Which of the following are true? (Choose all that apply)
public class Bunny {
public static void main(String[] args) {
Bunny bun = new Bunny();
} }- ABunny is a class.
- Bbun is a class.
- Cmain is a class.
- DBunny is a reference to an object.
- Ebun is a reference to an object.
- Fmain is a reference to an object.
- GNone of the above.
18Which represent the order in which the following statements can be assembled into a pro-
gram that will compile successfully? (Choose all that apply)
A: class Rabbit {}
B: import java.util.*;
C: package animals;- AA, B, C
- BB, C, A
- CC, B, A
- DB, A
- EC, A
- FA, C
- GA, B
19Suppose 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: } }- AThe Rabbit object from line 3 is first eligible for garbage collection immediately
following line 6. - BThe Rabbit object from line 3 is first eligible for garbage collection immediately
following line 8. - CThe Rabbit object from line 3 is first eligible for garbage collection immediately
following line 12. - DThe Rabbit object from line 4 is first eligible for garbage collection immediately
following line 9. - EThe Rabbit object from line 4 is first eligible for garbage collection immediately
following line 11. - FThe Rabbit object from line 4 is first eligible for garbage collection immediately
following line 12.
- AThe Rabbit object from line 3 is first eligible for garbage collection immediately
20What is true about the following code? (Choose all that apply)
public class Bear {
protected void finalize() {
System.out.println("Roar!");
}
public static void main(String[] args) {
Bear bear = new Bear();
bear = null;
System.gc();
} }- Afinalize() is guaranteed to be called.
- Bfinalize() might or might not be called
- Cfinalize() is guaranteed not to be called.
- DGarbage collection is guaranteed to run.
- EGarbage collection might or might not run.
- FGarbage collection is guaranteed not to run.
- GThe code does not compile.
21What 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: } }- A0
- B4
- CCompilation fails on line 3.
- DCompilation fails on line 4.
- ECompilation fails on line 7.
- FCompilation fails on line 8.
22Which of the following are true statements? (Choose all that apply)
- AJava allows operator overloading.
- BJava code compiled on Windows can run on Linux.
- CJava has pointers to specific locations in memory.
- DJava is a procedural language.
- EJava is an object-oriented language.
- FJava is a functional programming language.
23Which of the following are true? (Choose all that apply)
- Ajavac compiles a .class file into a .java file.
- Bjavac compiles a .java file into a .bytecode file.
- Cjavac compiles a .java file into a .class file.
- DJava takes the name of the class as a parameter.
- EJava takes the name of the .bytecode file as a parameter.
- FJava takes the name of the .class file as a parameter.
Information
- Description
- OCA JAVA 8 (OCA) Quiz on Chapter 1 Java Building Blocks, created by Fausto Trindade on 04/05/2020.
- Author
- Fausto Trindade
- Language
- Português
- Views
- 12
- Tags
- Rating
- No ratings yet Rate this quiz