250+ TOP MCQs on Sizeof Keyword and Answers

C programming questions on “Sizeof”. One shall practice these questions to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C programming questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C programming questions on “Sizeof” along with answers, explanations and/or solutions:

1. What is the sizeof(char) in a 32-bit C compiler?
a) 1 bit
b) 2 bits
c) 1 Byte
d) 2 Bytes
Answer: c
Clarification: None.

2. What will be the output of the following C code?

    #include 
    printf("%d", sizeof('a'));

a) 1
b) 2
c) 4
d) None of the mentioned
Answer: c
Clarification: None.

3. Size of an array can be evaluated by __________

(Assuming array declaration int a[10];)

a) sizeof(a);
b) sizeof(*a);
c) sizeof(a[10]);
d) 10 * sizeof(a);
Answer: a
Clarification: None.

4. What will be the output of the following C code?

  1.     #include 
  2.     union temp
  3.     {
  4.         char a;
  5.         char b;
  6.         int c;
  7.     }t;
  8.     int main()
  9.     {
  10.         printf("%d", sizeof(t));
  11.         return 0;
  12.     }

a) 1
b) 2
c) 4
d) 6
Answer: c
Clarification: None.

5. Which of the following is not an operator in C?
a) ,
b) sizeof()
c) ~
d) None of the mentioned
Answer: d
Clarification: None.

6. Which among the following has the highest precedence?
a) &
b) <<
c) sizeof()
d) &&
Answer: c
Clarification: None.

7. What is the sizeof(void) in a 32-bit C?
a) 0
b) 1
c) 2
d) 4
Answer: b
Clarification: None.

8. What type of value does sizeof return?
a) char
b) short
c) unsigned int
d) long
Answer: c
Clarification: None.

250+ TOP MCQs on General Utilities and Answers

C Multiple Choice Questions & Answers on “General Utilities – 6”.

1. What will the code display on compiling?

void funccall() 
{ 
   printf("this is funccalln"); 
}  
void main () 
{   
    atexit(funccall); 
    printf("program startsn");  
    printf("program endsn");  
}

a)

   program starts
   this is funccall
   program ends

b)

   this is funccall
   program starts
   program ends

c)

   program starts
   program ends
   this is funccall

d) error
Answer: c
Clarification: int atexit(void (*func)(void)) calls the specified function func when the program terminates. we can register termination function anywhere in the program, but it will be called at the time of the program termination.

2. What will be the output of the following C code?

int main () 
{ 
   printf("starting of programn"); 
   printf("program exitsn"); 
   exit(0);  
   printf("program endsn");  
   return(0); 
}

a)

   starting of program
   program exits
   program ends

b)

   starting of program
   program exits

c)

   starting of program
   program ends

d) error
Answer: b
Clarification: void exit(int status) function is used to terminate the calling process immediately.

3. What will the following C code do on compilation?

void main () 
{
   char com[50];  
   strcpy( com, "dir" ); 
   system(com); 
}

a) error
b) lists down all the files and directories in the current directory under windows machine
c) terminates the calling process immediately
d) calls specified function and terminates it at the end of the program
Answer: b
Clarification: int system(const char *command) function is used to pass the command name or the program name specified by command to the host environment to be executed by the command processor and returns after the command has been completed.

4. What will be the output of the following C code?

void main() 
{ 
   div_t  res;  
   res = div(34, 4); 
   printf("quotient part = %dn", res.quot); 
   printf("remainder part = %dn", res.rem); 
}

a)

    quotient part=0
    remainder part=4

b)

    quotient part=8
    remainder part=2

c)

    quotient part=4
    remainder part=0

d)

    quotient part=2
    remainder part=8

View Answer

Answer: b
Clarification: div_t div(int n, int d) used to divide n (numerator) by d (denominator). div_t is a structure defined in ,which has two members
int quot;
in rem;

 
 

5. Initial seed is ________ for the function srand(unsigned int seed).
a) 0
b) 1
c) 00
d) 01
Answer: b
Clarification: void srand(unsigned int seed);
This function returns a new sequence of pseudo-random numbers using the specified seed.Initial seed is 1.

6. Which statement is true with respect to RAND_MAX?
a) specifies value for status argument to exit indicating failure
b) specifies value for status argument to exit indicating success
c) specifies maximum value returned by rand()
d) specifies maximum value returned by srand()
Answer: c
Clarification: RAND_MAX specifies maximum value that has to returned by the function rand().
rand() function returns a pseudo-random number in the range 0 to RAND_MAX.

7. Which of the given function differs from the statement’errno is not neccessarily set on conversion error’?
a) atof()
b) atoi()
c) atol()
d) strtod()
Answer: d
Clarification: The function atoi() and atol() are similar to strtol(), atof() is similar to strtod() except that errono is not necessarily set on conversion error.

8. void free(void *p) performs which of the following functions?
a) returns pointer to allocated space for existing contents of p
b) de-allocates space to which p points
c) to abnormally terminate the program
d) no such function defined in stdlib.h
Answer: b
Clarification: void free(void *p);
This function de-allocates space to which p points(if p is not NULL).

9. Which of the given option is declared under the header file stdlib.h?
a) SEEK_CUR
b) SEEK_END
c) CLOCKS_PER_SEC
d) EXIT_SUCCESS
Answer: d
Clarification: SEEK_CUR and SEEK_END is defined under stdio.h, CLOCKS_PER_SEC is defined under time.h, EXIT_SUCCESS is defined under stdlib.h.
EXIT_SUCCESS specifies value for status argument to exit indicating success.

10. MB_CUR_MAX is not defined in stdlib.h.
a) true
b) false
Answer: b
Clarification: MB_CUR_MAX is defined under header file stdlib.h .
MB_CUR_MAX expands into a positive integer expression.It is the maximum number of bytes in a multibyte character present in the current locale.

250+ TOP MCQs on DMA Functions, Memory Leak, Dangling Pointers and Answers

C Objective Questions on “DMA Functions, Memory Leak, Dangling Pointers – 2”.

1. What will be the output of the following C code if it is executed on a 32 bit processor?

#include
#include
int main()
{
    int *p;
    p = (int *)malloc(20);
    printf("%dn", sizeof(p));
    free(p);
    return 0;
}

a) 2
b) 4
c) 8
d) Junk value
Answer: b
Clarification: The size of a pointer is 2 bytes on a 16 bit platform, 4 bytes on a 32 bit platform and 8 bytes on a 64 bit platform.

2. The number of arguments taken as input which allocating memory dynamically using malloc() is ___________
a) 0
b) 1
c) 2
d) 3
Answer: b
Clarification: An example of memory allocated using malloc():
(int*)malloc(3*sizeof(int)
It is clear from the above example that malloc() takes only one argument as input, that is the number of bytes to be allocated.

3. Suppose we have a one dimensional array, named ‘x’, which contains 10 integers. Which of the following is the correct way to allocate memory dynamically to the array ‘x’ using malloc()?
a) x=(int*)malloc(10);
b) x=(int*)malloc(10,sizeof(int));
c) x=malloc(int 10,sizeof(int));
d) x=(int*)malloc(10*sizeof(int));
Answer: d
Clarification: According to the syntax of malloc, the correct way to do the specified operation is: x=(int*)malloc(10*sizeof(int)); This operation can also be performed using calloc(). In that case, the method will be: x=(int*)calloc(10,sizeof(int));

4. What will be the error (if any) in the following C code?

#include
#include
#include
int main()
{
    char *p;
    *p = (char)calloc(10);
    strcpy(p, "HELLO");
    printf("%s", p);
    free(p);
    return 0;
}

a) No error
b) Error in the statement: strcpy(p,”HELLO”);
c) Error in the statement: *p=(char)calloc(10);
d) Error in the statement: free(p);
Answer: c
Clarification: The syntax for dynamically allocating memory using calloc() is incorrect. Hence, this code results in an error. The correct syntax for calloc is: void*calloc(size_t n, size_t size);

5. If malloc() and calloc() are not type casted, the default return type is ___________
a) void*
b) void**
c) int*
d) char*
Answer: a
Clarification: If malloc() and calloc() are not type casted, they return a pointer of the type void.

6. Pick out the correct statement with respect to the heap.
a) Local variables are stored on the heap
b) Static variables are stored on the heap
c) Heap is the data structure which is used to implement recursive function calls
d) Everything on the heap is anonymous
Answer: a
Clarification: Local variables are stored on the stack. Static variables are stored in the permanent storage area. Stack is the data structure used to implement recursive function calls. Hence, it is true that everything on heap s anonymous.

7. What will be the output of the following C code? (Given that the size of array is 4 and new size of array is 5)

#include
#include
main()
{
    int *p,i,a,b;
    printf("Enter size of array");
    scanf("%d",&a);
    p=(int*)malloc(a*sizeof(int));
    for(i=0;i<a;i++)
    printf("%dn",i);
    printf("Enter new size of array");
    scanf("%d",&b);
    realloc(p,b);
    for(i=0;i<b;i++)
    printf("%dn",i);
    free(p);
}

a)

   1234
   12345

b) Error
c)

   0123
   01234

d)

   0123
   12345

View Answer

Answer: c
Clarification: In the above code, we are reallocating memory. When the size of the array is 4, the output is 0123. When the size of the array is changed to 5, the output is 01234. Hence, the output is:
0123
01234

 
 

8. When the pointer is NULL, then the function realloc is equivalent to the function ___________
a) malloc
b) calloc
c) free
d) alloc
Answer: a
Clarification: If pointer is NULL, the call to the function realloc is equal to malloc(size), for any value of size. If size is equal to zero, then the pointer is not NULL and the call is equivalent to free(pointer).

9. Garbage collector frees the programmer from worrying about ___________
a) Dangling pointers
b) Creating new objects
c) Memory leak
d) Segmentation errors
Answer: c
Clarification: A garbage collector is a program that automatically removes unwanted data held temporarily in the memory during processing. Hence it frees the programmer from worrying about memory leaks.

10. If the space in memory allocated by malloc is not sufficient, then an allocation fails and returns ___________
a) NULL pointer
b) Zero
c) Garbage value
d) The number of bytes available
Answer: a
Clarification: A NULL pointer is returned when the memory allocated by malloc dynamically is insufficient.

250+ TOP MCQs on Declarations and Answers

C help anyone preparing for TCS and other companies C interviews. One should practice these Objective Questions and answers continuously for 2-3 months to clear TCS interviews on C Programming language.

Here is a listing of C questions and puzzles on “Declarations” along with answers, explanations and/or solutions:

1. Which of the following declaration is illegal?
a) char *str = “Best C programming classes by ”;
b) char str[] = “Best C programming classes by ”;
c) char str[20] = “Best C programming classes by ”;
d) char[] str = “Best C programming classes by ”;
Answer: d
Clarification: char[] str is a declaration in Java, but not in C.

2. Which keyword is used to prevent any changes in the variable within a C program?
a) immutable
b) mutable
c) const
d) volatile
Answer: c
Clarification: const is a keyword constant in C program.

3. Which of the following is not a pointer declaration?
a) char a[10];
b) char a[] = {‘1’, ‘2’, ‘3’, ‘4’};
c) char *str;
d) char a;
Answer: d
Clarification: Array declarations are pointer declarations.

4. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int k = 4;
  5.         float k = 4;
  6.         printf("%d", k)
  7.     }

a) Compile time error
b) 4
c) 4.0000000
d) 4.4
Answer: a
Clarification: Since the variable k is defined both as integer and as float, it results in an error.
Output:
$ cc pgm8.c
pgm8.c: In function ‘main’:
pgm8.c:5: error: conflicting types for ‘k’
pgm8.c:4: note: previous definition of ‘k’ was here
pgm8.c:6: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
pgm8.c:7: error: expected ‘;’ before ‘}’ token

5. Which of the following statement is false?
a) A variable defined once can be defined again with different scope
b) A single variable cannot be defined with two different types in the same scope
c) A variable must be declared and defined at the same time
d) A variable refers to a location in memory
Answer: c
Clarification: It is not an error if the variable is declared and not defined. For example – extern declarations.

6. A variable declared in a function can be used in main().
a) True
b) False
c) True if it is declared static
d) None of the mentioned
Answer: b
Clarification: Since the scope of the variable declared within a function is restricted only within that function, so the above statement is false.

7. The name of the variable used in one function cannot be used in another function.
a) True
b) False
Answer: b
Clarification: Since the scope of the variable declared within a function is restricted only within that function, the same name can be used to declare another variable in another function.

contest

250+ TOP MCQs on Precedence and Order of Evaluation and Answers

C helps anyone preparing for Wipro and other companies C interviews. One should practice these Objective Questions and answers continuously for 2-3 months to clear Wipro interviews on C Programming language.

Here is a listing of C programming Objective Questions on “Precedence and Order of Evaluation” along with answers, explanations and/or solutions:

1. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int b = 5 - 4 + 2 * 5;
  5.         printf("%d", b);
  6.     }

a) 25
b) -5
c) 11
d) 16
Answer: c
Clarification: None.

2. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int b = 5 & 4 & 6;
  5.         printf("%d", b);
  6.     }

a) 5
b) 6
c) 3
d) 4
Answer: d
Clarification: None.

3. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int b = 5 & 4 | 6;
  5.         printf("%d", b);
  6.     }

a) 6
b) 4
c) 1
d) 0
Answer: a
Clarification: None.

4. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int b = 5 + 7 * 4 - 9 * (3, 2);
  5.         printf("%d", b);
  6.     }

a) 6
b) 15
c) 13
d) 21
Answer: b
Clarification: None.

5. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int h = 8;
  5.         int b = (h++, h++);
  6.         printf("%d%dn", b, h);
  7.     }

a) 10 10
b) 10 9
c) 9 10
d) 8 10
Answer: c
Clarification: None.

6. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int h = 8;
  5.         int b = h++ + h++ + h++;
  6.         printf("%dn", h);
  7.     }

a) 9
b) 10
c) 12
d) 11
Answer: d
Clarification: None.

7. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int h = 8;
  5.         int b = 4 * 6 + 3 * 4 < 3 ? 4 : 3;
  6.         printf("%dn", b);
  7.     }

a) 3
b) 33
c) 34
d) Run time error
Answer: a
Clarification: None.

8. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int a = 2 + 3 - 4 + 8 -  5 % 4;
  5.         printf("%dn", a);
  6.     }

a) 0
b) 8
c) 11
d) 9
Answer: b
Clarification: None.

9. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         char a = '0';
  5.         char b = 'm';
  6.         int c = a && b || '1';
  7.         printf("%dn", c);
  8.     }

a) 0
b) a
c) 1
d) m
Answer: c
Clarification: None.

10. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         char a = 'A';
  5.         char b = 'B';
  6.         int c = a + b % 3 - 3 * 2;
  7.         printf("%dn", c);
  8.     }

a) 65
b) 58
c) 64
d) 59
Answer: d
Clarification: None.

250+ TOP MCQs on Functions Returning Non-integers and Answers

C helps anyone preparing for Cisco and other companies C interviews. One should practice these Objective Questions and answers continuously for 2-3 months to clear Cisco interviews on C Programming language.

Here is a listing of C language Objective Questions on “Functions Returning Non-integers” along with answers, explanations and/or solutions:

1. What is the return-type of the function sqrt()?
a) int
b) float
c) double
d) depends on the data type of the parameter
Answer: c
Clarification: None.

2. Which of the following function declaration is illegal?
a)

   double func();
   int main(){}
   double func(){}

b)

   double func(){};
   int main(){}

c)

   int main()
   {
       double func();
   }
   double func(){//statements}

d) None of the mentioned
Answer: d
Clarification: None.

3. What will be the output of the following C code having void return-type function?

  1.     #include 
  2.     void foo()
  3.     {
  4.         return 1;
  5.     }
  6.     void main()
  7.     {
  8.         int x = 0;
  9.         x = foo();
  10.         printf("%d", x);
  11.     }

a) 1
b) 0
c) Runtime error
d) Compile time error
Answer: d
Clarification: None.

4. What will be the data type returned for the following C function?

  1.     #include 
  2.     int func()
  3.     {
  4.         return (double)(char)5.0;
  5.     }

a) char
b) int
c) double
d) multiple type-casting in return is illegal
Answer: b
Clarification: None.

5. What is the problem in the following C declarations?

   int func(int);
   double func(int);
   int func(float);

a) A function with same name cannot have different signatures
b) A function with same name cannot have different return types
c) A function with same name cannot have different number of parameters
d) All of the mentioned
Answer: d
Clarification: None.

6. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int k = m();
  5.         printf("%d", k);
  6.     }
  7.     void m()
  8.     {
  9.         printf("hello");
  10.     }

a) hello 5
b) Error
c) Nothing
d) Junk value
Answer: a
Clarification: None.

7. What will be the output of the following C code?

  1.     #include 
  2.     int *m()
  3.     {
  4.         int *p = 5;
  5.         return p;
  6.     }
  7.     void main()
  8.     {
  9.         int *k = m();
  10.         printf("%d", k);
  11.     }

a) 5
b) Junk value
c) 0
d) Error
Answer: a
Clarification: None.

8. What will be the output of the following C code?

  1.     #include 
  2.     int *m();
  3.     void main()
  4.     {
  5.         int *k = m();
  6.         printf("hello ");
  7.         printf("%d", k[0]);
  8.     }
  9.     int *m()
  10.     {
  11.         int a[2] = {5, 8};
  12.         return a;
  13.     }

a) hello 5 8
b) hello 5
c) hello followed by garbage value
d) Compilation error
Answer: c
Clarification: None.