Programming Questions

Q:

What will be output of following c code?

#include <stdio.h>
int main()

{
    int x=123;
    int i={
         printf("c" "++")
    };
    for(x=0;x<=i;x++){
         printf("%x ",x);
    }
    return 0;

}

Answer

Output: c++0 1 2 3

Explanation: First printf function will print: c++ and return 3 to variable i.For loop will execute three time and printf function will print 0, 1, 2 respectively.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

63 44298
Q:

main()

{

char *p; p = "Hello";

printf ("%cn", *&*p);

}

A) H B) Hello
C) Compilation error D) H E L L O
 
Answer & Explanation Answer: A) H

Explanation:

* is a dereference operator & is a reference operator. They can be applied any number of times provided it is meaningful. Here p points to the first character in the string "Hello". *p dereferences it and so its value is H. Again & references it to an address and * dereferences it to the value H

Report Error

View Answer Report Error Discuss

Filed Under: Programming
Job Role: Software Architect

19 30359
Q:

main()

{

char s[ ] = "man";

int i;

for( i=0; s[ i ]; i++)

printf( "n%c%c%c%c", s[ i ], *(s+i), *(i+s), i[s] );

}

A) mmmm aaaa nnnn B) aaaa mmmm nnnn
C) nnnn aaaa mmmm D) None
 
Answer & Explanation Answer: A) mmmm aaaa nnnn

Explanation:

s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea.Generally array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as s[i].

Report Error

View Answer Report Error Discuss

Filed Under: Programming

24 26796
Q:

#define clrscr() 100

main()

{

clrscr();

printf( "%dn", clrscr() );

}

A) 100 B) 0
C) Compilation error D) Exception occurs
 
Answer & Explanation Answer: A) 100

Explanation:

Preprocessor executes as a seperate pass before the execution of the compiler. So textual replacement of clrscr() to 100 occurs.The input program to compiler looks like this :

main ()

{

100;

printf("%d\n",100);

}

Note: 100; is an executable statement but with no action. So it doesn't give any problem.

Report Error

View Answer Report Error Discuss

Filed Under: Programming

13 22820
Q:

enum colors {BLACK,BLUE,GREEN}

main()

{

printf( "%d..%d..%d", BLACK, BLUE, GREEN );

return(1);

}

A) 1..2..3 B) 0..1..2
C) 1..1..1 D) 0..0..0
 
Answer & Explanation Answer: B) 0..1..2

Explanation:

enum assigns numbers starting from 0, if not explicitly defined.

Report Error

View Answer Report Error Discuss

Filed Under: Programming

8 19006
Q:

Which of the following is not a valid escape code?

A) " B) \
C) ' D) =
 
Answer & Explanation Answer: D) =

Explanation:

The valid escape codes in programming languages are ', ", \, n, or t are the valid escape codes. 

 

For example, the sequence \n usually represents a newline, while the escape sequence \\ represents a backslash, \t usually represents a newtab.

Report Error

View Answer Report Error Discuss

39 18901
Q:

What is the output of this C code?

   #include <stdio.h>
    int main()
    {
        int a = 1, b = 1;
        switch (a)
        {
        case a*b:
            printf("yes ");
        case a-b:
            printf("non");
            break;
        }
    }

A) yes B) no
C) Compile time error D) yes no
 
Answer & Explanation Answer: C) Compile time error

Explanation:
Report Error

View Answer Report Error Discuss

Filed Under: Programming

7 18571
Q:

What will be output when you will execute following c code?

#include <stdio.h>
void main()

{
    signed int a = -1;
    unsigned int b = -1u;
    if(a == b)
         printf( "The Lord of the Rings" );
    else
         printf( "American Beauty" );
}

A) The Lord of the Rings B) American Beauty
C) Compilation error: Cannot compare signed number with unsigned number D) Warning: Illegal operation
 
Answer & Explanation Answer: A) The Lord of the Rings

Explanation:
Report Error

View Answer Report Error Discuss

Filed Under: Programming

11 18184