Tag: programming languages

Questions Related to programming languages

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 which declarations are valid, we need to consider the rules for naming variables in programming languages like C++.

A. int $x; - This declaration is valid. In C++, variable names can start with a letter, underscore, or a dollar sign.

B. int 123; - This declaration is invalid. Variable names cannot start with a digit.

C. int _123; - This declaration is valid. Variable names can start with an underscore.

D. int #dim; - This declaration is invalid. Variable names cannot start with a special character like a hash or pound sign.

E. int %percent; - This declaration is invalid. Variable names cannot start with a special character like a percent sign.

F. int central_sales_region_Summer_2005_gross_sales; - This declaration is valid. Variable names can contain letters, numbers, and underscores.

Therefore, the valid declarations are A, C, and F.

  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

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 TestFile { public static void main(String[] args) { File file = new File("test_file.txt"); System.out.print(" Fle Name = " + file.getName()); file.delete(); file.renameTo(new File("test_file2.txt")); System.out.print(" Fle Name = " + file.getName()); } }

  1. Null pointer Exception

  2. Fle Name = test_file.txt Fle Name = test_file.txt

  3. Compiler Error

  4. Fle Name = test_file.txt Fle Name = test_file2.txt


Correct Option: B

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