0

programming languages Online Quiz - 101

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

"WRITE MASTER-REC INVALID KEY GO TO PARA-ERROR. Indicate which of the following are not valid modes of opening the INDEXED file of which MASTER-REC is a record."

  1. a and b

  2. a and c

  3. c and d

  4. a and d


Correct Option: C

public interface Status { 12. /* insert code here */ int MY_VALUE = 10; 13. } Which three are valid on line 12? (Choose three.)

  1. final

  2. static

  3. native

  4. public

  5. private

  6. abstract


Correct Option: A,B,D
Explanation:

Explanation: In Java, an interface can only have public abstract methods and public static final fields. Therefore, on line 12, we can have only public, static, and final keywords. The correct answer options are D) public.

Option A) final - This option is correct because final keyword can be used only with the variables and methods

Option B) static - This option is correct because static keyword can be used with an interface.

Option C) native - This option is incorrect because native keyword is not allowed.

Option D) public - This option is correct because public keyword can be used

Option E) private - This option is incorrect because private keyword is not allowed

Option F) abstract - This option is incorrect because abstract keyword is not allowed.

Therefore, the correct options are B) static, D) public, and A) final.

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

AI Explanation

To determine the two code fragments that will allow the class to compile, let's go through each option:

Option A) foreach(x) System.out.println(z); - This option is incorrect because the "foreach" loop syntax is incorrect. It should be for (int z : x) to iterate through the elements of the x array.

Option B) for(int z : x) System.out.println(z); - This option is correct because it uses the enhanced "for" loop (also known as the "foreach" loop) to iterate through the elements of the x array. This syntax is valid and will allow the class to compile.

Option C) while( x.hasNext()) System.out.println( x.next()); - This option is incorrect because it assumes that x is an iterator object with the methods hasNext() and next(). However, in the given code, x is declared as a variable of type int[] (an array), which does not have these methods. Therefore, this option is incorrect.

Option D) for( int i=0; i&lt; x.length; i++ ) System.out.println(x[i]); - This option is correct because it correctly uses a basic "for" loop to iterate through the elements of the x array. It initializes a counter variable i to 0, checks if i is less than the length of the array x, and increments i in each iteration. The syntax x[i] is used to access the elements of the array. This option is valid and will allow the class to compile.

Therefore, the correct options are B) for(int z : x) System.out.println(z); and D) for( int i=0; i&lt; x.length; i++ ) System.out.println(x[i]);.

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. 3

  3. 1 2

  4. 2 3

  5. 1 2 3

  6. Compilation fails.


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) 2 - This option is incorrect because the code does not print only "2".

Option B) 3 - This option is incorrect because the code does not print only "3".

Option C) 1 2 - This option is incorrect because the code does not print "1" at any point.

Option D) 2 3 - This option is correct because the code prints "2" and "3" in the output.

Option E) 1 2 3 - This option is incorrect because the code does not print "1" at any point.

Option F) Compilation fails - This option is incorrect because there are no syntax errors in the code provided.

The correct answer is Option D, which is "2 3". This option is correct because the code prints "2" and "3" in the output.

Let's understand the code:

  • Line 13 initializes the variable x with the value 5.
  • Line 14 initializes the boolean variable b1 with true and b2 with false.
  • Line 17 checks if the condition (x==4) &amp;&amp; !b2 is true. Since x is not equal to 4 and !b2 is true (negation of false), the condition is false.
  • Line 20 assigns the value true to b2 and then checks the condition (b2 = true) &amp;&amp; b1. Since b2 is now true and b1 is also true, the condition is true.
  • Line 21 prints "3" in the output.

Therefore, the output of the code will be "2 3".

  1. 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 instance gets garbage collected.

  2. The code on line 33 throws an exception.

  3. The code on line 35 throws an exception.

  4. The code on line 31 throws an exception.

  5. The code on line 33 executes successfully.


Correct Option: B,C,E

AI Explanation

To answer this question, we need to understand the purpose of the finally block in Java and the conditions under which it is executed.

The finally block is used to specify a block of code that will be executed regardless of whether an exception is thrown or not. It is typically used to release resources or perform cleanup operations.

Now let's go through each option to determine when the code on line 37 will be executed:

Option A) The instance gets garbage collected - This option is incorrect. The execution of the finally block is not dependent on garbage collection.

Option B) The code on line 33 throws an exception - This option is correct. If an exception is thrown on line 33, the finally block will still be executed.

Option C) The code on line 35 throws an exception - This option is correct. If an exception is thrown on line 35, the finally block will still be executed.

Option D) The code on line 31 throws an exception - This option is incorrect. The finally block will not be executed if an exception is thrown on line 31.

Option E) The code on line 33 executes successfully - This option is correct. Even if the code on line 33 executes successfully, the finally block will still be executed.

Therefore, the correct answers are options B, C, and E.

What will be the output of the below code snippet? public class Quiz2 { public static void main(String args[]) { String letters = "ABC"; char[] numbers = { '1', '2', '3' }; System.out.println(letters + " easy as " + numbers); } }

  1. ABC easy as [C@3e25a5

  2. ABC easy as 123

  3. ABC easy as 1 2 3

  4. ABC easy as 1,2,3


Correct Option: A

What will be the output of the below code snippet? public class Quiz3 { public static void main(String args[]) { final String pig = "length: 10"; final String dog = "length: " + pig.length(); System.out.println("Animals are equal: " + pig == dog); } }

  1. True

  2. False


Correct Option: B

What will be the output of the below code snippet? public class Quiz4 { public static void main(String args[]) { int j = 0; for (int i = 0; i < 100; i++) j = j++; System.out.println(j); } }

  1. 99

  2. 0

  3. 100

  4. 101


Correct Option: B

What will be the output of the below code snippet? public class Quiz5 { public static void main(String args[]) { int i = 0; while (-1 << i != 0) i++; System.out.println(i); } }

  1. 32

  2. Infinite Loop

  3. 16

  4. 64


Correct Option: B

AI Explanation

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

Option A) 32 - This option is incorrect because the code snippet does not involve any operations that would produce the value 32.

Option B) Infinite Loop - This option is correct. In the given code snippet, the while loop condition is -1 &lt;&lt; i != 0, which shifts the bits of -1 to the left by i positions. As long as the shifted value is not 0, the loop will continue executing. However, since the left shift operation will never result in 0 for any value of i, the loop will continue indefinitely, causing an infinite loop.

Option C) 16 - This option is incorrect because the code snippet does not involve any operations that would produce the value 16.

Option D) 64 - This option is incorrect because the code snippet does not involve any operations that would produce the value 64.

The correct answer is B. The code will result in an infinite loop.

What will be the output of the below code snippet? public class Quiz6 { public static void main(String[] args) { System.out.println(decision()); } static boolean decision() { try { return true; } finally { return false; } } }

  1. True

  2. False

  3. Compilation Error

  4. Run Time Error


Correct Option: B

What will be the output of the below code snippet? public class Quiz8 { class Base { public String className = "Base"; } class Derived extends Base { private String className = "Derived"; } public static void main(String[] args) { System.out.println(new Derived().className); } }

  1. Derived

  2. Compilation Error in Main mehod

  3. Compilation Error due to Derived class

  4. Run Time Error


Correct Option: B

What will be the output of the below code snippet? public class Quiz9 { public static void main(String args[]) { int j = 0; for (int i = 0; i < 100; i++) j++; System.out.println(j); } }

  1. 99

  2. 100

  3. 0

  4. 101


Correct Option: B

What will be the output of the below code snippet? public class Quiz10 { public static void main(String args[]) { System.out.println((int) (char) (byte) -1); } }

  1. 65536

  2. 65535

  3. 1

  4. -1


Correct Option: B

Given: 20. public class CreditCard { 21. 22. private String cardlD; 23. private Integer limit; 24. public String ownerName; 25. 26. public void setCardlnformation(String cardlD, 27. String ownerName, 28. Integer limit) { 29. this.cardlD = cardlD; 30. this.ownerName = ownerName; 31. this.limit = limit; 32. } 33. }

  1. The class is fully encapsulated.

  2. The code demonstrates polymorphism.

  3. The ownerName variable breaks encapsulation

  4. The cardlD and limit variables break polymorphism.


Correct Option: C

Which Man class properly represents the relationship “Man has a best friend who is a Dog”?

  1. class Man extends Dog { }

  2. class Man implements Dog { }

  3. class Man { private BestFriend dog; }

  4. class Man { private Dog bestFriend; }

  5. class Man { private Dog }

  6. class Man { private BestFriend }


Correct Option: D

AI Explanation

To properly represent the relationship "Man has a best friend who is a Dog," we need to define a class that includes a private instance variable of type Dog to represent the best friend.

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

Option A) class Man extends Dog { } This option is incorrect because it represents a class Man that extends a class Dog, which implies an inheritance relationship rather than a composition relationship.

Option B) class Man implements Dog { } This option is incorrect because it represents a class Man that implements an interface Dog, which is not appropriate for representing a "has-a" relationship.

Option C) class Man { private BestFriend dog; } This option is incorrect because it introduces a new class BestFriend, which is not necessary to represent the relationship. Additionally, the variable should be of type Dog, not BestFriend.

Option D) class Man { private Dog bestFriend; } This option is correct because it represents a class Man that has a private instance variable bestFriend of type Dog, which properly represents the relationship "Man has a best friend who is a Dog."

Option E) class Man { private Dog } This option is incorrect because it includes invalid syntax. It is not possible to use angle brackets in this manner.

Option F) class Man { private BestFriend } This option is incorrect because it includes invalid syntax. It is not possible to use angle brackets in this manner.

The correct answer is D. This option is correct because it represents a class Man that has a private instance variable bestFriend of type Dog, which properly represents the relationship "Man has a best friend who is a Dog."

Which statements about JDBC are true?

  1. JDBC is an API to connect to relational-, object- and XML data sources

  2. JDBC stands for Java DataBase Connectivity

  3. JDBC is an API to access relational databases, spreadsheets and flat files

  4. JDBC is an API to bridge the object-relational mismatch between OO programs and relational


Correct Option: B,C

AI Explanation

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

Option A) JDBC is an API to connect to relational-, object- and XML data sources - This option is incorrect. JDBC is primarily used for connecting to relational databases, not object or XML data sources.

Option B) JDBC stands for Java DataBase Connectivity - This option is correct. JDBC stands for Java DataBase Connectivity and is an API provided by Java for connecting to relational databases.

Option C) JDBC is an API to access relational databases, spreadsheets, and flat files - This option is correct. JDBC can be used to connect to and access relational databases, as well as spreadsheets and flat files.

Option D) JDBC is an API to bridge the object-relational mismatch between OO programs and relational - This option is incorrect. While JDBC can be used in object-oriented programs to interact with relational databases, it is not specifically designed to bridge the object-relational mismatch.

The correct answer is B, C. JDBC stands for Java DataBase Connectivity and is an API used to access relational databases, spreadsheets, and flat files.

- Hide questions