0

programming languages Online Quiz - 182

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

What is the output of the following code snippet ?? void main() { int a = (2,4); printf("%d", a); }

  1. 2

  2. compiler error

  3. runtime error

  4. 4

  5. 0


Correct Option: D

What is the output of following code snippet ? void main() { printf(printf("anna")); }

  1. anna

  2. compile time error

  3. anna4

  4. non of the above

  5. garbage


Correct Option: C

What is the output of following code snippet ? void main() { int arr[5]={10,20,30,40,50}; printf("%d",(arr+2)); }

  1. 30

  2. 20

  3. syntax error

  4. garbage


Correct Option: D

What is the output ?? main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }

  1. I hate U

  2. I love U

  3. non of the above

  4. error


Correct Option: A

What is the output ? main() { char *p; printf("%d %d ",sizeof(*p),sizeof(p)); }

  1. 2 2

  2. 1 2

  3. compiler error

  4. non of the above


Correct Option: B

Which of the access modifiers a top level class may have?

  1. protected

  2. private

  3. public

  4. friendly


Correct Option: C
Explanation:

To answer this question, the user needs to know the concept of access modifiers in object-oriented programming. Access modifiers are keywords that determine the accessibility of classes, fields, methods, and other members of a class from other classes and packages. A top-level class is a class that is not a nested class or an inner class.

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

A. protected: A protected access modifier can be applied to class members but not to top-level classes. Protected members can be accessed from within the class itself, its subclasses, and classes in the same package.

B. private: A private access modifier can be applied to class members but not to top-level classes. Private members can only be accessed from within the class itself.

C. public: A public access modifier can be applied to top-level classes, as well as class members. Public members can be accessed from anywhere in the program.

D. friendly: There is no such access modifier as "friendly". It may be a typo or a misunderstanding of the default access modifier, which is applied when no access modifier is specified. The default access modifier allows access to members from within the same package.

Therefore, the correct answer is:

The Answer is: C

How many bytes are used to represent the primitive data type int in Java ?

  1. 4

  2. 2

  3. 8

  4. The number of bytes to represent an int is compiler dependent.


Correct Option: A

Given a one dimensional array arr, what is the correct way of getting the number of elements in arr.

  1. arr.length() – 1

  2. arr.length()

  3. arr.length - 1

  4. arr.length


Correct Option: D
Explanation:

The correct way of getting the number of elements in a one-dimensional array is:

A. arr.length

The option A, arr.length, returns the length of the array, which represents the number of elements in the array. This is the correct way to get the number of elements in the array.

The other options are incorrect:

B. arr.length() – 1: This option subtracts 1 from the length of the array, which does not give the correct count of elements in the array.

C. arr.length(): Adding parentheses after length is not necessary and will result in a syntax error.

D. arr.length - 1: This option subtracts 1 from the length of the array, which does not give the correct count of elements in the array.

So, the correct answer is: A. arr.length

What is the legal range of values for a variable declared as a byte?

  1. 0 to 256

  2. -128 to 127

  3. -127 to 128

  4. 0 to 255


Correct Option: B

In the following class definition, which is the first line (if any) that causes a compilation error. Select the one correct answer.public class test { public static void main(String args[]) { char c; int i; c = 'A'; // 1 i = c; //2 c = i + 1; //3 c++; //4 }}

  1. The line labeled 1.

  2. The line labeled 2.

  3. The line labeled 3.

  4. All the lines are correct and the program compiles.


Correct Option: C

Which of the following are keywords in Java?

  1. synchronized

  2. friend

  3. implement

  4. throws


Correct Option: A,D

What gets printed when the following program is compiled and run? class test { public static void main(String args[]) { int i,j,k,l=0; k = l++; j = ++k; i = j++; System.out.println(i); } }

  1. 1

  2. 3

  3. 2

  4. 0


Correct Option: A

AI Explanation

To determine the output of the program, let's go through the code step by step:

  1. Initialize the variables i, j, k, and l to 0.
  2. Assign the value of l to k and then increment the value of l by 1. So, k becomes 0 and l becomes 1.
  3. Increment the value of k by 1 and assign the result to j. So, j becomes 1 and k becomes 1.
  4. Assign the value of j to i and then increment the value of j by 1. So, i becomes 1 and j becomes 2.
  5. Print the value of i which is 1.

Therefore, the output of the program will be 1.

Hence, the correct answer is option A.

What gets printed when the following program is compiled and run. protected class example {public static void main(String args[]) {String test = "abc";test = test + test;System.out.println(test);}}

  1. The program does not compile because statement "test = test + test" is illegal.

  2. The program prints "abc"

  3. The program prints "abcabc"

  4. The class does not compile because the top level class cannot be protected.


Correct Option: D

What gets printed when the following program is compiled and run. Select the one correct answer class B { public static void main(String[] args) { int a=10; if(a=10) System.out.println("tomcat"); else System.out.println("root"); } }

  1. tomcat

  2. root

  3. Compile time Error

  4. Run Time Error


Correct Option: C

package inheritance; class TestInheritance1 { public int empId =100; public String name ="tcs" ; } package inheritance1; import inheritance.TestInheritance1; public class TestInheritance_1 extends TestInheritance1 { public static void main(String[] args) { System.out.println("EmpId is "+empId); } }

  1. EmpId is 100

  2. Compiler error

  3. EmpId is 0

  4. Exception


Correct Option: B

package inheritance; public class TestInheritance2 { public int empId =100; public String name ="tcs" ; } package inheritance1; import inheritance.TestInheritance2; public class TestInheritance_2 extends TestInheritance2 { void printEmpId() { System.out.println("EmpId is "+empId); } public static void main(String[] args) { TestInheritance_2 T_2 = new TestInheritance_2(); T_2.printEmpId(); } }

  1. Compiler Error

  2. EmpId is 0

  3. EmpId is 100

  4. Exception


Correct Option: C

package inheritance; public class TestInheritance3 { protected int p = 100; } package inheritance1; import inheritance.TestInheritance3; public class TestInheritance_3 extends TestInheritance3 { void printP() { System.out.println("p= "+p); } public static void main(String[] args) { TestInheritance_3 T_3 = new TestInheritance_3(); T_3.printP(); } }

  1. p= 0

  2. p= 100

  3. Compiler Error

  4. Exception


Correct Option: B

package inheritance; class TestInheritance4 { String name ="TCS"; } package inheritance1; import inheritance.TestInheritance4; public class TestInheritance_4 extends TestInheritance4 { void printName() { System.out.println("name ="+name); } public static void main(String[] args) { TestInheritance_4 T_4 = new TestInheritance_4(); T_4.printName(); } }

  1. Compiler Error

  2. name = TCS

  3. name = null

  4. Exception


Correct Option: A

package inheritance; public class TestInheritance7{ protected int p = 100; } package inheritance1; import inheritance.TestInheritance7; public class TestInheritance_7 extends TestInheritance7{ void printP() { TestInheritance7 T7 = new TestInheritance7(); System.out.println("p = "+T7.p); } public static void main(String[] args) { TestInheritance_7 T_7 = new TestInheritance_7(); T_7.printP(); } }

  1. p = 100

  2. Compiler error

  3. p = 0

  4. Exception


Correct Option: B
package inheritance;
class TestInheritance4 {
 String name = "JAVA";
}
package inheritance1;
import
import inheritance.TestInheritance4;
public class TestInheritance_4 extends TestInheritance4 {
 void printName() {
  System.out.println("name =" + name);
 }
 public static void main(String[] args) {
  TestInheritance_4 T_4 = new TestInheritance_4();
  T_4.printName();
 }
}
  1. name = JAVA

  2. Compiler Error

  3. Exception

  4. name = null


Correct Option: B

AI Explanation

To answer this question, we need to understand the concept of packages and inheritance in Java.

In the given code, there are two packages: inheritance and inheritance1. The TestInheritance4 class is defined in the inheritance package, and the TestInheritance_4 class is defined in the inheritance1 package.

The TestInheritance_4 class extends the TestInheritance4 class, which means it inherits the name field from the parent class.

Now, let's go through the options:

Option A) name = JAVA - This option is incorrect. Since the TestInheritance_4 class extends the TestInheritance4 class, it can access the name field. However, the name field is not explicitly assigned any value in the TestInheritance_4 class, so its default value of null will be printed.

Option B) Compiler Error - This option is correct. The code will give a compiler error because the import statement in the TestInheritance_4 class is incomplete. It is missing the required package name. The correct import statement should be import inheritance.TestInheritance4;.

Option C) Exception - This option is incorrect. There is no exception thrown in the code.

Option D) name = null - This option is incorrect. Although the name field is not explicitly assigned any value in the TestInheritance_4 class, the default value of null is not printed. The code will give a compiler error before reaching the point where printName() is called.

Therefore, the correct answer is B) Compiler Error.

- Hide questions