0

programming languages Online Quiz - 100

Description: programming languages Online Quiz - 100
Number of Questions: 20
Created by:
Tags: programming languages
Attempted 0/20 Correct 0 Score 0

Which type of driver provides JDBC access via one or more ODBC drivers?

  1. Type 4 driver

  2. Type 3 driver

  3. Type 2 driver

  4. Type 1 driver


Correct Option: D

Given: 1. package sun.scjp; 2. public enum Color { RED, GREEN, BLUE } 1. package sun.beta; 2. // insert code here 3. public class Beta { 4. Color g = GREEN; 5. public static void main( String[] argv) 6. { System.out.println( GREEN); } 7. } The class Beta and the enum Color are in different packages. Which two code fragments, inserted individually at line 2 of the Beta declaration, will allow this code to compile? (Choose two.)

  1. import sun.scjp.Color.*;

  2. import static sun.scjp.Color.*;

  3. import sun.scjp.Color; import static sun.scjp.Color.*;

  4. import sun.scjp.; import static sun.scjp.Color.;

  5. import sun.scjp.Color; import static sun.scjp.Color.GREEN;


Correct Option: C,E

AI Explanation

To answer this question, we need to understand the concept of importing packages and static members.

In the given code, the class Beta and the enum Color are in different packages. To access the enum Color in the class Beta, we need to import it. Additionally, since the enum Color is defined using the "enum" keyword, we can import its static members individually.

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

Option A) import sun.scjp.Color.*; This option is incorrect because it imports all the members (including non-static members) of the Color enum. It does not allow us to access the enum values directly without specifying the enum name.

Option B) import static sun.scjp.Color.*; This option is incorrect because it imports all the static members of the Color enum. It does not import the enum itself. Therefore, we won't be able to access the enum values directly without specifying the enum name.

Option C) import sun.scjp.Color; import static sun.scjp.Color.*; This option is correct because it imports the Color enum and its static members individually. It allows us to access the enum values directly without specifying the enum name.

Option D) import sun.scjp.; import static sun.scjp.Color.; This option is incorrect because it imports all the members of the sun.scjp package, including non-static members of the Color enum. It does not import the enum itself. Therefore, we won't be able to access the enum values directly without specifying the enum name.

Option E) import sun.scjp.Color; import static sun.scjp.Color.GREEN; This option is correct because it imports the Color enum and only the GREEN static member. It allows us to access the GREEN enum value directly without specifying the enum name.

The correct answers are C and E.

Given: 11. public static void parse(String str) { 12. try { 13. float f= Float.parseFloat(str); 14. } catch (NumberFormatException nfe) { 15. f= 0; 16. } finally { 17. System.out.println(f); 18. } 19. } 20. public static void main(String[] args) { 21. parse(”invalid”); 22. } What is the result?

  1. 0.0

  2. Compilation fails

  3. A ParseException is thrown by the parse method at runtime.

  4. A NumberFormatException is thrown by the parse method at


Correct Option: B

Which type of Statement can execute parameterized queries?

  1. PreparedStatement

  2. ParameterizedStatement

  3. ParameterizedStatement and CallableStatement

  4. All kinds of Statements (i.e. which implement a sub interface of Statement)


Correct Option: B

Given: 11. public static void main(String[] args) { 12. Object obj =new int[] { 1,2,3 }; 13. int[] someArray = (int[])obj; 14. for (int i: someArray) System.out.print(i +“ “) 15. } ‘What is the result?

  1. 1 2 3

  2. B. Compilation fails because of an error in line 12.

  3. B. Compilation fails because of an error in line 13.

  4. B. Compilation fails because of an error in line 14.


Correct Option: A

Given: 12. public class Test { 13. public enum Dogs {collie, harrier}; 14. public static void main(String [] args) { 15. Dogs myDog = Dogs.collie; 16. switch (myDog) { 17. case collie: 18. System.out.print(”collie “); 19. case harrier: 20. System.out.print(”harrier “); 21. } 22. } 23. } What is the result?

  1. collie

  2. harrier

  3. Compilation fails

  4. collie harrier


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) collie - This option is incorrect. If we follow the code execution, we see that the switch statement matches the value of myDog with the cases. The value of myDog is Dogs.collie, so it matches the case collie on line 17. However, there is no break statement after the System.out.print("collie ") on line 18, so the execution continues to the next case.

Option B) harrier - This option is incorrect. As explained in Option A, after the execution of the System.out.print("collie ") on line 18, there is no break statement, so the execution continues to the next case. Therefore, the output will include both "collie" and "harrier".

Option C) Compilation fails - This option is incorrect. The code provided does not contain any compilation errors.

Option D) collie harrier - This option is correct. As explained in Option A and Option B, the output will include both "collie" and "harrier". So the correct answer is D.

The correct answer is D.

Given: 33. try { 34. // some code here 35. } catch (NullPointerException e1) { 36. System.out.print(”a”); 37. } catch (RuntimeException e2) { 38. System.out.print(”b”); 39. } finally { 40. System.out.print(”c”); 41. } What is the result if a NullPointerException occurs on line 34?

  1. c

  2. a

  3. ab

  4. ac

  5. bc


Correct Option: D

Given: 10. public class Foo implements java.io.Serializable { 11. private int x; 12. public int getX() { return x; } 12.publicFoo(int x){this.x=x; } 13. private void writeObject( ObjectOutputStream s) 14. throws IOException { 15. // insert code here 16. } 17. } Which code fragment, inserted at line 15, will allow Foo objects to be correctly serialized and deserialized?

  1. s.writeInt(x);

  2. s.serialize(x);

  3. s.writeObject(x);

  4. s.defaultWriteObject();


Correct Option: D
Explanation:

To solve this question, the user needs to understand the concept of serialization in Java. Serialization is the process of converting a Java object into a stream of bytes so that it can be stored in a file or transmitted over a network. Deserialization is the reverse process of converting a stream of bytes back into a Java object.

The code given above defines a class Foo that implements the Serializable interface. This means that instances of Foo can be serialized and deserialized. The writeObject() method is a callback method that is called during serialization and allows the developer to customize the serialization process.

Option A: s.writeInt(x); This option will write the integer value of x to the ObjectOutputStream. However, this is not enough to fully serialize the Foo object, as there are other instance variables besides x that also need to be serialized.

Option B: s.serialize(x); This option is not valid, as there is no such method as serialize() in the ObjectOutputStream class.

Option C: s.writeObject(x); This option will write the object x to the ObjectOutputStream. However, this will not fully serialize the Foo object, as x is just one instance variable and there are others that also need to be serialized.

Option D: s.defaultWriteObject(); This option is the correct answer. The defaultWriteObject() method serializes all non-transient, non-static instance variables in the object. This includes the instance variable x, as well as any other instance variables that Foo may have. By calling this method, we ensure that the entire Foo object is serialized and can be correctly deserialized later.

Therefore, the answer is: D

Given: 11. String test= “a1b2c3”; 12. String[] tokens = test.split(”\d”); 13. for(String s: tokens) System.out.print(s +“ “); What is the result?

  1. a b c

  2. 1 2 3

  3. a1b2c3

  4. Compilation fails


Correct Option: A

AI Explanation

To determine the result of the given code, let's go through each line:

  1. String test = "a1b2c3"; - This line declares and initializes a String variable named "test" with the value "a1b2c3".

  2. String[] tokens = test.split("\d"); - This line splits the string "test" using the regular expression "\d", which matches any digit. The result is an array of strings, where each element contains the substrings separated by digits. In this case, the array will contain ["a", "b", "c"].

  3. for(String s : tokens) System.out.print(s + " "); - This line iterates over each element in the "tokens" array and prints it followed by a space. The output will be "a b c".

Therefore, the result of the given code is:

A) a b c

Which two are true? (Choose two.)

  1. An encapsulated, public class promotes re-use.

  2. Classes that share the same interface are always tightly

  3. An encapsulated class allows subclasses to overload methods, but

  4. An encapsulated class allows a programmer to change an


Correct Option: A,D

AI Explanation

To answer this question, let's go through each option:

Option A) An encapsulated, public class promotes re-use - This option is true. Encapsulation is a concept in object-oriented programming that promotes re-usability by hiding the internal implementation details of a class and exposing only the necessary public methods or properties. By encapsulating a class, other developers can use the class without needing to know the intricate details of how it works, thus promoting re-use.

Option B) Classes that share the same interface are always tightly - This option is false. Classes that share the same interface may or may not be tightly coupled. The level of coupling between classes depends on how they interact with each other and the dependencies they have. It is possible to have loosely coupled classes that share the same interface.

Option C) An encapsulated class allows subclasses to overload methods, but - This option is false. Encapsulation is a mechanism that hides the internal implementation details of a class. Overloading methods is a concept related to polymorphism, where a class can have multiple methods with the same name but different parameters. Encapsulation and method overloading are separate concepts.

Option D) An encapsulated class allows a programmer to change an - This option is true. Encapsulation provides a level of abstraction and encapsulates the internal implementation details of a class. This allows a programmer to change the internal implementation of a class without affecting the code that uses the class. The external code only interacts with the public methods or properties of the encapsulated class, so changes to the internal implementation can be made without impacting the code that uses the class.

Based on the explanations above, the correct options are A) An encapsulated, public class promotes re-use and D) An encapsulated class allows a programmer to change an.

  1. abstract double area() { }

  2. abstract double area()

  3. abstract double area();

  4. abstract double area(); { }


Correct Option: C
  1. Compilation fails.

  2. An exception is thrown at runtime.

  3. The code executes normally and prints “bar”.

  4. The code executes normally, but nothing prints.


Correct Option: C
  1. Compilation fails.

  2. An exception is thrown at runtime.

  3. The code executes normally and prints “sleep”.

  4. The code executes normally, but nothing is printed.


Correct Option: C
  1. A finalizer may NOT be invoked explicitly.

  2. The finalize method declared in class Object takes no action.

  3. super.finalize() is called implicitly by any overriding finalize method.

  4. The finalize method for a given object will be called no more than

  5. The order in which finalize will be called on two objects is based on


Correct Option: B,D
  1. Inheritance represents an is-a relationship.

  2. Inheritance represents a has-a relationship.

  3. Interfaces must be used when creating a has-a relationship.

  4. Instance variables can be used when creating a has-a relationship.


Correct Option: A,D
  1. The return type for a method can be any Java type, including void

  2. An important principal of object oriented programming is implementation hiding

  3. When you perform mathematical calculations on the unlike data type, java will perform a implicit conversion to unify the types

  4. All the Above


Correct Option: D
- Hide questions