0

programming languages Online Quiz - 52

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

Select three correct statements

  1. A static method may override another static method

  2. A static method cannot override a non-static method

  3. A non-static method cannot override a static method

  4. A non-static method may be overloaded by a static method

  5. A synchronized method cannot be overridden


Correct Option: A,B,C,D,E

How old are you 1

  1. 1

  2. 2

  3. 3

  4. 4


Correct Option: A,B,C,D

How old are you 3

  1. 1

  2. 2

  3. 3

  4. 4


Correct Option: D

How old are you 4

  1. 1

  2. 2

  3. 3

  4. 4


Correct Option: B

How old are you 5

  1. 1

  2. 2

  3. 3

  4. 4


Correct Option: C

How old are you 6

  1. 1

  2. 2

  3. 3

  4. 4


Correct Option: D

How old are you 7

  1. 1

  2. 2

  3. 3

  4. 4


Correct Option: A

Select three correct statements.

  1. ) A static method may override another static method

  2. A static method cannot override a non-static method

  3. non-static method cannot override a static method

  4. A non-static method may be overloaded by a static method

  5. A synchronized method cannot be overridden


Correct Option: B,C,D

Which of the following are methods of the Thread class.

  1. public void run()

  2. public void start()

  3. public void exit()

  4. public final void setAccess()

  5. public final void setPriority(int priNbr)

  6. public final int getPriority()


Correct Option: A,B,E,F

Casting occurs commonly between numeric types

  1. True

  2. False


Correct Option: A

Which are valid declarations? (Choose all that apply)

  1. int $x

  2. int 123

  3. int _123

  4. int #dim

  5. int %percent

  6. int central_sales_region_Summer_2005_gross_sales


Correct Option: A,C,F

AI Explanation

To determine the valid declarations, let's go through each option:

Option A) int $x - This option is a valid declaration. In programming languages like C or C++, variable names can start with an underscore (_) or a letter. Therefore, the variable name "$x" is valid.

Option B) int 123 - This option is not a valid declaration. Variable names cannot start with a number.

Option C) int 123 - This option is a valid declaration. Variable names can start with an underscore (), so "_123" is a valid variable name.

Option D) int #dim - This option is not a valid declaration. Variable names cannot start with special characters like "#" in most programming languages.

Option E) int %percent - This option is not a valid declaration. Variable names cannot start with special characters like "%" in most programming languages.

Option F) int central_sales_region_Summer_2005_gross_sales - This option is a valid declaration. Variable names can contain letters, numbers, and underscores. Therefore, "central_sales_region_Summer_2005_gross_sales" is a valid variable name.

The correct options are A, C, and F.

public class A { private int counter=0; public static int getInstanceCount() { return counter; } public A() { counter++; } } Given this code from Class B: 25. A a1 = new A(); 26. A a2 = new A(); 27. A a3 = new A(); 28. System.out.println(A.getInstanceCount()); What is the result?

  1. Compilation of class A fails.

  2. Line 28 prints the value 3 to System.out.

  3. Line 28 prints the value 1 to System.out

  4. A runtime error occurs when line 25 executes

  5. Compilation fails because of an error on line 28


Correct Option: A

AI Explanation

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

Option A) Compilation of class A fails - This option is correct because there is an error in the code. The variable counter is declared as private in class A, which means it is only accessible within the class. However, the getInstanceCount() method in class A is a static method, meaning it can be accessed without creating an instance of the class. So, when trying to access the counter variable in the static method, a compilation error will occur since counter is not accessible.

Option B) Line 28 prints the value 3 to System.out. - This option is incorrect because the code will not compile due to the error mentioned above. Therefore, line 28 will not be executed, and the value of getInstanceCount() will not be printed.

Option C) Line 28 prints the value 1 to System.out. - This option is incorrect because the code will not compile due to the error mentioned above. Therefore, line 28 will not be executed, and the value of getInstanceCount() will not be printed.

Option D) A runtime error occurs when line 25 executes. - This option is incorrect because there are no runtime errors in the given code. The error occurs during compilation, not at runtime.

Option E) Compilation fails because of an error on line 28. - This option is incorrect because the error in the code is on line 5, not line 28. The error is related to the accessibility of the counter variable in the static method getInstanceCount().

The correct answer is A) Compilation of class A fails. This option is correct because there is an error in the code that prevents it from compiling.

class Eggs { int doX(Long x, Long y) { return 1; } int doX(long... x) { return 2; } int doX(Integer x, Integer y) { return 3; } int doX(Number n, Number m) { return 4; } public static void main(String[] args) { new Eggs().go(); } void go() { short s = 7; System.out.print(doX(s,s) + " "); System.out.println(doX(7,7)); } } What is the result?

  1. 1 1

  2. 2 1

  3. 3 1

  4. 4 1

  5. 2 3

  6. 4 3


Correct Option: F

You read the following statement in a Java program that compiles and executes. submarine.dive(depth); What can you say for sure?

  1. depth must be an int

  2. dive must be a method.

  3. dive must be the name of an instance field.

  4. submarine must be the name of a class

  5. submarine must be a method.


Correct Option: B

Given: 11.classA { 12. public void process() { System.out.print(”A “); } } 13. class B extends A { 14. public void process() throws RuntimeException { 15. super.process(); 16. if (true) throw new RuntimeException(); 17. System.out.print(“B”); }} 18. public static void main(String[] args) { 19. try { ((A)new B()).process(); } 20. catch (Exception e) { System.out.print(”Exception “); } 21. } What is the result?

  1. Exception

  2. A Exception

  3. A Exception B

  4. A B Exception

  5. Compilation fails because of an error in line 14.

  6. Compilation fails because of an error in line 19.


Correct Option: B
  1. rbo = new ReallyBigObject(); 12. // more code here 13. rbo = null; 14. /* insert code here */ Which statement should be placed at line 14 to suggest that the virtual machine expend effort toward recycling the memory used by the object rbo?
  1. System.gc();

  2. Runtime.gc();

  3. System.freeMemory();

  4. Runtime.getRuntime().growHeap();

  5. Runtime.getRuntime().freeMemory();


Correct Option: A
  1. public class Threads5 { 2. public static void main (String[] args) { 3. new Thread(new Runnable() { 4. public void run() { 5. System.out.print("bar"); 6. }}).start(); 7. } 8. } What is the result?
  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. import java.util.*; 2. 3. public class LetterASort{ 4. public static void main(String[] args) { 5. ArrayList strings = new ArrayList(); 6. strings.add("aAaA"); 7. strings.add("AaA"); 8. strings.add("aAa"); 9. strings.add("AAaa"); 10. Collections.sort(strings); 11. for (String s : strings) { System.out.print(s + " "); } 12. } 13. } What is the result?
  1. Compilation fails.

  2. aAaA aAa AAaa AaA

  3. AAaa AaA aAa aAaA

  4. AaA AAaa aAaA aAa

  5. aAa AaA aAaA AAaa

  6. An exception is thrown at runtime


Correct Option: C
Explanation:

To solve this question, the user needs to know the basics of Java programming language and the concept of ArrayList and Collections.sort() method.

The program initializes an ArrayList of strings and adds four different string elements to it. Then, it uses the Collections.sort() method to sort the elements of the ArrayList in ascending order. Finally, it prints the sorted list of strings.

When comparing strings, the sort() method uses lexicographic ordering, meaning that it compares the strings character by character based on their Unicode values.

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

A. Compilation fails. This option is incorrect. The program is syntactically correct and will compile without errors.

B. aAaA aAa AAaa AaA. This option is incorrect. The sort() method sorts the strings in ascending order, so the correct output should start with AAaa, followed by AaA, then aAa, and finally aAaA.

C. AAaa AaA aAa aAaA. This option is correct. The sort() method sorts the strings in ascending order based on their Unicode values. Therefore, the correct output is AAaa AaA aAa aAaA.

D. AaA AAaa aAaA aAa. This option is incorrect. The correct output should start with AAaa, followed by AaA, then aAa, and finally aAaA.

E. aAa AaA aAaA AAaa. This option is incorrect. The correct output should start with AAaa, followed by AaA, then aAa, and finally aAaA.

F. An exception is thrown at runtime. This option is incorrect. There are no statements in the program that could throw any exceptions.

Therefore, the correct answer is:

The Answer is: C. AAaa AaA aAa aAaA

- Hide questions