0

programming languages Online Quiz - 297

Description: programming languages Online Quiz - 297
Number of Questions: 20
Created by:
Tags: programming languages
Attempted 0/20 Correct 0 Score 0
  1. Member Function

  2. Same name as Class name

  3. Default constructor initializes all non-initialized fields and variables to zero.

  4. Compiler adds an empty constructor, if we do not write our own constructor


Correct Option: A,B,C,D

Which is true about applet?

  1. Applet version has main method

  2. GUI components are added explicitly to the Applet

  3. Aplet class is declared private

  4. None of the above


Correct Option: D

JFC Stands for

  1. Java Foundation Course

  2. Java Fundamental Classes

  3. Java Foundation Classes

  4. None of the above


Correct Option: C

Given: 10. public class Bar { 11.static void foo(int...x) { 12. // insert code here 13. } 14. } Which two code fragments, inserted independently at line 12, will allow the class to compile? (Choose two.)

  1. foreach(x) System.out.println(z);

  2. for(int z : x) System.out.println(z);

  3. while( x.hasNext()) System.out.println( x.next());

  4. for( int i=0; i< x.length; i++ ) System.out.println(x[i]);


Correct Option: B,D

Given: 11. public class Test { 12. public static void main(String [] args) { 13. int x =5; 14. boolean b1 = true; 15. boolean b2 = false; 16. 17.if((x==4) && !b2) 18. System.out.print(”l “); 19. System.out.print(”2 “); 20. if ((b2 = true) && b1) 21. System.out.print(”3 “); 22. } 23. } What is the result?

  1. 2

  2. 2 3

  3. Compilation fails.

  4. 1 2 3


Correct Option: B

Given: 31. // some code here 32. try { 33. // some code here 34. } catch (SomeException se) { 35. // some code here 36. } finally { 37. // some code here 38. } Under which three circumstances will the code on line 37 be executed? (Choose three.)

  1. The code on line 31 throws an exception

  2. The code on line 33 throws an exception

  3. The code on line 35 throws an exception

  4. The code on line 33 executes successfully


Correct Option: B,C,D

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) The code on line 31 throws an exception - This option is incorrect. The finally block will only be executed if an exception is thrown or caught within the try block. In this case, if an exception is thrown on line 31, it will not be caught within the try block, so the finally block will not be executed.

Option B) The code on line 33 throws an exception - This option is correct. If an exception is thrown on line 33, it will be caught by the catch block on line 34. After the catch block is executed, the finally block on line 37 will be executed.

Option C) The code on line 35 throws an exception - This option is correct. If an exception is thrown on line 35, it will be caught by the catch block on line 34. After the catch block is executed, the finally block on line 37 will be executed.

Option D) The code on line 33 executes successfully - This option is correct. Even if the code on line 33 executes successfully without throwing an exception, the finally block on line 37 will still be executed. The finally block is always executed, regardless of whether an exception is thrown or not.

Therefore, the correct answer is options B, C, and D.

Given: 10. interface Foo {} 11. class Alpha implements Foo { } 12. class Beta extends Alpha {} 13. class Delta extends Beta { 14. public static void main( String[] args) { 15. Beta x = new Beta(); 16. // insert code here 17. } 18. } Which code, inserted at line 16, will cause a java.lang.ClassCastException?

  1. Alpha a = x;

  2. Foo f= (Alpha)x;

  3. Foo f= (Delta)x;

  4. Beta b = (Beta)(Alpha)x;


Correct Option: C

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) Alpha a = x; - This option is incorrect because the object 'x' is of type Beta, which is a subclass of Alpha. Therefore, it can be assigned to a variable of type Alpha without any issue.

Option B) Foo f = (Alpha)x; - This option is incorrect because the object 'x' is of type Beta, which is a subclass of Alpha. The cast to Alpha is valid since Beta is a subclass of Alpha. Additionally, 'x' can be assigned to a variable of type Foo because Beta indirectly implements the Foo interface through its superclass Alpha.

Option C) Foo f = (Delta)x; - This option is correct because the object 'x' is of type Beta, and you are trying to cast it to Delta. Since Delta is not a superclass of Beta, this cast will result in a ClassCastException. It is not possible to cast an object to a type that is not in its inheritance hierarchy.

Option D) Beta b = (Beta)(Alpha)x; - This option is incorrect because the object 'x' is already of type Beta, so there is no need for the cast. The cast to Alpha is also unnecessary since Beta is a subclass of Alpha. Therefore, this code will not cause a ClassCastException.

The correct answer is Option C. This option will cause a java.lang.ClassCastException because you are trying to cast an object of type Beta to a type that is not in its inheritance hierarchy (Delta).

  1. Locale loc = Locale.getLocale();System.out.println(loc.getDisplayCountry()+ “ “+ df.format(d));

  2. Locale bc = Locale.getLocale();System.out.println(loc.getDisplayCountry()+ “ “+ df.setDateFormat(d));

  3. Locale loc = Locale.getDefault();System.out.println(loc.getDisplayCountry()+ “ “+ df.setDateFormat(d));

  4. Locale loc = Locale.getDefault(); System.out.println(loc.getDisplayCountry()+ “ “ + df.format(d));


Correct Option: D
Explanation:

To solve this question, the user needs to understand the concepts of Locale, Date, and DateFormat objects.

Option A is incorrect because there is no such method as getLocale() in the Locale class. The correct method is getDefault(). Additionally, df.format(d) is used to format the date object d using the date format df. This option is incorrect because df.format(d) is called with a space between the method name and the parentheses.

Option B is incorrect because Locale.getLocale() is not a valid method in the Locale class. Also, df.setDateFormat(d) is not a valid method in the DateFormat class. Instead, df.format(d) should be used to format the Date object using the appropriate format.

Option C is incorrect because Locale.getDefault() is not used to get the current locale. Instead, Locale.getLocale() is used, which is not a valid method in the Locale class. df.setDateFormat(d) is also not a valid method in the DateFormat class. Instead, df.format(d) should be used to format the Date object using the appropriate format.

Option D is correct. Locale.getDefault() is used to get the current locale, and df.format(d) is used to format the Date object d using the appropriate date format. This option will output the current locale's country name and the appropriate version of the date object d.

Therefore, the correct answer is:

The Answer is: D

  1. The class is fully encapsulated

  2. The ownerName variable breaks encapsulation

  3. The cardlD and limit variables break polymorphism

  4. The setCardlnformation method breaks encapsulation


Correct Option: B
Explanation:

To solve this question, the user needs to understand the concepts of encapsulation and polymorphism.

Encapsulation is the concept of hiding the implementation details of a class and making the variables and methods private to ensure that they cannot be accessed from outside the class.

Polymorphism, on the other hand, is the ability of an object to take on many forms, depending on the context in which it is used.

Now let's examine each option and determine which is true:

A. The class is fully encapsulated: This option is incorrect. While the class does have private variables (cardlD and limit), the ownerName variable is public, which means it can be accessed from outside the class. Therefore, the class is not fully encapsulated.

B. The ownerName variable breaks encapsulation: This option is correct. The ownerName variable is declared as public, which means it can be accessed and modified from outside the class. This breaks the concept of encapsulation because the implementation details of the class are not fully hidden.

C. The cardlD and limit variables break polymorphism: This option is incorrect. The cardlD and limit variables have nothing to do with polymorphism. They are private variables that can only be accessed from within the class.

D. The setCardlnformation method breaks encapsulation: This option is incorrect. The setCardlnformation method is a public method, which is used to set the values of the private variables. It does not break encapsulation because it is providing a controlled way of accessing the private variables.

Therefore, the correct answer is:

The Answer is: B

Assume that country is set for each class. Given: 10. public class Money { 11. private String country, name; 12. public getCountry() { return country; } 13.} and: 24. class Yen extends Money { 25. public String getCountry() { return super.country; } 26. } 27. 28. class Euro extends Money { 29. public String getCountry(String timeZone) { 30. return super.getCountry(); 31. } 32. } Which two are correct? (Choose two.)

  1. Euro returns correct values

  2. An exception is thrown at runtime.

  3. Yen and Euro both return correct values

  4. Compilation fails because of an error at line 25.


Correct Option: A,D

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) Euro returns correct values - This option is correct. In the Euro class, the getCountry(String timeZone) method calls the super.getCountry() method, which is inherited from the Money class. Since the getCountry() method in the Money class does not take any arguments and simply returns the value of the country field, this method will return the correct value.

Option B) An exception is thrown at runtime - This option is not mentioned in the given code snippet. Therefore, it is incorrect.

Option C) Yen and Euro both return correct values - This option is incorrect. The Yen class overrides the getCountry() method and returns super.country. However, the super.country expression is incorrect. The country field is private in the Money class and cannot be accessed directly by the Yen class. Therefore, the Yen class will not return the correct value.

Option D) Compilation fails because of an error at line 25 - This option is correct. The Yen class attempts to access the country field of the Money class using super.country, but the country field is private and cannot be accessed from a subclass. This will result in a compilation error.

The correct answers are A and D.

  1. class Man { private Dog }

  2. class Man { private BestFriend dog; }

  3. class Man { private Dog bestFriend; }

  4. class Man implements Dog { }


Correct Option: C
Explanation:

To properly represent the relationship "Man has a best friend who is a Dog", we need to use composition to indicate that a Man object has a Dog object as its best friend.

Now, let's go through each option and explain why it is right or wrong:

A. class Man { private Dog }

This option is incorrect. The syntax used to represent the relationship between Man and Dog is incorrect. The "" does not make sense in this context.

B. class Man { private BestFriend dog; }

This option is incorrect. Although the variable name "dog" is used, it does not represent the intended relationship. The class BestFriend is not defined anywhere and doesn't make sense in this context.

C. class Man { private Dog bestFriend; }

This option is correct. The class Man has a private variable named "bestFriend" of type Dog, indicating that a Man object has a Dog object as its best friend.

D. class Man implements Dog { }

This option is incorrect. The interface Dog cannot properly represent the relationship between Man and Dog, and implementing the Dog interface does not make sense in this context.

Therefore, the correct answer is:

The Answer is: C. class Man { private Dog bestFriend; }

Given: 11. public class Person { 12. private name; 13. public Person(String name) { 14. this.name = name; 15. } 16. public int hashCode() { 17. return 420; 18. } 19. } Which is true?

  1. The time to determine whether a Person object is contained in a HashSet is constant and does NOT depend on the size of the map

  2. Deleting a Person key from a HashMap will delete all map entries for all keys of type Person

  3. Inserting a second Person object into a HashSet will cause the first Person object to be removed as a duplicate.

  4. The time to find the value from HashMap with a Person key depends on the size of the map.


Correct Option: D

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) The time to determine whether a Person object is contained in a HashSet is constant and does NOT depend on the size of the map - This option is incorrect. The time to determine whether a Person object is contained in a HashSet is dependent on the size of the HashSet. In the worst case scenario, if all the elements in the HashSet have the same hash code (as in the given code where the hash code is always 420), the time complexity becomes O(n), where n is the number of elements in the HashSet.

Option B) Deleting a Person key from a HashMap will delete all map entries for all keys of type Person - This option is incorrect. Deleting a Person key from a HashMap will only delete the specific entry associated with that key. It will not delete entries for other keys of type Person.

Option C) Inserting a second Person object into a HashSet will cause the first Person object to be removed as a duplicate - This option is incorrect. HashSet does not allow duplicate entries. When inserting a second Person object into a HashSet, the first Person object will not be removed as a duplicate. The HashSet will simply not allow the second Person object to be added if it is a duplicate of an existing object.

Option D) The time to find the value from HashMap with a Person key depends on the size of the map - This option is correct. In a HashMap, the time to find the value associated with a specific Person key is dependent on the size of the map. The time complexity for finding an element in a HashMap is typically O(1) on average, but in the worst case scenario, it can be O(n), where n is the number of elements in the map.

The correct answer is D. The time to find the value from a HashMap with a Person key depends on the size of the map.

Given: 23. Object [] myObjects = { 24. new integer(12), 25. new String(”foo”), 26. new integer(5), 27. new Boolean(true) 28. }; 29. Arrays.sort(myObjects); 30. for( int i=0; i

  1. Compilation fails due to an error in line 23.

  2. Compilation fails due to an error in line 29.

  3. A ClassCastException occurs in line 29.

  4. A ClassCastException occurs in line 31.


Correct Option: C
Explanation:

To solve this question, the user needs to understand the concepts of sorting arrays and the differences between the Object and Integer classes.

The code creates an array of Objects that includes Integer, String, and Boolean objects, then sorts the array using the Arrays.sort() method. Finally, the code prints out each object in the sorted array using a for loop.

Now, let's go through each option and explain why it is right or wrong:

A. Compilation fails due to an error in line 23. This option is incorrect because there are no errors in the syntax of line 23. The code creates an array of objects and initializes it with Integer, String, and Boolean objects.

B. Compilation fails due to an error in line 29. This option is incorrect because there are no errors in the syntax of line 29. The Arrays.sort() method is a valid method and can be used to sort arrays.

C. A ClassCastException occurs in line 29. This option is correct. The code attempts to sort an array of Objects, which includes Integer, String, and Boolean objects. When the sort method attempts to compare the elements in the array, a ClassCastException occurs because the elements are not all of the same type. The Integer objects are not comparable to the String and Boolean objects, resulting in an exception.

D. A ClassCastException occurs in line 31. This option is incorrect. The for loop uses the toString() method to print out each object in the array, which is a valid method for all objects. However, the exception occurs during the sort method in line 29, not during the printing of the objects.

The Answer is: C

  1. Compilation fails.

  2. An exception is thrown at runtime

  3. doStuffx = 6 main x = 6

  4. doStuffx = 5 main x = 5


Correct Option: D
Explanation:

To solve this question, the user needs to understand the basic concept of method invocation and parameter passing.

First, let's go through the code. The Pass class has a method main which initializes an integer variable x to 5. It then creates a new instance of the Pass class and invokes the doStuff method on that instance, passing in the value of x. Finally, it prints out the value of x.

The doStuff method takes an integer parameter x, prints out the value of x incremented by 1, and then increments the parameter x by 1.

Now, let's go through each option and explain why it is right or wrong:

A. Compilation fails - This option is incorrect. There are no syntax errors in the code.

B. An exception is thrown at runtime - This option is incorrect. There are no runtime errors in the code.

C. doStuffx = 6 main x = 6 - This option is incorrect. The value of x in main is still 5 after the doStuff method is called because Java is a pass-by-value language. The parameter x in doStuff is a copy of the value of x in main, so incrementing the parameter does not affect the original value of x.

D. doStuffx = 5 main x = 5 - This option is correct. The doStuff method prints out the value of x (which is 5) incremented by 1, resulting in the output "doStuff x = 6". However, the value of x in main remains 5 because Java is a pass-by-value language. Therefore, the final output is "main x = 5".

Therefore, the answer is: D. doStuffx = 5 main x = 5

Given: 10. package com.sun.scjp; 11. public class Geodetics { 12. public static final double DIAMETER = 12756.32; // kilometers 13. } Which two correctly access the DIAMETER member of the Geodetics class? (Choose two.)

  1. import com.sun.scjp.Geodetics;public class TerraCarta {public double halfway(){ return Geodetics.DIAMETER/2.0; } }

  2. import static com.sun.scjp.Geodetics;public class TerraCarta {public double halfway() { return DIAMETER/2.0; } }

  3. import static com.sun.scjp.Geodetics. *;public class TerraCarta {public double halfway() { return DIAMETER/2.0; } }

  4. package com.sun.scjp;public class TerraCarta {public double halfway() { return DIAMETER/2.0; } }


Correct Option: A,C

AI Explanation

To access the DIAMETER member of the Geodetics class, we need to understand the different ways to access static members.

Option A) import com.sun.scjp.Geodetics; public class TerraCarta { public double halfway(){ return Geodetics.DIAMETER/2.0; } }

This option is correct because it imports the Geodetics class and accesses the DIAMETER member using the syntax Geodetics.DIAMETER.

Option B) import static com.sun.scjp.Geodetics; public class TerraCarta { public double halfway() { return DIAMETER/2.0; } }

This option is incorrect because it tries to import the Geodetics class using the static keyword, but it does not specify which member of the class to import. Therefore, the line return DIAMETER/2.0; will cause a compilation error because DIAMETER is not recognized.

Option C) import static com.sun.scjp.Geodetics. *; public class TerraCarta { public double halfway() { return DIAMETER/2.0; } }

This option is correct because it imports all the static members of the Geodetics class using the * wildcard. Therefore, the line return DIAMETER/2.0; will correctly access the DIAMETER member.

Option D) package com.sun.scjp; public class TerraCarta { public double halfway() { return DIAMETER/2.0; } }

This option is incorrect because it does not import the Geodetics class, so the line return DIAMETER/2.0; will cause a compilation error because DIAMETER is not recognized.

Therefore, the correct answers are Option A and Option C.

Given: 10. class Nav{ 11. public enum Direction { NORTH, SOUTH, EAST, WEST } 12. } 13. public class Sprite{ 14. // insert code here 15. } Which code, inserted at line 14, allows the Sprite class to compile?14

  1. Direction d = NORTH;

  2. Nav.Direction d = NORTH;

  3. Direction d = Direction.NORTH;

  4. Nav.Direction d = Nav.Direction.NORTH;


Correct Option: D

AI Explanation

To answer this question, you need to understand how to access an enum type that is defined inside a class.

In the given code, the enum Direction is defined inside the class Nav. The Sprite class is trying to access this enum.

Let's go through each option to understand why it is correct or incorrect:

Option A) Direction d = NORTH; - This option is incorrect because NORTH is not directly accessible in the Sprite class. To access the NORTH value, you need to specify the enum type Direction.

Option B) Nav.Direction d = NORTH; - This option is incorrect because NORTH is not directly accessible in the Sprite class. Additionally, the correct way to access the enum type Direction is by using Nav.Direction to specify the fully qualified name.

Option C) Direction d = Direction.NORTH; - This option is incorrect because Direction.NORTH is not directly accessible in the Sprite class. To access the NORTH value, you need to specify the enum type Direction.

Option D) Nav.Direction d = Nav.Direction.NORTH; - This option is correct because it specifies the fully qualified name Nav.Direction to access the enum type Direction and the NORTH value. This allows the Sprite class to compile successfully.

The correct answer is option D. This option is correct because it correctly accesses the enum type Direction using the fully qualified name Nav.Direction and initializes the variable d with the value Nav.Direction.NORTH.

Given: 10. interface Foo { int bar(); } 11. public class Sprite { 12. public int fubar( Foo foo) { return foo.bar(); } 13. public void testFoo() { 14. fubar( 15. // insert code here 16.); 17. } 18. } Which code, inserted at line 15, allows the class Sprite to compile?

  1. Foo { public int bar() { return 1; } }

  2. new Foo { public int bar() { return 1; } }

  3. newFoo() { public int bar(){return 1; } }

  4. new class Foo { public int bar() { return 1; } }


Correct Option: C

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) Foo { public int bar() { return 1; } } This option is incorrect because it is missing the keyword "new" before the Foo interface. The Foo interface needs to be instantiated with a concrete implementation.

Option B) new Foo { public int bar() { return 1; } } This option is incorrect because it is missing a set of parentheses after "new Foo". When instantiating an interface with an anonymous class, the parentheses are required.

Option C) newFoo() { public int bar(){return 1; } } This option is correct because it correctly instantiates the Foo interface with an anonymous class and provides an implementation for the "bar()" method.

Option D) new class Foo { public int bar() { return 1; } } This option is incorrect because it is attempting to create a new class named "Foo" instead of instantiating the Foo interface. The keyword "class" is not needed in this context.

The correct answer is Option C. This option is correct because it properly instantiates the Foo interface with an anonymous class and provides an implementation for the "bar()" method.

Click the Exhibit button. 10. interface Foo { 11. int bar(); 12. } 13. 14. public class Beta { 15. 16. class A implements Foo { 17. public int bar() { return 1; } 18. } 19. 20. public int fubar( Foo foo) { return foo.bar(); } 21. 22. public void testFoo() { 23. 24. class A implements Foo { 25. public int bar() { return 2; } 26. } 27. 28. System.out.println( fubar( new A())); 29. } 30. 31. public static void main( String[] argv) { 32. new Beta().testFoo(); 33. } 34. } Which three statements are true? (Choose three.)

  1. The code compiles and the output is 2

  2. If lines 16, 17 and 18 were removed, the code would compile and

  3. If lines 24, 25 and 26 were removed, the code would compile and the output would be 1

  4. D. If lines 24, 25 and 26 were removed, compilation would fail


Correct Option: A,B,C

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) The code compiles and the output is 2 - This option is incorrect. If lines 16, 17, and 18 were removed, the class A would not implement the bar() method from the Foo interface. Therefore, the code would not compile, and there would be no output.

Option B) If lines 16, 17, and 18 were removed, the code would compile and - This option is correct. If lines 16, 17, and 18 were removed, the class A would not implement the bar() method from the Foo interface. However, since the fubar() method accepts a parameter of type Foo, it can still accept an instance of any class that implements the Foo interface, including the nested class A defined in the testFoo() method. Therefore, the code would compile.

Option C) If lines 24, 25, and 26 were removed, the code would compile and the output would be 1 - This option is correct. If lines 24, 25, and 26 were removed, the nested class A defined in the testFoo() method would not be used. Instead, the class A defined outside the testFoo() method would be used, which implements the bar() method and returns 2. Therefore, the code would compile, and the output would be 2.

Option D) If lines 24, 25, and 26 were removed, compilation would fail - This option is incorrect. If lines 24, 25, and 26 were removed, the nested class A defined in the testFoo() method would not be used. However, the code would still compile and execute using the class A defined outside the testFoo() method.

Therefore, the correct answer is:

A) The code compiles and the output is 2 B) If lines 16, 17, and 18 were removed, the code would compile and C) If lines 24, 25, and 26 were removed, the code would compile and the output would be 1

- Hide questions