Chapter 1 Java Basics

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

41. Given a class that uses the following import statements, which class would not be automatically accessible within the class without using its full package name. import dog.puppy.*; import dog.*; A: dog.puppy.female.KC B: dog.puppy.Georgette C: dog.Webby D: java.lang.object

A: dog.puppy.female.KC

25. Which of the following variables are always in scope for the entire program? A: Package variables B: Class variables C: Instance variables D: Local variables

B: Class variables

31. Which of the following statements about Java is true? A: Java is a procedural programming language. B: Java allows method overloading. C: Java allows operator overloading. D: Java allows direct access to objects in memory.

B: Java allows method overloading.

42. _____________ is the technique of structuring programming data as a unit consisting of attributes, with actions defined on the unit. A: Encapsulation B: Object orientation C: Platform Independence D: Polymorphism

B: Object orientation Polymorphism is the ability of an object to take on many forms. Polymorphism allows us to perform a single action in different ways. Polymorphism allows you to define one interface and have multiple implementations. In Java polymorphism is mainly divided into two types: Compile time Polymorphism > static (This type of polymorphism is achieved by function (method) overloading or operator overloading.) Runtime Polymorphism Operator Overloading: Java also provide option to overload operators. For example, we can make the operator ('+') for string class to concatenate two strings. We know that this is the addition operator whose task is to add two operands. So a single operator '+' when placed between integer operands, adds them and when placed between string operands, concatenates them. In java, Only "+" operator can be overloaded: To add integers To concatenate strings

11. Which statement about import statements is true ? A: The class will not compile if it contains unused import statements. B: Unused import statements can be removed from the class without causing a class to become unable to be compiled. C: The class will not compile if a duplicate import statement is not present. D: If a class contains an import statement for a class used in the program that cannot be found, it can still compile.

B: Unused import statements can be removed from the class without causing a class to become unable to be compiled.

37. Given the file Magnet.java below, which of the marked lines can you independently insert the line public String color; into and still have the code compile? // line a1 public class Magnet { // line a2 public void attach() { // line a3 } // line a4 } A: a1 and a3 B: a2 and a4 C: a2, a3, and a4 D: a1, a2, a3, and a4

B: a2 and a4 public String color; Cannot be inserted at a1 - variables are not allowed outside the class declaration. public String color; Cannot be inserted at a3 - local variables defined within methods cannot have access modifiers such as public. Instance variables can be defined anywhere in the class outside of a method.

50. Which statement about the JVM is true ? A: The JVM schedules garbage collection on a predictable schedule. B: The JVM ensures that the application will always terminate. C: The JVM requires a properly defined entry point method to execute the application. D: A Java compiled code can be run on any computer.

C: The JVM requires a properly defined entry point method to execute the application.

48. What is the result of compiling and executing the following application? package forecast; public class Weather { private static boolean heatWave = true; public static void main() { boolean heatWave = false; System.out.print(heatWave); } } A: true B: false C: It does not compile. D: It does compile but throws an exception. (error)

D: It does compile but throws an exception (error not exception).

18. What is the output of the following code snippet? String tree = "pine"; int count = 0; if (tree.equals("pine")) { int height = 55; count = count + 1; } System.out.print(height + count); A: 1 B: 55 C: 56 D: It does not compile.

D: It does not compile. Height is defined in a conditional statement and cannot be accessed outside of it.

9. Which statement about a valid .java file is true? A: It can only contain one class declaration. B: It can contain one public class declaration and one public interface definition. C: It must define at least one public class. D: It must define at most one public class.

D: It may define at most one public class.

13. Which statements about Java are true ? I. The java command can execute .java and .class files. II. Java is not object oriented. III. The javac command compiles directly into native machine code. A: I only. B: III only. C: II and III. D: None are true.

D: None are true. Bytecode is processed in the JVM.

23. Which of the following features allows a Java class to be run on a wide variety of computers and devices? A: Encapsulation B: Object Oriented C: Inheritance D: Platform independence

D: Platform independence

28. Given the following class definition, what is the maximum number of import statements that can be discarded and still have the code compile? For this question, assume that the Blackhole class is defined only in the stars package. package planetarium; import java.lang.*; import stars.*; import java.lang.Object; import stars.Blackhole; public class Observer { public void find(Blackhole blackhole) {} } A: Zero B: One C: Two D: Three

D: Three

14. Which of the following lines of code is not allowed as the first line of a Java class file? A: import widget.*; B: // Widget Manager C: package sprockets D: int facilityNumber;

D: int facilityNumber;

1: D 2: A 3: C 4: B 5: A 6: D 7: B 8: C 9: D 10: B 11: B 12: A 13: D 14: D 15: C 16: B 17: D 18: D 19: A 20: D 21: C 22: C 23: D 24: A 25: B 26: C 27: D 28: D 29: C 30: B 31: B 32: D 33: A 34: D 35: A 36: B 37: B 38: A 39: D 40: C 41: A 42: B 43: A 44: C 45: D 46: B 47: C 48: D 49: C 50: C

1: D 2: A 3: C 4: B 5: A 6: D 7: B 8: C 9: D 10: B 11: B 12: A 13: D 14: D 15: C 16: B 17: D 18: D 19: A 20: D 21: C 22: C 23: D 24: A 25: B 26: C 27: D 28: D 29: C 30: B 31: B 32: D 33: A 34: D 35: A 36: B 37: B 38: A 39: D 40: C 41: A 42: B 43: A 44: C 45: D 46: B 47: C 48: D 49: C 50: C

35. Which of the following is a valid code comment in Java? A: //////// Walk my dog B: #! Go team! C: / Process fails at runtime / D: None of the above

A: //////// Walk my dog

43. Given the following class definition, what is the maximum number of import statements that can be discarded and still have the code compile ? For this question, assume that the Broccoli class is in the food.vegetables package, and the Apple class is the food.fruit package. package food; import food.vegetables.*; import food.fruit.*; import java.util.Date; public class Grocery { Apple a; Broccoli b; Date c; } A: 0 B: 1 C: 2 D: 3

A: 0

33. What is the output of the following application? public class Airplane { static int start = 2; final int end; public Airplane(int x) { x = 4; end = x; } public void fly(int distance) { System.out.print(end-start+" "); System.out.print(distance); } public static void main(String... start) { new Airplane(10).fly(5): } } A: 2 5 B: 8 5 C: 6 5 D: The code does not compile.

A: 2 5

38. What is required to define a valid Java class file? A: A class declaration B: A package statement C: At least one import statement D: The public modifier

A: A class declaration

19. Which of the following is true of a Java bytecode file? A: It can be run on any computer with a compatible JVM. B: It can only be executed on the same type of computer that it was created on. C: It can be easily read and modified in a standard text editor ? D: It requires the corresponding .java that created it to execute.

A: It can be run on any computer with a compatible JVM.

12. What is the result of compiling and executing the following class? 1: public class ParkRanger { 2: int birds = 10; 3: public static void main(String[] data) { 4: int trees = 5; 5: System.out.print(trees + birds) ; 6: } 7: } A: It does not compile. B: It compiles but throws an exception at runtime. C: It compiles and outputs 5. D: It compiles and outputs 15.

A: It does not compile. The main method is static and by itself does not have access to any class instance variable. To access the instance variable (non-static) the static method would need to create an instance of the class and then use dot notation to point to the instance variable.

24. Which of the following is "NOT" a property of a JVM ? A: It prevents Java bytecode from being easily decoded/decompiled. B: It supports platform independence. C: It manages memory for the application. D: It translates Java instruction to machine instructions.

A: It prevents Java bytecode from being easily decoded/decompiled.

5. Which of the following is not a facet of traditional object-oriented programming languages ? A: Objects are grouped as procedures, separate from the data they act on. B: An object can take many forms via casting. C: An object can hold data, referred to as attributes. D: An object can perform actions, via methods.

A: Objects are grouped as procedures, separate from the data they act on.

8. Which of the following is not a valid code comment in Java ? A: // Add 5 to the result B: /*** TODO: Fix bug 12312 ***/ C: # Add configuration value D: /* Read file from system ****/

C: # Add configuration value

40. Given that a Math class exists in both the java.lang and pocket.complex packages, what is the result of compiling the following class? 1: package pocket; 2: import pocket.complex.*; 3: import java.util.*; 4: public class Calculator { 5: public static void main(String[] args) { 6: System.out.print(Math.floor(5)); 7: } 8: } A: The code does not compile because of line 2. B: The code does not compile because of line 3. C: The code does not compile because of line 6. D: The code compiles without issue.

C: The code does not compile because of line 6.

29. Given the following class definition, which command will cause the application to output the message White-tailed? package forest; public class Deer { public static void main(String... deerParams) { System.out.println(deerParams[2]); } } A: java forest.Deer deer 5 "White-tailed deer" B: java forest.Deer "White-tailed deer" deer 3 C: java forest.Deer Red deer White-tailed deer D: java forest.Deer My "deer White-tailed deer"

C: java forest.Deer Red deer White-tailed deer

26. Given the following wildcard import statements, which class would be included in the import? import television.actor.*; import movie.director.*; A: television.actor.recurring.Marie B: movie.directors.John C: television.actor.Package D: movie.NewRelease

C: television.actor.Package

39. What is the proper filename extension for a Java source file? A: .jav B: .class C: .source D: .java

D: .java

4. Given that a Date class exists in java.util and java.sql packages, what is the result of compiling the following class? 1: import java.util; 2: import java.sql; 3: public class BirthdayManager { 4: private Date rob = new Date(); 5: private java.util.Date sharon = new java.util.Date(); 6:} A: The code does not compile because of lines 1 and 2. B: The code does not compile because of line 4. C: The code does not compile because of line 5. D: The code compiles without issue.

B: The code does not compile because of line 4.

30. Which of the following is a true statement? A: The java command compiles a .java file into a .class file. B: The javac command compiles a .java file into a .class file. C: The java command compiles a .class file into a .java file. D: The javac command compiles a .class file into a .java file.

B: The javac command compiles a .java file into a .class file.

7. Which package is imported into every Java class by default ? A: java.util B: java.lang C: system.lang D: java.system

B: java.lang

16. Given that the current directory is /user/home, with an application Java file in /user/home/Manager.java that uses the default package, which are the correct commands to compile and run the application in Java? A: javac Manager java Manager B: javac Manager.java java Manager C: javac Manager java Manager.class D: javac Manager.java java Manager.class

B: javac Manager.java java Manager

36. Which of the following method signatures is not a valid declaration of an entry point in a Java application. A: public static void main(String... arguments) B: public static void main(String arguments) C: public static final void main(String[] arguments) D: public static void main(String[] arguments)

B: public static void main(String arguments)

46. What is the result of compiling and executing the following class ? package sports; public class Bicycle { String color = "red"; public void printColor(String color) { color = "purple"; System.out.print(color); } public static void main(String[] rider) { new Bicycle().printColor("blue"); } } A: red B: purple C: blue D: It does not compile.

B: purple

21. What is the result of compiling and executing the following class? 1: public class Tolls{ 2: private static int yesterday = 1; 3: int tomorrow = 10; 4: public static void main(String[] args) { 5: Tolls tolls = new Tolls(); 6: int today=20, tomorrow = 40; 7: System.out.print(today + tolls.tomorrow + tolls.yesterday); 8: } 9: } A: The code does not compile due to line 6. B: The code does not compile due to line 7. C: 31 D: 61

C: 31 While using an instance reference to access a static variable is not recommended, it does not prevent the variable from being read.

47. Which statements about calling the compilation command javac and the execution command java are true ? I: java may use a period . to separate packages. II: javac takes a . java file and returns a . class file. III: java may use a slash (/) to separate packages. A: I only B: II only C: I and II D: I, II, and III

C: I and II

44. Given the following application, what is the expected output? public class Keyboard { private boolean numlock = True; static boolean caplock = false; public static void main(String... shortcuts) { System.out.print(numlock+" "+capslock); } } A: true true B: false false C: It does not compile. D: It compiles but throws an exception at runtime.

C: It does not compile.

22. Given the following class definition, which is the only line that does not contain a compilation error? 1: public ThisClassDoesNotCompile { 2: double int count; 3: void errors() {} 4: static void private limit; } A: Line 1 B: Line 2 C: Line 3 D: Line 4

C: Line 3

15. Which one of the following statements is true about using packages to organize your code in Java? A: Every class is required to include a package declaration. B: To create a new package, you need to add a package. init file directory. C: Packages allow you to limit access to classes, methods, or data from classes outside the package. D: It is not possible to restrict access to objects and methods within a package.

C: Packages allow you to limit access to classes, methods, or data from classes outside the package.

3. What is the proper filename extension for a Java byte code compiled file ? A: .java B: .bytecode C: .class D: .dll

C: class

45. What is the result of compiling and executing the following class? public class Rollerskates { static int wheels = 1; int tracks = 5; public static void main(String[] arguments) { RollerSkates s = new RollerSkates(); int feet=4, tracks = 15; System.out.println(feet + tracks + s.wheels); } } A: The code does not compile. B: 5 C: 10 D: 20

D: 20

20. What is the correct character for terminating a statement in Java? A: A colon(:) B: An end-of-line character C: A tab character D: A semicolon

D: A semicolon

34. What is one of the most important reasons that Java supports extending classes via inheritance? A: Inheritance requires that a class that extends another class be in the same package. B: The program must spend extra time/resources at runtime jumping through multiple layers of inheritance to determine precise methods and variables. C: Method signature changes in parent classes may break subclasses that use overloaded methods. D: Developers minimize duplicate code in new classes by sharing code in a common parent class.

D: Developers minimize duplicate code in new classes by sharing code in a common parent class.

6. Which variables have a scope limited to a method ? A: Interface variables B: Class variables C: Instance variables D: Local variables

D: Local variables

17. Structuring a Java class such that only methods within the class can access its instance variables is referred to as _______? A: platform independence B: object orientation C: inheritance D: encapsulation

D: encapsulation

27. Which is the correct order of statements for a Java class file? A: import statements, package statements, class declaration B: package statements, class declaration, import statement C: class declaration, import statements, package declaration D: package statement, import statement, class declaration

D: package statement, import statement, class declaration

32. Given the following code, what values inserted in order into the blank lines, allow the code to compile? _______ agent; public ________ Banker { private static _______ getMaxWithdrawal() { return 10; } } A: import, class, null B: import, interface, void C: package, int, int D: package, class, long

D: package, class, long

1. Which of the following method signatures is a valid declaration of an entry point in a Java application ? A: public void main(String[] args) B: public void main() C: private static void start(String[] mydata) D: public static final void main(String[] args)

D: public static final void main(String[] mydata)


Set pelajaran terkait

Chapter 13 - The Spinal Cord and Spinal Nerves: Wiley Questions

View Set

Task 3, Knowledge-based systems /Expert systems, Knowledge Representations

View Set

Foundations of the Restoration (REL 225) Midterm

View Set

4th Grade Abeka - Science Test 7

View Set

CNA Ch.4 L.1- Food and Nutrition

View Set