0

programming languages Online Quiz - 193

Description: programming languages Online Quiz - 193
Number of Questions: 20
Created by:
Tags: programming languages
Attempted 0/20 Correct 0 Score 0
  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

Given:

1. enum A { A } 
2. class E2 { 
3.   enum B { B } 
4.   void C() { 
5.     enum D { D } 
6.   } 
7. } 

Which statements are true? (Choose all that apply.)

  1. If only line 1 is removed the code compiles.

  2. If only line 3 is removed the code compiles.

  3. If only line 5 is removed the code compiles.

  4. If lines 1 and 3 are removed the code compiles

  5. If lines 1, 3 and 5 are removed the code compiles.


Correct Option: C,E

AI Explanation

To answer this question, we need to understand the rules and restrictions regarding the declaration and usage of enums in Java.

In Java, an enum is a special data type that allows for a variable to be a set of predefined constants. Each constant is defined within the enum. Enums can only be declared at the top level of a class or interface, or within a class (as a member) but not inside a method.

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

Option A) If only line 1 is removed the code compiles. This option is incorrect because removing line 1 would remove the declaration of enum A. Since enum A is not used in the code, its removal does not affect the compilation.

Option B) If only line 3 is removed the code compiles. This option is incorrect because removing line 3 would remove the declaration of enum B. However, enum B is used within the class E2, so its removal would result in a compilation error.

Option C) If only line 5 is removed the code compiles. This option is correct because removing line 5 would remove the declaration of enum D, which is a local enum declared inside the method C(). Since enum D is not used outside of the method, its removal does not affect the compilation.

Option D) If lines 1 and 3 are removed the code compiles. This option is incorrect because removing line 1 would remove the declaration of enum A, and removing line 3 would remove the declaration of enum B. Since enum B is used within the class E2, its removal would result in a compilation error.

Option E) If lines 1, 3, and 5 are removed the code compiles. This option is correct because removing line 1 would remove the declaration of enum A, removing line 3 would remove the declaration of enum B, and removing line 5 would remove the declaration of enum D. Since enum B is used within the class E2, its removal would result in a compilation error. However, since enum D is a local enum declared inside the method C(), its removal does not affect the compilation.

Therefore, the correct answer is: C. If only line 5 is removed the code compiles. E. If lines 1, 3, and 5 are removed the code compiles.

package testing; public class SimpleClone implements Cloneable { int number = 100; int[] intArray = new int[10]; public SimpleClone() { int number = 100; for (int i = 0; i < intArray.length; i++) { intArray[i] = number; number++; } } public static void main(String[] args) { SimpleClone testclone1 = new SimpleClone(); SimpleClone testclone2; try { testclone2 = (SimpleClone) testclone1.clone(); testclone1.intArray[0] = 1200; testclone2.intArray[0] = 1400; testclone1.number = 1200; System.out.print(" testclone1 intArray[0] = " + testclone1.intArray[0]); System.out.print(" testclone1one2 intArray[0] = " + testclone2.intArray[0]); System.out.print(" testclone1 value of number = " + testclone1.number); System.out.print(" testclone2 value of number = " + testclone2.number); } catch (CloneNotSupportedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

  1. testclone1 intArray[0] = 1400 testclone1one2 intArray[0] = 1400 testclone1 value of number = 1200 testclone2 value of number = 100

  2. testclone1 intArray[0] = 1200 testclone1one2 intArray[0] = 1400 testclone1 value of number = 1200 testclone2 value of number = 100

  3. testclone1 intArray[0] = 1400 testclone1one2 intArray[0] = 1400 testclone1 value of number = 1200 testclone2 value of number = 1200

  4. Compiler Error


Correct Option: A

package io; import java.io.*; public class TestFile1 { public static void main(String[] args) { //No file exists by the name test_file.txt File file = new File("test_file.txt"); System.out.print(" Fle Name = "+file.getName()); System.out.print(" File Status = "+file.exists()); } }

  1. Fle Name = test_file.txt File Status = false

  2. Compiler Error

  3. Null Pointer Exception

  4. Fle Name = test_file.txt File Status = true


Correct Option: A

package inheritance; class X { Y b = new Y(); X() { System.out.print("X"); } } class Y { Y() { System.out.print("Y"); } } public class Z extends X { Y y = new Y(); Z() { System.out.print("Z"); } public static void main(String[] args) { new Z(); } }

  1. YXYZ

  2. Z

  3. ZX

  4. ZXYY


Correct Option: A

Signature has to be the same for overloading.

  1. True

  2. False


Correct Option: B

Overriding is an example of runtime polymorphism.

  1. True

  2. False


Correct Option: A

Unreachable statements produce a compile-time error.

  1. True

  2. False


Correct Option: A

Integer division by zero throws an exception.

  1. True

  2. False


Correct Option: A

package simplejava; public class TestConstructor2 { int p; TestConstructor2() { System.out.print(" I am in Constructor"); } TestConstructor2(int p) { this(); this.p = p; System.out.print(" p = " + p); } public static void main(String[] args) { TestConstructor2 tp1 = new TestConstructor2(10); } }

  1. I am in Constructor p = 10

  2. I am in Constructor p = 0

  3. Compiler Error

  4. Exception


Correct Option: A

package simplejava;public class TestMain { TestMain() { System.out.println("I am in Constructor"); } TestMain(String message) { System.out.println("Message " + message); } public static void main(String[] args) { TestMain tm = new TestMain(); } public static void main(String args) { TestMain tm = new TestMain("Hello"); }}

  1. Message Hello

  2. I am in Constructor

  3. Compiler Error

  4. Exception


Correct Option: B

package simplejava;public class TestMain1 { TestMain1() { System.out.println("I am in Constructor"); } TestMain1(String message) { System.out.println("Message " + message); } public void main(String[] args) { TestMain1 nm = new TestMain1(); TestMain1 nm1 = new TestMain1("Hello"); }}

  1. Compiler Error

  2. Exception

  3. I am in Constructor

  4. Message Hello


Correct Option: B

Are you allowed to have more than one top-level(non-inner) class definition per source file?

  1. True

  2. False


Correct Option: A

Assume the bit pattern of byte x is: 10110001. What will the sign of x be after x>>2?

  1. postive

  2. negative

  3. can't be find

  4. none of the above


Correct Option: B

A class can define two methods with the same name as long as the return types are different

  1. True

  2. False


Correct Option: B

All exceptions inherit from

  1. java.lang.Exception

  2. java.lang.Error

  3. java.lang.Throwable

  4. none of the above


Correct Option: C
- Hide questions