Tag: programming languages

Questions Related to programming languages

How old are you 5

  1. 1

  2. 2

  3. 3

  4. 4


Correct Option: C

How old are you 7

  1. 1

  2. 2

  3. 3

  4. 4


Correct Option: A
  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.