Variables Pointer Array And Files - C Programming Questions For Job Interview

1. How will you change the value of a constant variable in C?
Answer: Constant can be changed by using the pointer. Initialize a pointer to point to the value of a and then change the value using the pointer. 
#include<stdio.h>
int main()
{
const int = 5;
printf("%d", a);
int *k = (int *) &a;
*k = 10;
printf("%d", *k);
printf("%d", a);
return 0;
}

2. What is the output of the following program?
#define SQR(x) (x*x)
main()
{
int a, b = 3;
a = SQR(b + 2);
printf("%d", a);
}

a) 25
b) 11
c) Error
d) Garbage Value

Answer: b) 11.
Since it passes like (3+2) to #define, where it calculates as (3+2 * 3+2), as 1st preference is multiply & then addition, it evaluates as (3+ 2 * 3 +2) = (3+6+2)=11.


3. What is the output of the following code?
main()
{
if ((1 || 0) && (0 || 1)) {
printf("OK I am done.");
} else {
printf("OK I am gone.");
}
}
a) none of the above
b) compile error
c) OK I am gone
d) OK I am done

Answer: d)
((1 || 0) && (0 || 1)) will be:
(( 1 ) && ( 1 ))
(1 && 1) =>1: so will print: OK I am done.

4. What is the difference between structure & union?

Answer :1. Union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members.
2. In union, one block is used by all the member of the union but in case of structure, each member has its own memory space.
3. Union is best in the environment where memory is less as it shares the memory allocated. But structure cannot be implemented in shared memory.
4. As memory is shared, ambiguity is more in union, but less in structure.
5. Self-referential union cannot be implemented in any data structure, but self-referential structure can be implemented.

5. What is a wild pointer?
Answer:Wild pointer is a pointer that doesn't point to either a valid object (of the indicated type, if applicable), or to a distinguished null value, if applicable.

C Programming Basic Questions
Array in Embedded C
C Programming Viva Questions
Dangling and Wild Pointers
Variables and Pointers Volatile Or Constant
C Questions for Job Interview


6. How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?

Answer: Consider the following code in which ‘arr’ is an N-element array of pointers to functions returning pointers to functions returning pointers to char.
Code
char *(*(*arr[N]) ()) ()

7. What is the return type of the printf & scanf functions?
a) void
b) float
c) int
d) any

Answer: The return type of printf() and scanf() is int In the declaration, observe the return type is int. int printf (const char *format, ...);
int scanf (const char *format, ... );
printf() returns the number of characters printed on the console
scanf() returns the number of variables to which you are providing the input.
Following example for clear idea.
int main()
{
int i, j;
printf("\nPrinted %d charecters on stdoutn ", printf("Enter values of i & j"));
printf("\nYou entered input for %d variables", scanf("%d %d", &i, &j));
getch();
}


8. What is size of FILE data type? FILE *fp;
Answer: 
FILE *p = NULL;
printf("size of pointer = %d\n", sizeof(p));
Output:
size of pointer = 8
Pointer has size depending on the machine's architecture.

9. What is the output of the following code?
{
printf("%d, %d", sizeof('c'), sizeof(100));
}

a) 4, 100
b) 2, 100
c) 2, 2
d) 4, 4
Answer:It depends on the compiler. If compiler is treating your integer as a 2 byte then answer will be 2,2 and if compiler is treating your integer as 4 byte then answer will be 4,4

10. How to swap the content of two variables without a temporary variable?
Answer:
x = x * y;
y = x / y;
x = x / y;

11. How to print "n" in C?
Answer:
printf("\"n\"");


12. What is the output of the following code?
#define square(a) (a*a)
main()
{
printf("%d", square(4 + 5));
}

Answer: 29
4+5*4+5=4+20+5=29

13. What is the size of int in C or C++?
Options:
a) 2 byte
b) System dependent
c) Compiler dependent
Answer:c)
It is compiler dependent, in GCC we have size of 4 bytes while in Turbo-C we have it as 2 bytes.

14. What is the output of the following code?
 #include <stdio.h>
 void main() {
 int s = 0;
while (s++ < 10) {
if (s < 4 && s < 9)
continue;
printf(" %d ", s);
 }
}

1) 1 2 3 4 5 6 7 8 9
2) 1 2 3 10
3) 4 5 6 7 8 9 10
4) 4 5 6 7 8 9
Answer: 3)-The result of the expression s++ is the value of s *before* the increment, so the expression (s++ < 10) operates on the values 0 through 9.In the body of the loop, s has been incremented, so the expression (s < 4 && s < 9) operates on the values 1 through 10. When s is between 1 and 3, the ‘continue’ statement is executed and the loop repeats from the beginning, skipping the printf. So only the values 4 through 10 are written to standard output.

Related

Variables 5252116281849886140

Post a Comment

SPAMMING will not be Appreciated.

emo-but-icon

Hot in week

Recent

Comments

Our Channel

Contact Us

Name

Email *

Message *

item