Chapter 6

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

33: Which of the following statements is true? A: An instance method is allowed to reference a static variable. B: A static method is allowed to reference an instance variable. C: A static initialization block is allowed to reference an instance variable. D: A final static variable may be set in a constructor.

A: An instance method is allowed to reference a static variable.

25: Which of the following is a valid method name is Java? A: Go_$Outside$2() B: have-Fun() C: new() D: 9enjoyTheWeather()

A: Go_$Outside$2()

29: Which of the following is a valid JavaBean method prefix? A: is B: gimme C: request D: put

A: is get, set, and is.

09: Fill in the blank: A ______________ variable is always available to all instances of the class. A: public B: local C: static D: instance

C: static

08: Which of the following can fill in the blank to make the class compile? package ai; public class Robot { ______ compute() { return 10; } } A: Public int B: Long C: void D: private String

B: Long

05: Fill in the blanks: Java uses ________ to send data into a method. A: pass-by-null B: pass-by-value C: both pass-by-value and pass-by-reference D: pass-by-reference

B: pass-by-value Pass-by-value: java uses it to Copy primitives and references of objects into a method.

16: Given the class below, which method signature could be successfully added to the class as an overloaded version of the findAverage() method? public class Calculations { public Integer findAverage(int sum) { return sum; } } A: public Long findAverage(int sum); B: public Long findAverage(int sum, int divisor); C: public Integer average(int sum); D: private void findAverage(int sum);

B: public Long findAverage(int sum, int divisor);

34: Given the following method declaration, which line can be inserted to make the code compile? public short calculateDistance(double lat1, double lon1, double lat2, double lon2,){ // INSERT CODE HERE } A: return new Integer(3); B: return new Byte((byte)6); C: return 5L; D: return new Short(4).longValue();

B: return new Byte((byte)6); Option D also does not compile because the Short constructor requires an explicit cast to convert the value of 4. new Short((short)4).

40: Given the following class, which line of code when inserted below would prevent the class from compiling? public class Drink { public static void water(){} public void get() { //INSERT CODE HERE } } A: water(); B: this.Drink.water(); C: this.water(); D: Drink.water();

B: this.Drink.water();

35: Which of the following statements about overloaded methods are true? I: Overloaded methods must have the same name. II: Overloaded methods must have the same return type. III: Overloaded methods must have a different list of parameters. A: I B: I and II C: I and III D: I, II, and III

C: I and III

13: Which statement about a no-argument constructor is true? A: The Java compiler will always insert a default no-argument constructor if you do not define a no-argument constructor in your class. B: In order for a class to call super() in one of its constructors, its parent class must explicitly implement a no-argument constructor. C: If a class extends another class that has only one constructor that takes a value, then the child must explicitly declare at least one constructor. D: A class may contain more than one no-argument constructor.

C: If a class extends another class that has only one constructor that takes a value, then the child must explicitly declare at least one constructor. Java only inserts a no-argument constructor if there are no other constructors in the class.

36: How many lines of code would need to be removed for the following class to be compiled ? package work; public class Week { private static final String monday; String tuesday; final static wednesday = 3; final protected int thursday = 3; } A: One B: Two C: Three D: The code will not compile regardless of the number of lines removed.

C: Three

41: Given the following method declaration signature, which of the following is a valid call of of this method? public void call(int count, String me, String... data) A: call(9,"me", 10, "Al") B: call(5) C: call(2,"home","sweet") D: call("answering","service")

C: call(2,"home","sweet")

27: Which of the following is a true statement about passing data to a method? A: A change made to a primitive value passed to a method is reflected in the calling method. B: A change made to data within an object passed to a method is reflected in the calling method. C: Reassigning an object reference passed to a method is reflected in the calling method. D: A change made to the boolean value passed to a method is reflected in the calling method.

B: A change made to data within an object passed to a method is reflected in the calling method. Pass-by-value : changes made to primitive values and object references passed to a method are NOT reflected in the calling method. Changes to the data within an object are visible to the calling method since the object that the copied reference points to is the same.

20: Given a method with one of the following return types, which data type prevents the return statement from being used within the method? A: byte B: String C: void D: None of the above

D: None of the above A method with a void return type can still call the return command with no values and exit the method.

15: Diagram here. A: B: C: D:

Diagram here.

47:What is the output of the following application? package sports ; public class Football { public static Long getScore(Long timeRemaining){ return 2*timeRemaining; //m1 } public static void main(String[] refs) { System.out.print(getScore(startTime)); //m2 } } A: 8 B: It does not compile because of line m1. C: It does not compile because of line m2. D: The code does compile, but throws an exception at runtime.

C: It does not compile because of line m2.

28: What is the possible output of the following application? package wrap; public class Gift { public final Object contents; protected Object getContents(){ return contents } protected void setContents(Object getContents){ this.contents = contents; } public void showPresent(){ System.out.print("Your gift: "+contents); } public static void main(String[] args) { Gift gift = new Gift(); gift.setContents(gift); gift.showPresent(); } } A: Your gift: wrap.Gift@29ca2745 B: Your gift: Your gift: C: The code does not compile. D: The code does compile, but throws an exception at runtime.

C: The code does not compile. final modifier on the contents instance variable.

48: Which of the following is a valid method name in Java? A: $sprint() B: \jog13() C: walk#() D: %run()

A: $sprint() Java methods must start with a letter, the dollar $ symbol, or underscore _ character. The hashtag (#) symbol cannot be included in a method name.

39: What is the output of the following application? package ship; public class Phone { private int size; public Phone(int Size){ this.size=size} ; public static void sendHome(Phone p, int newSize) { p = new Phone(newSize); p.size = 4; } public static final void main(String... params) { final Phone phone = new Phone(3); sendHome(phone,7); System.out.print(phone.size); } } A: 3 B: 4 C: 7 D: The code does not compile.

A: 3

46: What is the output of the following application? public class chooseWisely { public chooseWisely(){ super(); } public int choose(int choice) {return 5;} public int choose(short choice) {return 2;} public int choose(long choice) {return 11;} public static void main(String[] path) { System.out.print(new ChooseWisely().choose(byte)2+1); } } A: 5 B: 2 C: 11 D: The code does not compile.

A: 5 The addition + operator automatically promotes all byte and short values to int.

18: Which of the following data types can be modified after they are passed to a method as an argument ? A: int[] B: String C: long D: boolean

A: int[] Strings are immutable, and cannot be modified. Variables are passed by value and not by reference. The contents of an array can be modified when passed to a method, since only a copy of the array is passed.

10: Which line of code, inserted at line p1, causes the application to print 5? package games; public class Jump { private int rope = 1; protected boolean outside; public Jump(){ //p1 outside = true; } public Jump(int rope){ this.rope = outside ? rope : rope+1; } public static void main(String[] home) { System.out.print(new Jump().rope); } } A: this(4); B: new Jump(4); C: this(5); D: rope = 4;

A: this(4); this() - is the command to call one constructor from another constructor in the same class

11: Which of the following statements is not true? A: An instance of one class may access an instance of another class's attributes if it has a reference to the instance and the attributes are declared public. B: An instance of one class may access private package attributes in a parent class, provided the parent class is not in the same package. C: Two instances of the same class may access each others private attributes. D: An instance of one class may access an instance of another class's attributes if both classes are located in the same package and marked protected.

B: An instance of one class may access private package attributes in a parent class, provided the parent class is not in the same package.

49: Assume there is a class Bouncer with protected variable. Methods in which class can access this variable. A: Only subclasses of Bouncer. B: Any subclass of Bounder or any class in the same package as Bouncer. C: Only classes in the same package as Bouncer D: Any subclass of Bouncer

B: Any subclass of Bounder or any class in the same package as Bouncer.

23: Given the following method signatures, which classes can call it ? void run(String government) A: Classes in other packages B: Classes in the same package C: Subclasses in a different package D: Subclasses in a different package

B: Classes in the same package private same class package private Classes in the same package protected Subclasses in a different package public Subclasses in a different package

07: Which of the following statements about calling this() in a constructor is not true? A: If this() is used, it must be the first line of the constructor. B: If super() and this() are both used in the same constructor, super() must appear on the line immediately after this(). C: If arguments are provided to this(), the there must be a constructor in the class able to take those arguments D: If the no-argument this() is called, then the class must explicitly implement the no-argument constructor.

B: If super() and this() are both used in the same constructor, super() must appear on the line immediately after this().

19: What is the best way to call the following method from another class in the same package, assuming the class using the method does not have any static imports? package useful; public class Mathhelper { public static int roundValue(double d){ //implementation method } } A: MathHelper:roundValue(5.92) B: MathHelper.roundValue(3.1) C: roundValue(4.1) D: useful.MathHelper:roundValue(65.3)

B: MathHelper.roundValue(3.1)

02: What is the command to call one constructor from another constructor in the same class? A: super() B: this() C: that() D: construct()

B: this() super() - is the command to call a constructor in the parent class this() - is the command to call one constructor from another constructor in the same class that() construct()

42: Which statement about a static variable is true? A: The value of a static variable must be set when the variable is declared or in a static initialization block. B: It is not possible to read static final variable to outside the class in which they are defined. C: It is not possible to reference static methods using static imports. D: A static variable is always available in all instances of the class.

D: A static variable is always available in all instances of the class.

17:Which of the following is 'NOT' a reason to use encapsulation when designing a class? A: Promote usability by other developers. B: Maintain class data integrity of data elements. C: Prevent users from modifying the internal attributes of a class. D: Increase concurrency and improve performance.

D: Increase concurrency and improve performance. Encapsulation makes no guarantees about concurrency and performance.

31: What access modifier is used to mark class members package-private? A: private B: default C: protected D: None of the above.

D: None of the above.

45: What is the output of the following application? package jungle ; public class RainForest extends Forest { public RainForest(long treeCount){ this.treeCount = treeCount+1; } public static void main(String[] birds) { System.out.print(new RainForest(5).treeCount); } } class Forest { public long treeCount; public RainForest(long treeCount) { this.treeCount = treeCount+2; } } A: 5 B: 6 C: 8 D: The code does not compile.

D: The code does not compile.

44: How many final modifiers would need to be removed for this application to compile? package park; public class Tree { public final static long numberOfTrees; public final double height; static {} { final int initHeight = 2; height = initHeight; } static { numberOfTrees = 100; height = 4; } A: None B: One C: Two D: The code will not compile regardless of the number of final modifiers removed.

D: The code will not compile regardless of the number of final modifiers removed. The last static initialization block accesses height, which is an instance variable, not a static variable.

32: How many lines of the following program contain compilation errors ? package sky; public class Stars { private int inThe = 4; public void Stars(){ super(); } public Stars(int inThe){ this.inThe = this.inThe; } public static void main(String[] endless) { System.out.print(new sky.Stars(2).inThe); } } A: None B: One C: Two D: Three

D: Three

22: Fill in the blanks: ____________ is used to call a construstor in the parent class, while _______________ is used to reference a member of the parent class. A: super() and this() B: super() and super() C: super() and this D: super() and super

D: super() and super super() - call a constructor in the parent class super - reference a member of the parent class this() - call a constructor in the current class this - reference a member of the current class

26: Which of the following lines of code can be inserted in the line below that would allow the class to compile? package farm ; public class Coop { public final int static getNumberOfChickens(){ // Insert code here } A: return 3.0; B: return 5L; C: return 10; D:None of the above.

D:None of the above. Incorrect method signature, int is in the wrong place.

24: Which statement(s) about the following class would help to properly encapsulate the data in the class? package shield; public class protect { private String Material; protected int strength; public int getStrength(){ return strength; } public void setStrength(int strength) { this.strength = strength; } } I: Change the access modifier of strength to private. II: Add a getter method for material. III: Add a setter method for material. A: I B: II and III C: I, II and III D: None, the data in the class is already encapsulated.

A: I Not required to do Material.

43: Which of the following is not a true statement? A: The first line of every constructor is a call to the parent constructor via super() command. B: A class does not have to have a constructor explicitly defined. C: A constructor may pass arguments to the parent constructor. D: A final instance variable whose value is not set when they are declared or in an initialization block should be set by the constructor.

A: The first line of every constructor is a call to the parent constructor via super() command. The first line of a constructor could be this() or super().

14: Which of the following method signatures does not contain a compiler error ? A: public void sing(String key, String... harmonies) B: public void sing(int note, String... sound, int music) C: public void sing(String... keys, String... pitches) D: public void sing(String... notes, String melodies)

A: public void sing(String key, String... harmonies) A method signature can only contain at most one varsargs parameter and it must be the last in the list.

38:Fill in the blanks: The ___________ access modifiers allows access to everything the ____________ access modifiers does and more. A: public, private B: private, package-private C: package-private, protected D: private, public

A: public, private

21: How many final modifiers would need to be removed for this application to compile? package end; public final class Games { public final static int finish(final int score){ final int win = 3; final int result = score++ < 5 ? 2 : win; return result+=win; } public static void main(final String[] v) { System.out.print(finish(Integer.parseInt(v[0])); } } A: None B: One C: Two D: The code does not compile regardless of the number of final modifiers that are removed.

C: Two The score variable is modified by the post-increment ++ operator, while the result variable is modified by the compound addition += operator. Removing both final modifiers allows the code to compile.

30: Given the following two classes, each in a different package, which line inserted below allows the second class to compile? package ; public class Store { public static String getClothes(){ return "dress";} } package wardrobe; //INSERT CODE HERE public class Closer { public void borrow(){ System.out.print("Borrowing clothes: "+getClothes()); } } A: static import clothes.Store.getClothes; B: import clothes.Store.*; C: import static clothes.Store.getClothes; D: import static clothes.Store.*;;

C: import static clothes.Store.getClothes; The closet class uses the method getClothes without a reference to the class name Store, therefore a static import is required.

01: Fill in the blanks: The ______________ access modifier allows access to everything the __________ access modifier does and more. A: private-package, protected B: protected, public C: protected, package-private D: private, package-private

C: protected, package-private public - (access to protected )any package protected - (access to default) same class or same package or subclass default private-package - (access to private) same class or same package private - same class

06: Which of the following is a valid JavaBean method signature. A: public void getArrow() B: public void setBow() C: public void setRange(int range) D: public String addTarget(String target)

C: public void setRange() public void getArrow() - should return a value public void setBow() - should accept a value public void setRange(int range) - takes value returns nothing public String addTarget(String target) - ERROR

37: What is the output of the following application ? package pet; public class Puppy { public static int wag = 5; //q1 public void Puppy(int wag){ //q2 this.wag = wag; } public static void main(String[] tail) { System.out.print(new Puppy(2).wag); //q3 } } A: 2 B: It does not compile because line q1. C: It does not compile because line q2. D: It does not compile because line q3.

D: It does not compile because line q3.

04: What is true about the following program? package figures; public class Dolls { public void nested(){ nested(2,true); } //g1 public int nested(int level, boolean height){ return nested(level); } public int nested(int level){ return level + 1; } //g2 public static void main(String[] outOfTheBox) { System.out.print(new Dolls().nested()); } } A: It compiles successfully and prints 3 at runtime. B: It does not compile because of line g1. C: It does not compile because of line g2. D: It does not compile for some other reason.

D: It does not compile for some other reason. Method overloading works fine. An exception is thrown on print when void is defined as the return type from the no argument return type.

12: Given the following class, what should be inserted into the two blanks to ensure the class data is properly encapsulated? package storage; public class Box { public String stuff; _______ String _______() { return stuff; } public void setStuff(String stuff) { this.stuff = stuff; } } A: public and getStuff B: private and isStuff C: public and setStuff D: None of the above

D: None of the above.

50:Given the following two classes, each in a different package, which line inserted below allows the second class to compile? package commerce ; public class Bank { public void withdrawal(int amountInCents) {} public void deposit(int amountInCents) {} public static String getClothes(){ return "dress";} } package employee; //INSERT CODE HERE public class Teller { public void processAccount(int depositSlip, int withdrawalSlip) { withdrawal(withdrawalSlip); depositSlip(depositSlip); } } A: import static commerce.Bank.*; B: static import commerce.Bank.*; C: import static commerce.Bank; D: None of the above.

D: None of the above. A static import is used to import static members of another class. In this case, the withdrawal() and deposit() methods in the Bank class are not marked static. They require an instance of Bank to be used and cannot be imported as static methods.

03:What is the output of the following application? package stocks; public class Bond { private static int price = 5; public boolean sell(){ if(price<10) { price++; return true; } else if(price>=10) { return false; } } public static void main(String[] cash) { new Bond().sell(); new Bond().sell(); new Bond().sell(); System.out.print(price); } } A: 5 B: 6 C: 7 D: The code does not compile.

D: The code does not compile.


Kaugnay na mga set ng pag-aaral

ALS Distance Learning Set B Volume 1, Volume 2

View Set

Introduction to Economics Test# 2 Chapter 11 Perfect Competition

View Set

casse-tete: nouritures et boissons

View Set

Dental Assisting Section 2 Terminology & Anatomy

View Set

Evolve - Med Surg - Cardio, Chapter 69: Management of Patients with Autoimmune disorders RV, Pharmacology (Hesi)

View Set