Tag: programming languages

Questions Related to programming languages

  1. 23

  2. Prints some address

  3. Compilation error

  4. Run time error


Correct Option: C

#include main(){ static int a[20]; int i=0; a[i]=i++; printf("%d %d %d",a[0],a[1],i); }

  1. 0 0 0

  2. 0 1 0

  3. 0 0 1

  4. 0 1 1


Correct Option: C

#include main(){ int a=2; int *p=&a; print("%x\t",p); print("%x\t",*p++); print("%x\t",p); }

  1. someaddress value someaddress

  2. value value value

  3. syntax error

  4. compilation error


Correct Option: C

main() { int a[5] = {10,20,30,40,50}; char *p; p=(char *)a; printf("%d\n",a); }

  1. some value

  2. compilation error

  3. 10

  4. 50


Correct Option: A

AI Explanation

To answer this question, let's go through the code step by step:

main() {
    int a[5] = {10,20,30,40,50};
    char *p;
    p=(char *)a;
    printf("%d\n",a);
}

In this code, an integer array a is declared and initialized with values 10, 20, 30, 40, and 50.

The variable p is declared as a character pointer.

The line p=(char *)a; casts the integer array a to a character pointer p. This means that p now points to the first element of the integer array a.

The printf("%d\n",a); statement prints the value of the integer array a. However, the format specifier %d is used to print integers, not arrays.

So, the correct format specifier to print the value of a would be %p, which is used to print pointers.

Now, let's go through the options:

A) Some value - This option is correct. When we use the incorrect format specifier %d to print the array a, it will print some unexpected value. The exact value printed will depend on the system and compiler being used.

B) Compilation error - This option is incorrect. The code will compile without any errors.

C) 10 - This option is incorrect. Since the incorrect format specifier %d is used, it will not print the value 10.

D) 50 - This option is incorrect. Since the incorrect format specifier %d is used, it will not print the value 50.

The correct answer is A) Some value, as explained above.

  1. Size of c: some value Addr of c:some address

  2. Size of c: some value Addr of c:some value

  3. Size of c: some address Addr of c:some address

  4. Compilation error


Correct Option: D

Can a variable be declared as both const and volatile

  1. True

  2. False


Correct Option: A

public abstract class AbstractTest { public int getNum() { return 45; } public abstract class Bar { public int getNum() { return 38; } } public static void main(String[] args) { AbstractTest t = new AbstractTest() { public int getNum() { return 22; } }; AbstractTest.Bar f = t.new Bar() { public int getNum() { return 57; } }; System.out.println(f.getNum() + " " + t.getNum()); } }

  1. 57 22

  2. 45 38

  3. 45 57

  4. D. An exception occurs at runtime


Correct Option: B

AI Explanation

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

Option A) 57 22 - This option is incorrect because the variable f is an instance of the inner class Bar, which overrides the getNum() method to return 57. However, the variable t is an instance of the anonymous subclass of AbstractTest, which overrides the getNum() method to return 22. Therefore, the correct output for f.getNum() and t.getNum() is 45 and 38, respectively.

Option B) 45 38 - This option is correct because the variable f is an instance of the inner class Bar, which inherits the getNum() method from the outer class AbstractTest and returns 38. The variable t is an instance of the anonymous subclass of AbstractTest, which overrides the getNum() method to return 22. Therefore, the correct output for f.getNum() and t.getNum() is 45 and 38, respectively.

Option C) 45 57 - This option is incorrect because the variable f is an instance of the inner class Bar, which inherits the getNum() method from the outer class AbstractTest and returns 38. The variable t is an instance of the anonymous subclass of AbstractTest, which overrides the getNum() method to return 22. Therefore, the correct output for f.getNum() and t.getNum() is 45 and 38, respectively, not 57.

Option D) An exception occurs at runtime - This option is incorrect because there are no exceptions thrown in the given code. The code will execute without any errors and will print the output.

The correct answer is B. The output of the code will be "45 38" because f.getNum() returns 45 and t.getNum() returns 38.

  1. for(int y : x) {

  2. for(x : int y) {

  3. int y = 0; for(y : x) {

  4. for(int y=0, z=0; z

  5. for(int y=0, int z=0; z

  6. int y = 0; for(int z=0; z


Correct Option: A,D,F