250+ TOP MCQs on Formatted Input and Answers

C test on “Formatted Input”. One shall practice these test 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 test questions come with detailed explanation of the answers which helps in better understanding of C concepts.

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

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int n;
  5.         scanf("%d", n);
  6.         printf("%dn", n);
  7.         return 0;
  8.     }

a) Compilation error
b) Undefined behavior
c) Whatever user types
d) Depends on the standard
Answer: b
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         char *n;
  5.         scanf("%s", n);
  6.         return 0;
  7.     }

a) Compilation error
b) Undefined behavior
c) Nothing
d) None of the mentioned
Answer: b
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         char n[] = "hellonworld!";
  5.         char s[13];
  6.         sscanf(n, "%s", s);
  7.         printf("%sn", s);
  8.         return 0;
  9.     }

a) hellonworld!
b)

hello
world!

c) hello
d) hello world!
Answer: c
Clarification: The array n contains a string which has a newline character in between the strings “hello” and “world”. A newline character is considered as a whitespace character for inputs for the scanf(), sscanf() and fscanf() functions. So, the sscanf() function will only copy upto the string “hello” into the array s. Hence, the output of the printf() function be only the string “hello”.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         short int i;
  5.         scanf("%hd", &i);
  6.         printf("%hd", i);
  7.         return 0;
  8.     }

a) Compilation error
b) Undefined behavior
c) Whatever user types
d) None of the mentioned
Answer: c
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         short int i;
  5.         scanf("%*d", &i);
  6.         printf("%hd", i);
  7.         return 0;
  8.     }

a) Compilation error
b) Somegarbage value
c) Whatever user types
d) Depends on the standard
Answer: b
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         short int i;
  5.         scanf("%*hd", &i);
  6.         printf("%hd", i);
  7.         return 0;
  8.     }

a) Compilation error
b) Somegarbage value
c) Whatever user types
d) Depends on the standard
Answer: b
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         short int i;
  5.         scanf("%h*d", &i);
  6.         printf("%hd", i);
  7.         return 0;
  8.     }

a) Compilation error
b) Undefined behavior
c) Somegarbage value
d) Depends on the standard.
Answer: a
Clarification: None.

8. Which of the following is NOT a delimiter for an input in scanf?
a) Enter
b) Space
c) Tab
d) None of the mentioned
Answer: d
Clarification: None.

9. If the conversion characters of int d, i, o, u and x are preceded by h, it indicates?
a) A pointer to int
b) A pointer to short
c) A pointer to long
d) A pointer to char
Answer: b
Clarification: None.

250+ TOP MCQs on Mathematical Functions and Answers

C questions and puzzles on “Mathematical Functions”. One shall practice these questions and puzzles to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These programming puzzles 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 questions come with detailed explanation of the answers which helps in better understanding of C concepts.

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

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

  1.     #include 
  2.     #include 
  3.     int main()
  4.     {
  5.         int i = 90;
  6.         printf("%fn", sin(i));
  7.         return 0;
  8.     }

a) Compile time error
b) Undefined behaviour
c) 0.893997
d) 1.000000
Answer: c
Clarification: None.

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

  1.     #include 
  2.     #include 
  3.     int main()
  4.     {
  5.         unsigned int i = -1;
  6.         printf("%fn", fabs(i));
  7.         return 0;
  8.     }

a) Compile time error
b) 1
c) -1
d) None of the mentioned
Answer: d
Clarification: None.

3. function fabs defined math.h header file takes the argument of type integer.
a) True
b) False
c) Depends on the implementation
d) Depends on the standard
Answer: b
Clarification: None.

4. log(x) function defined in math.h header file is __________
a) Natural base logarithm
b) Logarithm to the base 2
c) Logarithm to the base 10
d) None of the mentioned
Answer: a
Clarification: None.

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

  1.     #include 
  2.     #include 
  3.     int main()
  4.     {
  5.         int i = 10;
  6.         printf("%fn", log10(i));
  7.         return 0;
  8.     }

a) Compile time error
b) 1.000000
c) 2.302585
d) None of the mentioned
Answer: b
Clarification: None.

6. What type of inputs are accepted by mathematical functions?
a) short
b) int
c) float
d) double
Answer: d
Clarification: None.

7. In linux, apart from including math header file, the program is successfully executed by which of the following?
a) cc filename.c
b) cc filename.c -lc
c) cc -math filename.c
d) cc -lm filename.c
Answer: d
Clarification: None.

8. Which of the following is not a valid mathematical function?
a) frexp(x);
b) atan2(x,y);
c) srand(x);
d) fmod(x);
Answer: d
Clarification: None.

250+ TOP MCQs on Typedef and Answers

C Multiple Choice Questions & Answers (MCQs) on “Typedef”.

1. Which of the following keywords is used to define an alternate name for an already existing data type?
a) default
b) volatile
c) typedef
d) static
Answer: c
Clarification: The keyword typedef is used to define an alternate name for an already existing data type. It is mostly used for used defined data types.

2. We want to create an alias name for an identifier of the type unsigned long. The alias name is: ul. The correct way to do this using the keyword typedef is ____________
a) typedef unsigned long ul;
b) unsigned long typedef ul;
c) typedef ul unsigned long;
d) ul typedef unsigned long;
Answer: a
Clarification: The syntax of the keyword typedef is: keyword ;
Hence if we want to create an alias name (ul) for an identifier of type unsigned long, the correct way to do this would be: typedef unsigned long ul;

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

#include
main()
{
    typedef int a;
    a b=2, c=8, d;
    d=(b*2)/2+8;
    printf("%d",d);
}

a) 10
b) 16
c) 8
d) error
Answer: a
Clarification: In the code shown above, the keyword typedef is used to give an alias name (a) to an identifier of the type int. The expression on evaluation gives the answer 10. Hence the output of the code shown above is 10.

4. WWhat will be the output of the following C code? (If the name entered is: )

#include
#include
typedef struct employee
{
    char  name[50];
    int   salary;
} e1;
void main( )
{
    printf("Enter Employee name");
    scanf("%s",e1.name);
    printf("n%s",e1.name);
}

a) .name
b) n
c)
d) Error
Answer: d
Clarification: The code shown above will result in an error because we have used the data type e1 (defined using the keyword typedef) in the form of an identifier.

5. The keyword typedef cannot be used to give alias names to pointers.
a) True
b) False
Answer: b
Clarification: The statement given in the question is incorrect. The keyword typedef can be used to give an alias name to all data types as well as pointers.

6. What is the size of myArray in the code shown below? (Assume that 1 character occupies 1 byte)

typedef char x[10];
x myArray[5];

a) 5 bytes
b) 10 bytes
c) 40 bytes
d) 50 bytes
Answer: d
Clarification: The size of myArray will be equal to 50 bytes. In the code shown above, we have defined a character array x, of size 10. Hence the output of the code shown above is 10*5 = 50 bytes.

7. We want to declare x, y and z as pointers of type int. The alias name given is: intpt The correct way to do this using the keyword typedef is:
a)

    int typedef* intptr;
    int x,y,z;

b)

     typedef* intptr;
     int x,y,z;

c)

    int typedef* intptr;
    intptr x,y,z;

d)

     typedef int* intptr;
     intptr x,y,z;

View Answer

Answer: d
Clarification: It shows the correct way to declare x, y and z as pointers of type int using the keyword typedef. The advantage of using typedef with pointers is that we can declare any number of pointers in a single statement.

 
 

8. Consider this statement: typedef enum good {a, b, c} hello; Which of the following statements is incorrect about hello?
a) hello is a typedef of enum good
b) hello is a structure
c) hello is a variable of type enum good
d) the statement shown above is erroneous
Answer: a
Clarification: The keyword typedef is used to give an alternate name to an existing data type. Hence hello is the new name for enum good.

9. One of the major difference between typedef and #define is that typedef interpretation is performed by the _________________ whereas #define interpretation is performed by the _____________
a) pre-processor, compiler
b) user, pre-processor
c) compiler, pre-processor
d) compiler, user
Answer: c
Clarification: The major difference between typedef and #define is that the typedef interpretation is performed by the compiler whereas #define interpretation is performed by pre-processor.

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

#include
int main()
{
    typedef union a
    {
        int i;
        char ch[2];
    }hello;
    hello u;
    u.ch[0] = 3;
    u.ch[1] = 2;
    printf("%d, %d", u.ch[0], u.ch[1]);
    return 0;
}

a) 2, 3
b) 3, 2
c) 32
d) error
Answer: b
Clarification: In the code shown above, we have defined hello, which is the instance variable of a union (a) using typedef. On the execution of the code shown above, we obtain the output: 3, 2

250+ TOP MCQs on Localization and Answers

C Multiple Choice Questions & Answers (MCQs) on “Localization”.

1. Which header file is used to define data formats and currency symbols?
a) setjmp.h
b) locale.h
c) stdarg.h
d) assert.h
Answer: b
Clarification: locale. h is the header file which defines the location specific settings, such as data formats and currency location.

2. Which among the given macros is defined in the header file locale.h?
a) SCHAR_MAX
b) FLT_RADIX 2
c) EDOM
d) LC_CTYPE
Answer: d
Clarification: LC_CTYPE is the macro defined under header file locale.h. This macro affects all character functions.

3. Which macro sets everything defined under locale. h?
a) LC_ALL
b) LC_COLLATE
c) LC_SET
d) LC_TIME
Answer: a
Clarification: LC_ALL is the macro which sets everything. It is defined under header file locale.h.

4. Select the function that reads or sets location dependent information.
a) longjmp()
b) setlocale()
c) assert()
d) toupper()
Answer: b
Clarification: setlocale() function reads or sets location dependent information.
The function declaration is as follows:
char *setlocale(int category, const char loc).

5. Select the correct statement.
a) LC_MONETARY affects the monetary information
b) LC_MONETARY does not affect the monetary information
c) LC_ALL does not set everything
d) LC_CTYPE affects only one character functions
Answer: a
Clarification: LC_MONETARY is the macro defined under header file locale.h which affects the monetary information provided by localeconv function.

6. Which macro is used in the setlocale() function?
a) LC_SET
b) FLT_RADIX 2
c) LC_MESSAGES
d) SHRT_MAX
Answer: c
Clarification: LC_MESSAGES in the function char *setlocale(char category, const char loc) is used for system responses.

7. LC_COLLATE affects strcoll() and strxfrm() functions.
a) true
b) false
Answer: a
Clarification: LC_COLLATE is the macro defined under header file locale.h. It affects strcoll() and strxfrm() functions.

8. Which macro affects the strftime() function?
a) LC_TIME
b) LC_SEC
c) LC_MIN
d) LC_SET
Answer: a
Clarification: LC_TIME is the macro defined under locale. h which affects the strftime() function.

9. Select the macro that affects the information provided by localeconv function.
a) LC_ALL
b) LC_COLLATE
c) LC_NUMERIC
d) LC_CTYPE
Answer: c
Clarification: LC_NUMERIC is the macro defined under the header file locale.h which affects decimal point formatting and informations provided by localeconv function.

10. What is returned by the function localeconv()?
a) current location value
b) past location value
c) pointer to the last location
d) pointer to the current location
Answer: d
Clarification: The function returns pointer to the struct Iconv for the current location. The function os declared as follows:
struct lconv *localeconv(void).

250+ TOP MCQs on Conditional Preprocessor Directives and Answers

C Multiple Choice Questions & Answers on “Conditional Preprocessor Directives – 2”.

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

#include
#define san 10
main()
{
    #ifdef san
    #define san 20
    #endif
    printf("%d",san);
}

a) 10
b) 20
c) Error
d) 1020
Answer: b
Clarification: In the code shown above, if the identifier san is defined, then its value is redefined and changed to 20. It is then printed. Hence the output is 20.

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

#include
#define hello 
main()
{
    #ifdef hello
    #define hi 4
    #else
    #define hi 5
    #endif
    printf("%d",hi);
 
}

a) 4
b) 5
c) 45
d) error
Answer: a
Clarification: The code shown above illustrates #define, #ifdef, #else, #endif. Since the identifier hello is defined (condition of #if is true), another identifier names hi is defined and it’s value is 4. If hello was not defined, then the value of hi would be 5.

3. The purpose of the preprocessor directive #error is that ____________
a) It rectifies any error present in the code
b) It rectifies only the first error which occurs in the code
c) It causes the preprocessor to report a fatal error
d) It causes the preprocessor to ignore an error
Answer: c
Clarification: #error is a preprocessor directive which causes the preprocessor to report a fatal error. The tokens which form the rest of the line after #error are used as the error message.

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

#include
#define max 20
main()
{
    #ifndef max
    #define min 10
    #else
    #define min 30
    #endif
    printf("%d",min);
}

a) 10
b) 20
c) 30
d) error
Answer: c
Clarification: The output of the code shown above is 30. Since the identifier max is defined (the condition of #ifndef is true), hence another identifier, that is, min is defined and its value is equal to 30.

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

#include
#define hello 10
main()
{
    #ifdef hello
    #undef hello
    #define hello 100
    #else
    #define hello 200
    #endif
    printf("%d",hello);
}

a) Error
b) 10
c) 100
d) 200
Answer: c
Clarification: The output of the code shown above is 100. If the identifier hello is defined, then it is undefined and redefined. It’s new value is 100. If the identifier was not defined, it would be defined with a value of 200.

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

#include
#define sf 10
main()
{
    if(sf==100)
        printf("good");
    else
    {
        printf("bad");
        sf=100;
    }
    printf("%d",sf);
}

a) 100
b) bad
c) 10
d) error
Answer: d
Clarification: The above code will result in an error because a macro cannot be defined using a simple assignment operator. In the code shown above, the value of sf is changed to 100 using the assignment operator. This causes an error.

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

#include
void f()
{
    #define sf 100
    printf("%d",sf);
}
int main()
{
    #define sf 99;
    f();
    printf("%d",sf);
}

a) error
b) 100
c) 99
d) 10099
Answer: a
Clarification: Macro definition is global even if it is appears within the function scope. In this case the macro sf is defined in the function f(). Hence it results in a compile time error.

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

#include
#define INDIA 1
#define US 2
#define CC US
main()
{
    #if CC==INDIA
    printf("Rupee");
    #elif CC==US
    printf("Dollar");
    #else
    printf("Euro");
    #endif 
}

a) Euro
b) Rupee
c) Dollar
d) Error
Answer: c
Clarification: The output of the code shown above is Dollar. Since the macro CC is defined as US at the beginning of the code, it satisfies the condition #elif(CC==US). Hence “Dollar” is printed.

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

#define sqr(x) x*x
main()
{
    int a1;
    a1=25/sqr(5);
    printf("%d",a1);
}

a) 25
b) 1
c) 5
d) error
Answer: a
Clarification: The output of the code shown above is 25. This is because the arithmetic statement takes the form of: 25/5*5 (which is equal to 1). If the macro had been defined as #define sqr(x) (x*x), the output would have been 1.

10. Which of the following is not a preprocessor directive?
a) #error
b) #pragma
c) #if
d) #ifelse
Answer: d
Clarification: #ifelse is not a preprocessor directive. #error, #pragma, #if are preprocessor directives. There is a preprocessor directive, #elif, which performs the function of else-if.

250+ TOP MCQs on Type Conversions and Answers

C language Objective Questions on “Type Conversions”. One shall practice these Objective 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 language Objective Questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C language Objective Questions on “Type Conversions” 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.         float x = 0.1;
  5.         if (x == 0.1)
  6.             printf("");
  7.         else
  8.             printf("Advanced C Classes");
  9.     }

a) Advanced C Classes
b)
c) Run time error
d) Compile time error
Answer: a
Clarification: None.

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

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

a) 0.100000, junk value
b) Junk value, 0.100000
c) 0, 0.100000
d) 0, 0.999999
Answer: b
Clarification: None.

3. What will be the output of the following C code? (Initial values: x= 7, y = 8)

  1.     #include 
  2.     void main()
  3.     {
  4.         float x;
  5.         int y;
  6.         printf("enter two numbers n", x);
  7.         scanf("%f %f", &x, &y);
  8.         printf("%f, %d", x, y);
  9.     }

a) 7.000000, 7
b) Run time error
c) 7.000000, junk
d) Varies
Answer: c
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         double x = 123828749.66;
  5.         int y = x;
  6.         printf("%dn", y);
  7.         printf("%lfn", y);
  8.     }

a) 0, 0.0
b) 123828749, 123828749.66
c) 12382874, 12382874.0
d) 123828749, 0.000000
Answer: d
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         int x = 97;
  5.         char y = x;
  6.         printf("%cn", y);
  7.     }

a) a
b) b
c) 97
d) Run time error
Answer: a
Clarification: None.

6. When double is converted to float, then the value is?
a) Truncated
b) Rounded
c) Depends on the compiler
d) Depends on the standard
Answer: c
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         unsigned int i = 23;
  5.         signed char c = -23;
  6.         if (i > c)
  7.             printf("Yesn");
  8.         else if (i < c)
  9.             printf("Non");
  10.     }

a) Yes
b) No
c) Depends on the compiler
d) Depends on the operating system
Answer: b
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int i = 23;
  5.         char c = -23;
  6.         if (i < c)
  7.             printf("Yesn");
  8.         else
  9.             printf("Non");
  10.     }

a) Yes
b) No
c) Depends on the compiler
d) Depends on the standard
Answer: b
Clarification: None.