Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов)....

65
1. Основные понятия (12 вопросов) ====================================================================================== The following code appears in a file named Plant.java . What is the result of compiling this source file? (Select one answer.) public class Plant { // line 1 public boolean flowering; // line 2 public Leaf [] leaves; // line 3 } // line 4 // line 5 class Leaf { // line 6 public String color; // line 7 public int length; // line 8 } // line 9 1. The code compiles successfully and two bytecode files are generated: Plant.class and Leaf.class 2. The code compiles successfully and one bytecode file is generated: Plant.class . 3. A compiler error occurs on line 1. 4. A compiler error occurs on line 3. 5. A compiler error occurs on line 6. _______________________________________________________________ __ 1. The code does not contain any compiler errors. It is valid to define multiple classes in a single file as long as only one of them is public and the others have the default access. 1

Transcript of Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов)....

Page 1: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

1. Основные понятия (12 вопросов)======================================================================================

The following code appears in a file named Plant.java . What is the result of compiling this source file? (Select one answer.)

public class Plant { // line 1 public boolean flowering; // line 2 public Leaf [] leaves; // line 3} // line 4

// line 5class Leaf { // line 6 public String color; // line 7 public int length; // line 8} // line 9

1. The code compiles successfully and two bytecode files are generated: Plant.class and Leaf.class

2. The code compiles successfully and one bytecode file is generated: Plant.class .

3. A compiler error occurs on line 1.4. A compiler error occurs on line 3.5. A compiler error occurs on line 6._________________________________________________________________1. The code does not contain any compiler errors. It is valid to define multiple classes in a single file as long as only one of them is public and the others have the default access.

1

Page 2: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

======================================================================================

Suppose a class named com.mycompany.Main is a Java application, and Main.class is in the following directory:

\projects\build\com\mycompanyWhich of the following commands successfully executes Main ? (Select two answers.)

1. java - classpath=\projects\build com.mycompany.Main2. java - classpath \projects\build\com\mycompany Main3. java - classpath \projects\build com.mycompany.Main4. java - classpath \projects\build\com mycompany.Main5. java - cp \projects\build com.mycompany.Main

________________________________________________________________3 and 5. 3 assigns the -classpath flag to the appropriate directory. 5 also set the class path correctly except -cp is used. The -cp and -classpath flags are identical. 1 uses anequals sign = with the -classpath flag, which is not the correct syntax. 2 and 4 set the class path to the wrong directory and also incorrectly refer to the Main class without its fully qualified name, which is com.mycompany.Main.

2

Page 3: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

======================================================================================

What is the outcome of the following statements? (Select one answer.) String s1 = “Canada”;String s2 = new String(s1);if(s1 == s2) {

System.out.println(“s1 == s2”);}if(s1.equals(s2)) {

System.out.println(“s1.equals(s2)”);}

1. There is no output.2. s1 == s23. s1.equals(s2)4. Both B and C_________________________________________________________________3. The reference s1 points to a String object in the string pool because “Canada” is aliteral string known at compile time. The reference s2 points to a String object created dynamically at runtime, so this object is created on the heap. Therefore 2 is incorrect because s1 and s2 point to different objects. However, 3 is correct because s1 and s2 are both String objects that equal “Canada”, so s1.equals(s2) evaluates to true. Because 3 is correct, 1 and 4 must be incorrect.

3

Page 4: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

======================================================================================

Suppose we have the following class named GC :

import java.util.Date; // 1// 2

public class GC { // 3public static void main(String [] args) { // 4

Date one = new Date(); // 5Date two = new Date(); // 6Date three = one; // 7one = null; // 8Date four = one; // 9three = null; // 10two = null; // 11two = new Date(); // 12

} // 13} // 14

Which of the following statements are true? (Select two answers.)

1. The Date object from line 5 is eligible for garbage collection immediately following line 8.2. The Date object from line 5 is eligible for garbage collection immediately following line 10.3. The Date object from line 5 is eligible for garbage collection immediately following line 13.4. The Date object from line 6 is eligible for garbage collection immediately following line 11.5. The Date object from line 6 is eligible for garbage collection immediately following line 13.

__________________________________________________________________2 and 4. The Date object from line 5 has two references to it — one and three —and becomes eligible for garbage collection after line 10, so 2 is a true statement. The reference four is set to null on line 9, which does not affect the object from line 5. The Date object from line 6 only has a single reference to it — two — and therefore becomes eligible for garbage collection after line 11 when two is set to null, so 4 is a true statement.

4

Page 5: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

======================================================================================

When does the String object instantiated on line 4 become eligible for garbage collection?

public class ReturnDemo { // 1// 2

public static String getName() { // 3String temp = new String(“Jane Doe”); // 4return temp; // 5

} // 6// 7

public static void main(String [] args) { // 8String result; // 9result = getName(); // 10System.out.println(result); // 11result = null; // 12System.gc(); // 13

} // 14} // 15

1. Immediately after line 42. Immediately after line 53. Immediately after line 104. Immediately after line 125. Immediately after line 13_________________________________________________________________4. The object on line 4 is referred to by the temp reference, which goes out of scope after line 5. However, the result reference gets a copy of temp, so it refers to the “Jane Doe” object until line 12 when result is set to null, at which point “Jane Doe” is no longer reachable and becomes immediately eligible for garbage collection. Therefore, the answer is 4.

5

Page 6: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

======================================================================================

What is the output of the following code?

byte a = 40, b = 50; // 4byte sum = (byte) a + b; // 5System.out.println(sum); // 6

1. Line 5 generates a compiler error.2. 403. 504. 905. An undefined value___________________________________________________________________________________________1. Line 5 generates a possible loss of precision compiler error. The cast operator has the highest precedence, so it is evaluated first, casting a to a byte (which is fine). Then the addition is evaluated, causing both a and b to be promoted to ints. The value 90, stored as an int, is assigned to sum, which is a byte. This requires a cast, so the code does not compile and therefore the correct answer is 1. (This code would compile if parentheses were used around (a + b).)

6

Page 7: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

======================================================================================

What is the output of the following code?int x = 5 * 4 % 3; // 5System.out.println(x); // 6

1. Line 5 generates a compiler error.2. 23. 34. 55. 6

_________________________________________________________________2. The * and % operators have the same level or precedence and are therefore evaluated left-to-right. The result of 5 * 4 is 20 and 20 % 3 is 2 (20 divided by 3 is 18; the remainder is 2). Therefore, the answer is 2.

7

Page 8: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

======================================================================================

What is the output of the following code?

byte y = 14 & 9; // 3System.out.println(y); // 4

1. Line 3 generates a compiler error.2. 153. 144. 95. 8

_________________________________________________________________5. To evaluate the & operator, you need to express the numbers in binary and evaluate & on each column, as shown here:14 = 0000 11109 = 0000 100114 & 9 = 0000 1000The resulting binary number 00001000 is 8 in decimal, so the answer is 5.

8

Page 9: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

======================================================================================

Given the following class named House , which of the following statements is true? (Select two answers.)

public class House { // 1public String address = new String(); // 2

// 3public void finalize() { // 4

System.out.println(“Inside House”); // 5address = null; // 6

} // 7} // 8

1. “ Inside House ” is displayed just before a House object is garbage collected.2. “ Inside House ” is displayed twice just before a House object is garbage

collected.3. The finalize method on line 4 never actually gets called.4. There is no need to assign address to null on line 6.5. The String object from line 2 is guaranteed to be garbage collected after its

corresponding House object is garbage collected.

_________________________________________________________________1 and 4. Just before an object is garbage collected, its finalize method is invoked once,so 1 is true but 2 is incorrect. 3 is incorrect because it is just not a true statement. 4 is correct; there is no need to assign address to null because it is about to be deleted from memory. 5 is incorrect, though, because address may not be the only reference to the String object that address refers to.

9

Page 10: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

======================================================================================

What is the output of the following code?

int x = 0; // 3String s = null; // 4if(x == s) { // 5

System.out.println(“Success”); // 6} else { // 7

System.out.println(“Failure”); // 8} // 9

1. Success2. Failure3. Line 4 generates a compiler error.4. Line 5 generates a compiler error.

__________________________________________________________________4. The variable x is an int and s is a reference. These two data types are incomparablebecause neither variable can be converted to the other variable’s type.The compiler error occurs on line 5 when the comparison is attempted, so the answer is 4.

10

Page 11: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

11

Page 12: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

======================================================================================

What is the output of the following code?

int x1 = 50, x2 = 75; // 3boolean b = x1 > = x2; // 4if(b = true) { // 5

System.out.println(“Success”); // 6} else { // 7

System.out.println(“Failure”); // 8} // 9

1. Success2. Failure3. Line 4 generates a compiler error.4. Line 5 generates a compiler error.

_________________________________________________________________1. The code compiles successfully, so 3 and 4 are incorrect. The value of b after line 4 is false. However, the if statement on line 5 contains an assignment, not a comparison. The value of b is assigned to true on line 5, and the assignment operator returns true, so line 6 executes and displays “Success”.

12

Page 13: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

======================================================================================

What is the output of the following code?

int c = 7; // 5int result = 4; // 6result += ++c; // 7System.out.print(result); // 8

1. 82. 113. 124. 155. Line 7 generates a compiler error.

___________________________________________________________________________________________3. The code compiles successfully, so F is incorrect. On line 7, c is incremented to 8 before being used in the expression because it is a pre-increment. The 8 is added to result, which is 4, and the resulting 12 is assigned to result and displayed on line 8. Therefore, the answer is 3.

13

Page 14: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

2. Декларация, инициализация...(14 вопросов)

What is the result of the following code?

public class Shape { // 1private String color; // 2

// 3public Shape(String color) { // 4

System.out.print(“Shape”); // 5this.color = color; // 6

} // 7// 8

public static void main(String [] args) { // 9new Rectangle(); // 10

} // 11} // 12

// 13class Rectangle extends Shape { // 14

public Rectangle() { // 15System.out.print(“Rectangle”); // 16

} // 17} // 18

1. ShapeRectangle2. RectangleShape3. Rectangle4. Line 4 generates a compiler error.5. Line 15 generates a compiler error.

__________________________________________________________________5. If a constructor does not call this or super on its first line of code, the compiler inserts the statement super();, which occurs in the Rectangle class just after line 15. A call to super() in Rectangle invokes a no-argument constructor in Shape, but Shape does not have a no-argument constructor. The compiler error occurs at line 15, so the answer is 5.

14

Page 15: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

=======================================================Given the following class definitions:

public class Parent { // 1public Parent() { // 2

System.out.print(“A”); // 3} // 4

} // 5// 6

class Child extends Parent { // 7public Child(int x) { // 8

System.out.print(“B”); // 9} // 10

// 11public Child() { // 12

this(123); // 13System.out.print(“C”); // 14

} // 15} // 16

what is the output of the following statement?new Child();

1. ABC2. ACB3. AB4. AC5. This code does not compile.

________________________________________________________________1. The statement new Child() invokes the constructor on line 12. The call to this(123)invokes the constructor on line 8, which calls super() implicitly before line 9. The call to super() invokes the constructor on line 3, where A is printed. Control jumps back to line 9 and B is printed. Control jumps back to line 14 and C is printed.

15

Page 16: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

Which of the following identifiers are valid Java identifiers? (Select three.)

1. A, $, B2. hello_World3. transient4. java.lang5. Public

__________________________________________________________________1, 2, and 5. 1 is valid because you can use the dollar sign in identifiers. 2 is valid becausethe underscore is a valid Java character. 3 is not a valid identifier because transient is a Java keyword. 4 is not valid because the dot (.) is not allowed in identifiers. 5 is valid because Java is case sensitive, so Public is not a keyword and therefore a valid identifier.

16

Page 17: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the output of the following program?

public class WaterBottle { // 1private String brand; // 2private boolean empty; // 3

// 4public static void main(String [] args ) { // 5

WaterBottle wb = new WaterBottle(); // 6if(!wb.empty) { // 7

System.out.println(“Brand = “ + wb.brand); // 8} // 9

} // 10} // 11

1. Line 6 generates a compiler error.2. Line 7 generates a compiler error.3. Line 8 generates a compiler error.4. There is no output.5. Brand = null

__________________________________________________________________5. The code compiles fine, so 1, 2, and 3 are incorrect. Boolean fields initialize to false and references initialize to null, so empty is false and brand is null. Therefore, line 7 is true and Brand = null is output. Therefore, 4 is incorrect and the answer is 5.

17

Page 18: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

Given the following class definition:

public class Television { // 1private int channel = setChannel(7); // 2

// 3public Television(int channel) { // 4

this.channel = channel; // 5System.out.print(channel + “ “); // 6

} // 7// 8

public int setChannel(int channel) { // 9this.channel = channel; // 10System.out.print(channel + “ “); // 11return channel; // 12

} // 13} // 14

what is the output of the following statement?new Television(12);

1. 122. 12 73. 7 124. 75. The code does not compile.

________________________________________________________________3. The code compiles fine, so 5 is incorrect. Because explicit initialization occurs before a constructor is invoked, line 2 executes before the Television constructor on line 4 is executed. The 7 is output on line 11, then the constructor is invoked and 12 is output. Therefore, the output is 7 12, so the answer is 3.

18

Page 19: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

Suppose we have the following class named ClassRoom :

package my.school; // 1public class ClassRoom { // 2

public static int globalKey = 54321; // 3} // 4

Now suppose we have the following class named Administrator:

package my.city; // 1// 2

public class Administrator { // 3public int getKey() { // 4

return globalKey; // 5} // 6

} // 7

Which one of the following statements inserted at line 2 of the Administrator classwill make the Administrator class compile successfully?

1. import my.school.ClassRoom;2. import static my.school.ClassRoom.*;3. import static my.school.ClassRoom;4. import static my.school.*;5. Nothing — the class compiles.

_________________________________________________________________2. 5 is incorrect. Without any imports, the Administrator class will not compile because line 5 of Administrator refers to globalKey, a static field in ClassRoom.1 imports the ClassRoom class, which is a valid import but does not import globalKey. 2 imports all static fields of ClassRoom, so 2 is a correct answer. 3 and 4 are not valid statements and generate compiler errors. Therefore, the only correct answer is 2.

19

Page 20: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the output of the following program?

public class ScorePrinter { // 1public static void printScores(int... scores) { // 2

for(int x : scores) { // 3System.out.print(x + “,”); // 4

} // 5} // 6

// 7public static void main(String [] args) { // 8

int [] x = {198, 247, 152, 207}; // 9printScores(x); // 10

} // 11} // 12

1. Compiler error on line 22. Compiler error on line 93. Compiler error on line 104. 198,247,152,2075. 198,247,152,207,

__________________________________________________________________5. The printScores method takes in a variable-length argument on line 2 and it iscorrectly declared, so 1 is incorrect. Line 9 is a valid array initializer statement, so 2 is incorrect. A variable-length parameter is an array behind the scenes and can accept an array argument, so line 10 is valid and 3 is incorrect. The code compiles and the for-each loop displays each number in the array followed by a comma, so 4 is incorrect and 5 is the correct answer.

20

Page 21: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

Given the following class definition:

public class Test { // 1public void print(byte x) { // 2

System.out.print(“byte”) // 3} // 4public void print(int x) { // 5

System.out.print(“int”); // 6} // 7public void print(float x) { // 8

System.out.print(“float”); // 9} // 10public void print(Object x) { // 11

System.out.print(“Object”); // 12} // 13

} // 14

what is the result of the following statements?

Test t = new Test(); // 20short s = 123; // 21t.print(s); // 22t.print(12345L); // 23t.print(6.789); // 24

1. bytefloatObject2. intfloatObject3. byteObjectfloat4. intObjectfloat5. intObjectObject

_________________________________________________________________2. The argument on line 22 is a short. It can be promoted to an int, so print on line 5is invoked. The argument on line 23 is a long. It can be promoted to a float, so print on line 8 is invoked. The argument on line 24 is a double. It can be promoted to a java.lang.Double, so print on line 11 is invoked. Therefore, the output is intfloatObject and the correct answer is 2.

21

Page 22: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

Given the following class definition:

public class Browser { // 1public static void addToFavorites(int id, String... urls) { // 2

for(String url : urls) { // 3System.out.println(url); // 4

} // 5} // 6

} // 7

which of the following statements are valid method calls to addToFavorites ?

1. Browser.addToFavorites(101);2. Browser.addToFavorites();3. Browser.addToFavorites(102, “ a “ );4. Browser.addToFavorites(103, 104, 105);5. Browser.addToFavorites(106, “ x ” , “ y ” , “ z “ );

________________________________________________________________1, 3, and 5. The urls parameter is variable length, so any number of Strings can bepassed in after the int argument. 1 has no Strings, 3 has one String, and 5 has three Strings, so these answers are correct. 2 does not pass in the required int and generates a compiler error. 4 passes in three ints, which also generates a compiler error.

22

Page 23: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

Given the following class definitions:

class Parent { // 1public void printResults(String... results) { // 2

System.out.println(“In Parent”); // 3} // 4

} // 5// 6

class Child extends Parent { // 7public int printResults(int id) { // 8

System.out.println(“In Child”); // 9return 0; // 10

} // 11} // 12

what is the result of the following statement?new Child().printResults(0);

1. In Parent2. In Child3. 04. Line 2 generates a compiler error.5. Line 8 generates a compiler error.

__________________________________________________________________2. The code compiles fine, so 4 and 5 are incorrect. The printResults method in Child is overloading printResults in Parent, not overriding. In method overloading, the return type can be any data type, so printResults in Child returning an int is not a problem. Invoking printResults with an int argument calls the method on line 8, which displays In Child. Therefore, the answer is 2.

23

Page 24: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the result of the following program?

class Parent { // 1public float computePay(double d) { // 2

System.out.println(“In Parent”); // 3return 0.0F; // 4

} // 5} // 6

// 7public class Child extends Parent { // 8

public double computePay(double d) { // 9System.out.println(“In Child”); // 10return 0.0; // 11

} // 12// 13

public static void main(String [] args) { // 14new Child().computePay(0.0); // 15

} // 16} // 17

1. In Parent2. In Child3. 0.04. null5. The code does not compile.

5.The return type of an overridden method must either be the same or a child class of the return type of the parent method. Because double is not a child class of float (they are primitive types), line 8 generates a compiler error. Therefore, the answer is 5.

24

Page 25: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

Given the following class definitions, what is the output of the statement new Child(); ?

class Parent { // 1{ // 2

System.out.print(“1”); // 3} // 4

// 5public Parent(String greeting) { // 6

System.out.print(“2”); // 7} // 8

} // 9// 10

class Child extends Parent { // 11static { // 12

System.out.print(“3”); // 13} // 14

// 15{ // 16

System.out.print(“4”); // 17} // 18

} // 19

1. 12342. 31233. 31424. 31245. The code does not compile.

________________________________________________________________5. The Child class gets the default constructor because it does not define a constructor explicitly. The default constructor contains the line super(); which does not compile because Parent does not have a no-argument constructor. Therefore, the correct answer is 5.

25

Page 26: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

Given the following class definitions:

class Parent { // 1public void print(double d) { // 2

System.out.print(“Parent”); // 3} // 4

} // 5// 6

class Child extends Parent { // 7public void print(int i) { // 8

System.out.print(“Child”); // 9} // 10

} // 11

what is the result of the following code?

Child child = new Child(); // 15child.print(10); // 16child.print(3.14); // 17

1. ChildParent2. ChildChild3. ParentParent4. Line 8 generates a compiler error.5. Line 17 generates a compiler error.

__________________________________________________________________1. The code compiles fine, so 4 and 5 are incorrect. The child class is overloading print, not overriding it. The method call on line 16 invokes print in the child, and the method call on line 17 invokes print in the parent, so the output is ChildParent. Therefore, the answer is 1.

26

Page 27: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

Given the following class definitions:

class Pet { // 1{ // 2

System.out.print(“A”); // 3} // 4public Pet() { // 5

System.out.print(“B”); // 6} // 7 { // 8

System.out.print(“C”); // 9} // 10

// 11} // 12

// 13class Cat extends Pet { // 14

public Cat() { // 15System.out.print(“D”); // 16

} // 17static { // 18

System.out.print(“E”); // 19} // 20

} // 21

what is the result of the following statement?new Cat();

1. ABCDE2. ACBED3. EACBD4. EBACD5. The output may vary.

_________________________________________________________________3. Executing new Cat() means the Cat class must be loaded first by the class loader,which causes its static initializer on line 18 to execute first, displaying E. The Pet instance initializers are next, in the order they appear, so A and C are displayed. Then the Pet constructor is invoked, displaying B, and finally the Cat constructor is invoked, displaying D.The output is EACBD, so the answer is 3.

27

Page 28: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

3. Управляющие конструкции (15 вопросов)

What is the result of the following code?

int x = 10, y = 3; // 3if(x % y == 2) // 4System.out.print(“two”); // 5System.out.print(x%y); // 6if(x%y == 1) // 7System.out.print(“one”); // 8

1. two2. two13. two24. one5. 1one

_________________________________________________________________5. 10%3 equals 1 , so line 4 is false , which results in line 5 being skipped. Line 6 executes and 1 is printed regardless because it is not a part of the if statement. Line 7 is true , so one is also printed. Therefore, the answer is 5.

28

Page 29: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the result of the following code?

int x = 5, y = 10; // 4boolean b = x < 0; // 5if(b = true) { // 6System.out.print(x); // 7} else { // 8System.out.print(y); // 9} // 10

1. Compiler error on line 5.2. Compiler error on line 6.3. 54. 105. The code compiles but there is no output.

_________________________________________________________________3. The code compiles fine, so 1 and 2 are incorrect. In an if - else statement, either the true block or false block executes, so either x or y must be printed, which implies 5 is incorrect. On line 5, the boolean variable b is assigned to false because 5 is not less than 0. Line 6 is an assignment, not a comparison. b is assigned to true on line 6 and the result of the assignment is true , so line 7 executes and a 5 is printed. Therefore, the answer is 3.

29

Page 30: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the output of the following program?

public class Question3 { // 1public static void main(String [] args) { // 2

String year = “Senior”; // 3switch(year) { // 4case “Freshman” : // 5case “Sophomore” : // 6case “Junior” : // 7

System.out.print(“See you next year”); // 8break; // 9

case “Senior” : // 10System.out.print(“Congratulations”); // 11

default : // 12System.out.print(“Invalid year”); // 13

} // 14} // 15

} // 16

1. See you next year2. Congratulations3. CongratulationsInvalid year4. Invalid year5. The code does not compile.

______________________________________________________________5. You cannot switch on a String . Line 4 generates a compiler error, so the correct answer is 5.

30

Page 31: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the result of the following code?

final char a = ‘A’, d = ‘D’; // 4char grade = ‘B’; // 5switch(grade) { // 6case a : // 7case ‘B’ : // 8

System.out.print(“great”); // 9case ‘C’ : // 10

System.out.print(“passed”); // 11break; // 12

case d : // 13case ‘F’ : // 14

System.out.print(“not good”); // 15} // 16

1. great2. greatpassed3. Compiler error on line 44. Compiler error on line 75. Compiler errors on lines 7 and 13

_______________________________________________________________ 2. The code compiles fine, so 3, 4, and 5 are incorrect. The case on line 8 is satisfied, so line 9 executes and great is printed. There is no break, so line 11 executes and passed is printed and therefore 1 is incorrect. The switch breaks at line 12, so the final output is greatpassed and the answer is 2.

31

Page 32: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the result of the following code?

char c = ‘a’;for(int i = 1; i < = 3; i++) {

for(int j = 0; j < = 2; j++) {System.out.print(c++);

}}

1. abcdefghi2. bcdefghij3. abcdef4. abcabcabc5. The code does not compile.

_________________________________________________________________1. The outer loop executes 3 times and the inner loop executes 3 times, so 9 characters are printed starting with ‘ a ‘ , then ‘ b ’ and so on up to ‘ i ’ . Therefore, the output is ‘abcdefghi’ and the answer is 1.

32

Page 33: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the result of the following code?

String [] values = {“one”, “two”, “three”}; // 10for(int index = 0; index < values.length; index++) { // 11

System.out.print(values[index]); // 12} // 13System.out.print(index); // 14

1. onetwothree2. onetwothree23. onetwothree34. onetwothree45. The code does not compile.

__________________________________________________________________5. The int variable index is declared within the for statement, so its scope is only within the for loop. Line 14 generates a compiler error because index is out of scope, so the answer is 5.

33

Page 34: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the output of the following program?

public class Average { // 1public static void main(String [] args) { // 2

int [] scores = {2,4,5,5,6,8}; // 3int sum = 0; // 4for(int x : scores) { // 5

sum += x; // 6} // 7System.out.println(sum / scores.length); // 8

} // 9} // 10

1. 302. 63. 44. 55. The code does not compile.

__________________________________________________________________4The code compiles fine, so 5 is incorrect. Line 3 creates an array of six int s. Theenhanced for loop adds the six int s together, so sum is 2+4+5+5+6+8=30 . scores.length is 6 and 30/6 equals 5, which is printed on line 8. Therefore, the answer is 4.

34

Page 35: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the output of the following code?

int count = 0; // 5rowloop : for(int row = 1; row < = 3; row++) { // 6

for(int col = 1; col < = 2; col++) { // 7if(row * col % 2 == 0) // 8

continue rowloop; // 9count++; // 10

} // 11} // 12System.out.println(count); // 13

1. 12. 23. 34. 45. 6

2. The expression on line 8 is true when row * col is an even number. Let’s step through each iteration:row = 1 and col = 1 : Line 8 is false , the continue is skipped, and count is

incremented to 1 .row = 1 and col = 2 : Line 8 is true , the continue executes, and control jumps to

the next iteration of the outer for loop.row = 2 and col = 1 : Line 8 is true again, so we jump to the next iteration of the

outer loop.row = 3 and col = 1 : Line 8 is false so count gets incremented to 2 .row = 3 and col = 2 : Line 8 is true , the continue executes, and the outer loop is

done.Therefore, the output is 2 and the answer is 2.

35

Page 36: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the result of the following code?

int m = 9, n = 1; // 5int x = 0; // 6while(m > n) { // 7

m--; // 8n += 2; // 9x += m + n; // 10

} // 11System.out.println(x); // 12

1. 112. 133. 234. 365. 50

__________________________________________________________________4. You need to tackle these types of questions by analyzing one iteration through the loop at a time. Let ’ s analyze each step:m = 9 and n = 1 : m > n is true , m is decremented to 8 , n is incremented to 3 , and x is 8 +3 = 11.m = 8 and n = 3 : m > n is true , m is decremented to 7 , n is incremented to 5 , and x is 11 + 7 + 5 = 23.m = 7 and n = 5 : m > n is true , m is decremented to 6 , n is incremented to 7 , and x is 23 + 6 + 7 = 36.m = 6 and n = 7 : m > n is false , so the loop terminates.The final value of x is 36 , so the answer is 4.

36

Page 37: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

Given the following class definition:

public class Forever { // 1public void run() { // 2

while(true) { // 3System.out.println(“Hello”); // 4

} // 5System.out.println(“Goodbye”); // 6

} // 7} // 8

what is output of the following statement?new Forever().run();

1. Prints Hello indefinitely2. Prints Hello until an error occurs3. Prints Hello until an error occurs, then prints Goodbye4. Compiler error on line 35. Compiler error on line 6

________________________________________________________________5 The code does not compile, so 1, 2, and 3 are incorrect. Line 3 is fine — you can declare an infinite while loop. The compiler is aware that line 3 is an infinite loop and that line 6 is an unreachable statement, so the compiler generates an error at line 6. Therefore, the answer is 5.

37

Page 38: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the result of the following code?

int y = 1; // 7do { // 8

System.out.print(y + “ “); // 9} while (y < = 10); // 10

1. The code does not compile.2. 1 2 3 4 5 6 7 8 93. 1 2 3 4 5 6 7 8 9 104. 1 2 3 4 5 6 7 8 9 10 115. ‘ 1 ‘ an infinite number of times

_________________________________________________________________5The loop control variable y equals 1 and does not change in this do - while loop.Because 1 <= 10 is always true, this is an infinite loop and 1 followed by a space displays indefinitely, so the answer is 5.

38

Page 39: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the result of the following code?

do { // 7 int y = 1; // 8System.out.print(y++ + “ “); // 9

} while (y < = 10); // 10

1. The code does not compile.2. 1 2 3 4 5 6 7 8 93. 1 2 3 4 5 6 7 8 9 104. 1 2 3 4 5 6 7 8 9 10 115. ‘ 1 ‘ an infinite number of times.

_________________________________________________________________1. The variable y is declared within the do statement on line 8, so it is out of scope of line 10. Therefore, line 10 generates a compiler error and the answer is 1.

39

Page 40: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the result of the following code?

Boolean keepGoing = true; // 5int result = 1; // 6int i = 10; // 7do { // 8

i--; // 9if(i == 5) { // 10

keepGoing = false; // 11} // 12result < < = 1; // 13

} while (keepGoing); // 14System.out.println(result); // 15

1. 82. 163. 324. 645. Line 14 generates a compiler error.

__________________________________________________________________3. Line 14 compiles fine. A control structure that requires a boolean expression can also use java.lang.Boolean values. The loop control variable i goes from 10 down to 5, then the loop stops executing. Shifting result to the left 1 on line 13 is the equivalent of multiplying by 2, so result takes on the successive values 2 , 4 , 8 , 16, and 32 through the five iterations. Therefore, the answer is 3.

40

Page 41: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the output of the following program?

public class Vowels { // 1public static int countVowels(String input) { // 2

int count = 0; // 3int length = input.length(); // 4int i = 0; // 5

// 6String lowercase = input.toLowerCase(); // 7while(i < length) { // 8

switch(lowercase.charAt(i)) { // 9case ‘a’: // 10case ‘e’: // 11case ‘i’: // 12case ‘o’: // 13case ‘u’: // 14

count++; // 15} // 16i++; // 17

} // 18return count; // 19

} // 20// 21

public static void main(String [] args) { // 22int x = countVowels(“Supercalifragilisticexpialidocious”); // 23System.out.print(x); // 24

} // 25} // 26

1. 02. 163. 344. 355. The code does not compile.

_________________________________________________________________2. The code compiles fine, so 5 is incorrect. The while loop iterates through the String one character at a time and increments count if the character is a vowel. Because the given word has 16 vowels, the output is 16 and the answer is 2.

41

Page 42: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the result of the following code?

int x = 10; // 3if(x < 0) // 4

System.out.print(“anywhere”); // 5else if(x < 5) // 6

if(x == 10) // 7System.out.print(“here”); // 8

else if(x > = 5) // 9System.out.print(“there”); // 10

else // 11System.out.print(“somewhere”); // 12

else // 13System.out.print(“nowhere”); // 14

1. anywhere2. here3. there4. somewhere5. nowhere_______________________________________________________________________5. The best way to explain the answer is by reformatting the code so the lines are indented properly, as follows:

int x = 10;if(x < 0)

System.out.print(“anywhere”);else if(x < 5)if(x == 10)

System.out.print(“here”);else if(x > = 5)

System.out.print(“there”);else

System.out.print(“somewhere”);else

System.out.print(“nowhere”);The x < 0 comparison on line 4 is false, as is x < 5 on line 6. The else on line 13matches up with the if on line 5, so line 14 executes and nowhere is printed. Therefore, the answer is 5.

42

Page 43: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

4. ОО-понятия (7 вопросов)

Which one of the following uses of inheritances is probably NOT a good design?

1. Car extends Vehicle2. Elephant extends Mammal3. Laptop extends Computer4. Square extends Triangle5. Apple extends Fruit__________________________________________________________________4. The only inheritance that does not satisfy the is-a relationship is 4, because asquare is not a triangle. Therefore, the answer is 4.

43

Page 44: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

Given the following class definitions:

public class Parent { // 1protected void sayHi() { // 2

System.out.print(“Hi”); // 3} // 4

} // 5// 6

class Child extends Parent { // 7public void sayHi() { // 8

System.out.print(“Hello”); // 9} // 10

} // 11

what is output of the result of the following statements?

Parent p = new Child(); // 15p.sayHi(); // 16

1. Hi2. Hello3. Compiler error on line 84. Compiler error on line 155. Line 16 causes an exception to be thrown._________________________________________________________________2. The code compiles and runs fine, so 3, 4, and 5 are incorrect. The compiler sees the sayHi method of Parent on line 16, but at runtime the sayHi method of Child is invoked because the Child class overrides the sayHi method. Therefore, the output is Hello and the answer is 2.

44

Page 45: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the result of the following code?

public class Beverage { // 1private int ounces = 12; // 2boolean carbonated = false; // 3

// 4public static void main(String [] args) { // 5

System.out.println(new SodaPop()); // 6} // 7

} // 8// 9

class SodaPop extends Beverage { // 10public String toString() { // 11

return ounces + “ “ + carbonated; // 12} // 13

} // 14

1. 12 false2. Compiler error on line 63. Compiler error on line 104. Compiler error on line 115. Compiler error on line 12_______________________________________________________________5. The code does not compile, so 1 is incorrect. On line 12, the child class SodaPopattempts to access the ounces field of its parent class Beverage. Because the ounces field is private, SodaPop does not have access to it and a compiler error is generated. Therefore, the answer is 5.

45

Page 46: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the result of the following code?

public class Fruit { // 1private String color = “Green”; // 2

// 3public static void main(String [] args) { // 4

Fruit apple = new Fruit(); // 5apple.color = “Red”; // 6System.out.println(apple.color); // 7

} // 8} // 9

1. Red2. Green3. Compiler error on line 54. Compiler error on lines 6 and 75. Line 6 throws an exception at runtime.__________________________________________________________________1. The code compiles and runs fine, so 3, 4, and 5 are incorrect. The main method isdefined within Fruit, so main has access to the private field color. (If main were defined in a different class, then lines 6 and 7 would not compile.) Line 6 changes the color to Red and line 7 prints it out, so the answer is 1.

46

Page 47: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the result of the following code?

public class X { // 1protected final void doSomething() { // 2

System.out.println(“X”); // 3} // 4

// 5public static void main(String [] args) { // 6

X x = new Y(); // 7x.doSomething(); // 8

} // 9} // 10

// 11class Y extends X { // 12

protected void doSomething() { // 13System.out.println(“Y”); // 14

} // 15} // 16

1. X2. Y3. Compiler error on line 24. Compiler error on line 85. Compiler error on line 13

__________________________________________________________________5. The code does not compile, so 1 and 2 are incorrect. The parent class X declares afinal method named doSomething, and the child class Y attempts to override it online 13. Because a final method cannot be overridden, a compiler error occurs on line 13 and the answer is 5.

47

Page 48: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

Given the following Vehicle and Car class definitions:

package my.vehicles;

public class Vehicle {public String make;protected String model;private int year;int mileage;

}

package my.vehicles.cars; // 1// 2

import my.vehicles.*; // 3// 4

public class Car extends Vehicle { // 5public Car() { // 6

// 7} // 8

} // 9

which of the following statements can appear on line 7 so that the Car class compiles successfully? (Select all that apply.)

1. make = “ Honda “ ;2. model = “ Pilot “ ;3. year = 2009;4. mileage = 15285;5. None of the above__________________________________________________________________1 and 2. The make field in Vehicle is public, so it is accessible anywhere. Therefore, 1 isa correct answer. The model field is protected, so it is accessible in child classes. Because Car extends Vehicle, 2is a correct answer. The year field is private in Vehicle, so 3 does not compile. The mileage field has the default access, but Car is in a different package than Vehicle, so 4 is incorrect.

48

Page 49: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

What is the result of the following code?

public class Browser { // 1public static void main(String [] args) { // 2

Browser b = new Firefox(); // 3IE e = (IE) b; // 4e.go(); // 5

} // 6// 7

public void go() { // 8System.out.println(“Inside Browser”); // 9

} // 10} // 11

// 12class Firefox extends Browser { // 13

public void go() { // 14System.out.println(“Inside Firefox”); // 15

} // 16} // 17

// 18class IE extends Browser { // 19

public void go() { // 20System.out.println(“Inside IE”); // 21

} // 22} // 23

1. Inside Browser2. Inside Firefox3. Inside IE4. Compiler error on line 45. Line 4 generates an exception at runtime.__________________________________________________________________5. The code compiles fine, so 4 is incorrect. However, a ClassCastException is thrown at runtime on line 4 when the reference b, which points to a Firefox object, is cast to an IE reference. Therefore, the answer is 5.

49

Page 50: Higher School of Economics€¦  · Web viewОсновные понятия (12 вопросов). The following code appears in a file named Plant.java . What is the result of compiling

Итого: 12 + 14 + 15 + 7 = 48 вопросов

50