0

programming languages Online Quiz - 44

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

#include main() { char a[4]="HELLO"; printf("%s",a); }

  1. O

  2. H

  3. HELLO

  4. Compilation Error


Correct Option: D

#include main (){ unsigned int a = 0xf0ad; char *c = (char *) &a; printf("value @c: %x",*c); }

  1. value @c: ffffffad

  2. value @c: 0xf0ad

  3. compilation error

  4. syntax error


Correct Option: A

#include main(){ extern int i; i=23; printf("%d",i); }

  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.

#include main (){ char c[] = "Hello"; char *p = "Hello"; printf("Size of c: %d Addr of c:%x\n",sizeof(c),c); }

  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

Which of the below symbol is used to make comments in C language?

  1. //

  2. #

  3. !!


Correct Option: B

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.

class Plane { static String s = "-"; public static void main(String[] args) { new Plane().s1(); System.out.println(s); } void s1() { try { s2(); } catch (Exception e) { s += "c"; } } void s2() throws Exception { s3(); s += "2"; s3(); s += "2b"; } void s3() throws Exception { throw new Exception(); } }

  1. -

  2. -c

  3. -c2

  4. -2c

  5. -c22b

  6. -2c2b


Correct Option: B
  1. class Loopy { 2. public static void main(String[] args) { 3. int[] x = {7,6,5,4,3,2,1}; 4. // insert code here 5. System.out.print(y + " "); 6. } 7. } }
  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

Given: 3. public class Circles { 4. public static void main(String[] args) { 5. int[] ia = {1,3,5,7,9}; 6. for(int x : ia) { 7. for(int j = 0; j < 3; j++) { 8. if(x > 4 && x < 8) continue; 9. System.out.print(" " + x); 10. if(j == 1) break; 11. continue; 12. } 13. continue; 14. } 15. } 16. }

  1. 1 3 9

  2. 5 5 7 7

  3. 1 3 3 9 9

  4. 1 1 3 3 9 9

  5. 1 1 1 3 3 3 9 9 9

  6. Compilation fails


Correct Option: D

AI Explanation

To determine the output of the given code, let's go through each option:

Option A) 1 3 9

  • This option is incorrect because it only includes the values 1, 3, and 9. It does not take into account the values 5 and 7.

Option B) 5 5 7 7

  • This option is incorrect because it includes duplicate values. The code does not print the same value multiple times in a row.

Option C) 1 3 3 9 9

  • This option is incorrect because it includes duplicate values. The code does not print the same value multiple times in a row.

Option D) 1 1 3 3 9 9

  • This option is correct. Let's go through the code step by step to understand why:
    • The array ia contains the values {1, 3, 5, 7, 9}.
    • The outer for loop iterates over each element x in ia.
    • The inner for loop iterates three times.
    • For each iteration of the inner for loop, the code checks if x is greater than 4 and less than 8. If it is, the continue statement is executed, skipping the rest of the inner loop.
    • If x is not greater than 4 and less than 8, the code prints the value of x.
    • After the first iteration of the inner loop, the code encounters a break statement when j is equal to 1, which stops the inner loop.
    • The outer loop then continues to the next element in ia.
    • The code prints the values 1, 1, 3, 3, 9, 9, which matches option D.

Option E) 1 1 1 3 3 3 9 9 9

  • This option is incorrect because it includes duplicate values. The code does not print the same value multiple times in a row.

Option F) Compilation fails

  • This option is incorrect. The code does not have any syntax errors and will compile successfully.

The correct answer is option D) 1 1 3 3 9 9. This option is correct because it accurately represents the output of the given code.

  1. public class Wind { 4. public static void main(String[] args) { 5. foreach: 6. for(int j=0; j<5; j++) { 7. for(int k=0; k< 3; k++) { 8. System.out.print(" " + j); 9. if(j==3 && k==1) break foreach; 10. if(j==0 || j==2) break; 11. } 12. } 13. } 14. }
  1. 0 1 2 3

  2. 1 1 1 3 3

  3. 0 1 1 1 2 3 3

  4. 1 1 1 3 3 4 4 4

  5. 0 1 1 1 2 3 3 4 4 4

  6. compilation fails


Correct Option: C

What will be printed by the code below? my $val = 'x'; print ref($val);

  1. SCALAR

  2. empty value

  3. STRING

  4. "not a reference"


Correct Option: B

AI Explanation

To answer this question, let's break down the code and go through each option:

The code initializes a scalar variable $val with a value of 'x'.

The ref() function in Perl is used to determine the reference type of a variable. If a variable is not a reference, ref() will return an empty value.

Option A) SCALAR - This option is incorrect because ref() does not return the reference type of the variable in this case. It will return an empty value.

Option B) empty value - This option is correct because ref() will return an empty value since $val is not a reference.

Option C) STRING - This option is incorrect because ref() does not return the reference type of the variable in this case. It will return an empty value.

Option D) "not a reference" - This option is incorrect because the code does not explicitly output the string "not a reference". It will return an empty value.

The correct answer is B. The code will print an empty value.

What will be printed by the following code? package A; sub new { return bless {}; } sub foo { return 'A'; } package B; use base 'A'; sub foo { return 'B'; } package main; my $obj = B->new(); print $obj->foo(), "\n";

  1. A

  2. B

  3. the code will fail

  4. None of the above


Correct Option: A

What will be the value of $val? my $str = 'aa bb cccc'; my $val = () = $str =~ /\w+/g;

  1. undef

  2. aa

  3. cccc

  4. 3

  5. 0


Correct Option: D

AI Explanation

To determine the value of $val, let's break down the code step by step:

my $str = 'aa bb cccc';
my $val = () = $str =~ /\w+/g;

In the second line, the regular expression /\w+/g is used to match one or more word characters in the string $str. The g modifier indicates that the regular expression should be executed globally, meaning it will match all occurrences in the string.

The $str =~ /\w+/g expression returns a list of all the matches found in the string. In this case, the matches are 'aa', 'bb', and 'cccc'.

The () surrounding the assignment operator = in the line $val = () = $str =~ /\w+/g is known as the list context. It forces the right-hand side of the assignment to be evaluated in a list context.

The () = $str =~ /\w+/g expression assigns the matches to an empty list (). However, the assignment is happening in a scalar context, so the empty list is being evaluated as the number of elements in the list.

Therefore, the value of $val will be the number of matches found, which is 3.

The correct answer is D) 3.

What gets printed? my $a = 123; my $b = 0123; if ($a == $b) { print "same"; } else { print "different"; }

  1. same

  2. different

  3. the code is ill-formed

  4. None of the above


Correct Option: B
- Hide questions