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

66 44884
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

20 33388
Q:

public abstract interface Frobnicate { public void twiddle(String s); } 

Which is a correct class?

A) public abstract class Frob implements Frobnicate { public abstract void twiddle(String s) { } } B) public abstract class Frob implements Frobnicate { }
C) public class Frob extends Frobnicate { public void twiddle(Integer i) { } } D) public class Frob implements Frobnicate { public void twiddle(Integer i) { } }
 
Answer & Explanation Answer: B) public abstract class Frob implements Frobnicate { }

Explanation:

B is correct, an abstract class need not implement any or all of an interface’s methods

Report Error

View Answer Report Error Discuss

Filed Under: Programming

3 30101
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 29179
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 24639
Q:

What will be output of following program?


#include <stdio.h>
int main()

{
   void (*p)();
   int (*q)();
   int (*r)();
   p = clrscr;
   q = getch;
   r = puts;
  (*p)();
  (*r)("www.sawaal.com");
  (*q)();
  return 0;
}

A) NULL B) www.sawaal.com
C) Compilation error D) None of above
 
Answer & Explanation Answer: B) www.sawaal.com

Explanation:

p is pointer to function whose parameter is void and return type is also void. r and q is pointer to function whose parameter is void and return type is int . So they can hold the address of such function.

Report Error

View Answer Report Error Discuss

Filed Under: Programming

2 22909
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

40 22766
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 21782