250+ TOP MCQs on scanf and Answers

C Multiple Choice Questions & Answers (MCQs) on “scanf – 1”.

1. The syntax of the scanf() is scanf(“control string “, arg1,arg2,arg3,….,argn); the prototype of control string is ____________
a) [=%[width][modifiers]type=]
b) [=%[modifiers][width]type=]
c) [=%[width] [modifiers]]
d) [width][modifiers]
Answer: a
Clarification: scanf() starts with the symbol % followed by the width, modifier, type of the argument.

2. What is the use of symbol * in the control string as shown [=%[*][width] [modifiers] type=]?
a) * is optional and used when the data should be read from the stream but ignored
b) * is not optional, used to read data from the stream but it is not ignored
c) * is not optional, it is used to read data stream but ignored
d) * is optional and used to read data from stream but it is not ignored
Answer: a
Clarification: * is an optional argument, it indicates that data should be read from the steam but ignored (not stored in a memory location)

3. What action is carried out by scanf if a user enters any blank spaces, tabs, and newlines?
a) consider as input
b) ignores it
c) produces error
d) nothing can be said
Answer: b
Clarification: The scanf() function ignores any blank spaces, tabs, and newlines entered by the user. This scanf() function just returns the number of input fields successfully scanned and stored.

4. What error will generate if the read and write parameters are not separated by commas?
a) run-time error
b) compile error
c) logical error
d) no error
Answer: b
Clarification: A compile error will be generated if the read and write parameters are not separated by commas.

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

char str[] =”Good”;
scanf(%s”, str);

a) compile error
b) run-time error
c) good
d) logical error
Answer: c
Clarification: String can be read from the stream without the use of address of operator (&).

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

scanf(%d %d %d”,&n1,&n2);

a) read data for two
b) generate error
c) read data for three
d) nothing can be said
Answer: b
Clarification: The following scanf() statement will generate an error as no variable address is given for the third conversion specification.

7. What form the data must be entered for the given C code?

scanf(%d / %d”, &n1,&n2);

a) 6  9
b) 6/9
c) compile error
d) run-time error
Answer: b
Clarification: The slash in the control String are neither white space characters nor a part of conversion specification, so the user must enter data of the form 6/9.

8. A fatal error will be generated if the format string is ended with a white space character.
a) true
b) false
Answer: a
Clarification: An error will be generated if the format string %s is ended with white space character.

9. Explain the format string “%5d%s %c”
a) five characters as a decimal integer, then reads the remaining as a string and then scans the first non-whitespace character
b) compile error
c) run-time error
d) read first five characters as a decimal and ignore the rest
Answer: a
Clarification: The above format string reads the first five characters as a decimal integer, then reads the remaining as a string until a space, newline or tab is found, then reads the first non-whitespace character.

10. _____ is an optional argument that gives the maximum number of characters to be read.
a) modifiers
b) width
c) precision
d) length
Answer: b
Clarification: Width is the argument that gives the maximum number of characters to be read. Few characters will be read if the scanf function encounters white space and it will stop processing further.

250+ TOP MCQs on Mathematical functions and Answers

C Objective Questions on “Mathematical functions – 3”.

1. Which of the following statement is correct?

    double x, y, z;
    x = 5.123456;
    z= modf(x, *y);

a) y stores integer part of x, z returns fractional part of x
b) y stores integer part of x, z returns integer part of x
c) y stores fractional part of x, z returns integer part of x
d) y stores fractional part of x, z returns fractional part of x
Answer: a
Clarification: double modf(double x, double *y)
This function returns the fractional part of x, with the same sign. modf() function breaks the argument value into integer and fraction parts, each of which has the same sign as the argument. It stores the integer part as a double in the object pointed to by y.

2. A domain error occurs if x is negative and y is not an integral value for the function pow(double x, double y).
a) true
b) false
Answer: a
Clarification: The pow() function computes x raised to the power y. A domain error occurs if x is negative and y is not an integral value. A domain error occurs if the result cannot be represented when x is zero and y is less than or equal to zero. A range error may occur.

3. A function is declared as sqrt(-x) under the header file math.h, what will the function return?
a) square root of x
b) complex number
c) domain error
d) range error
Answer: c
Clarification: double sqrt (double x);
The sqrt function computes the nonnegative square root of x. A domain error occurs if the argument is negative.

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

    double x=1.2
    printf("%.1lf", ceil(x));

a) 1
b) 2
c) 1.0
d) 2.0
Answer: d
Clarification: The ceil function returns the smallest integral value not less than x, expressed as a double.

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

    double x=3,y= - 6;
    printf("%lf %lf", fabs(x), fabs(y));

a) 3 6
b) -3 6
c) 3.0 6.0
d) 3.000000 6.000000
Answer: d
Clarification: double fabs(double x);
The fabs function computes the absolute value of a floating-point number X.

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

double x=1.8;
printf("%.2lf",floor(x));

a) 2.0
b) 2.00
c) 1.0
d) 1.00
Answer: d
Clarification: double floor(double x);
The floor function computes the largest integral value not greater than x.

7. double ______ (double x, double y) computes the floating-point remainder of x/y.
a) modf
b) fmod
c) ceil
d) floor
Answer: b
Clarification: double fmod(doublr x, double y);
The fmod() function computes the floating-point remainder of x/y.

8. sqrt(x) function is not faster than the apparent equivalent pow(x,0.5).
a) true
b) false
Answer: b
Clarification: sqrt(x) function is generally much faster than the apparent equivalent pow (x, 0.5).

9. Which of the given C function can be used instead of the apparent identity pointed to by y?

int x=1; 
double y= 0.5 * (exp (x) + exp (-x));

a) cos(x)
b) cosh(x)
c) fmod(x)
d) modf(x)
Answer: b
Clarification: The cosh function returns the hyperbolic cosine value. cosh(x)= 0.5 * (exp (x) + exp (-x))

10. Which function is used to recombine the fraction and exponent parts of a floating-point value after you have worked on them separately?
a) frexp()
b) exp()
c) modf()
d) Idexp()
Answer: d
Clarification: ldexp() – Use this function to recombine the fraction and exponent parts of a floating-point value after you have worked on them separately.

250+ TOP MCQs on Date and Time Function and Answers

C Multiple Choice Questions & Answers on “Date and Time Function – 2”.

1. Which of the following format specifiers is used to represent the name of the time zone?
a) %A
b) %B
c) %H
d) %Z
Answer: d
Clarification: The format specifier %Z is used to specify the name of the time zone. %A- full weekday, %B- full month name, %H-hours(0-23).

2. What will be the output of the following C code if the system time is 4:27 PM?

#include
#include
int main()
{
    struct tm *ptr;
    time_t t;
    char str[100];
    t = time(NULL);
    ptr = localtime(&t);
    strftime(str,100,"%H %p %M minutes",ptr);
    puts(str);
    return 0;
}

a) 16 27 minutes
b) 4 27 minutes
c) 16 PM 27 minutes
d) 4 PM 27 minutes
Answer: c
Clarification: %H is a format specifier used to represent the hours (0-23) and %I specifies the hours in the format(0-12), %p is used to specify AM or PM, %M is used to specify the minutes. Hence output will be: 16 PM 27 minutes

3. What will be the output of the following C code if the system date is 8/22/2016?

#include
#include
int main()
{
    struct tm *ptr;
    time_t t;
    char str[100];
    t = time(NULL);
    ptr = localtime(&t);
    strftime(str,100,"%B",ptr);
    puts(str);
    return 0;
}

a) 9
b) August
c) Aug
d) Error
Answer: b
Clarification: %B is a format specifier which is used to specify the full month name. Hence the output will be August.

4. What will be the output of the following C code if the system date is 6/2/2017 (Friday)?

#include
#include
int main()
{
    struct tm *ptr;
    time_t t;
    char str[100];
    t = time(NULL);
    ptr = localtime(&t);
    strftime(str,100,"%A",ptr);
    puts(str);
    return 0;
}

a) Error
b) Fri
c) Friday
d) 6
Answer: c
Clarification: %A specifies the full weekday. Hence the output is Friday.

5. Which of the following library functions is used to read location dependent information?
a) localtime()
b) localeconv()
c) localcon()
d) local()
Answer: b
Clarification: localeconv() is used to read the location dependent information. The function used to find the current time is localtime().

6. Which of the following functions is used to convert the date and time into a calendar format?
a) difftime()
b) clock()
c) mktime()
d) ctime()
Answer: c
Clarification: The function mktime() is used to convert the date and time into a calendar format. The function difftime() is used to find the difference between two specified timings, the function clock() is used to return the number of ticks.

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

#include
#include
main()
{
    struct tm t;
    time_t tc;
    t.tm_year=2017-1900;
    t.tm_mday=25;
    t.tm_mon=5;
    t.tm_hour=1;
    t.tm_min=30;
    t.tm_sec=0;
    t.tm_isdst=0;
    tc=mktime(&t);
    printf(ctime(&tc));
}

a) Sun Jun 25 01:30:00 2017
b) Sun June 25 1:30 2017
c) Sun Jun 25 1:30 117
d) Sun June 25 1:30:00 117
Answer: a
Clarification: The function mktime() converts the time and date into a calendar format. Hence the output of the code shown above is: Sun Jun 25 1:30:00 2017.

8. The value of tm_isdst is ____ when DST( Daylight Savings Time) is in effect, ______ when DST is not in effect and ______ when the DST status is unknown.
a) -1, 1, 0
b) 1, 0, -1
c) 0, 1, -1
d) 1, -1, 0
Answer: b
Clarification: The value of tm_isdst is 1 when Daylight Savings Time is in effect, 0 when DSP is not in the effect and -1 when DST status is not known.

9. The library function clock() returns the number of _________ elapsed since the start of the program.
a) minutes
b) clock ticks
c) milli-seconds
d) micro-seconds
Answer: b
Clarification: The library function clock() returns the number of clock ticks elapsed since the start of the program. To get the number of seconds used by the CPU, we should divide by CLOCKS_PER_SEC.

10. What will be the output of the following C code if the name entered is “TOM” and time taken to enter this name is 2 seconds?

#include 
#include 
int main ()
{
	time_t time1,time2;
	char get_input [256];
	double dif_sec;
	time (&time1);
	printf ("Please enter the name of your pet: ");
	gets (get_input);
	time (&time2);
	dif_sec = difftime (time2,time1);
	printf ("%.2fn", dif_sec );
	return 0;
}

a) Error
b) 2
c) 2.0
d) 2.00
Answer: d
Clarification: The library function difftime() returns the difference in seconds between time1 and time 2. Since the format specifier is %.2f, the output will be 2.00.

250+ TOP MCQs on Variable Names and Answers

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

Here is a listing of C Objective Questions on “Variable Names” along with answers, explanations and/or solutions:

1. Which is valid C expression?
a) int my_num = 100,000;
b) int my_num = 100000;
c) int my num = 1000;
d) int $my_num = 10000;
Answer: b
Clarification: Space, comma and $ cannot be used in a variable name.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         printf("Hello World! %d n", x);
  5.         return 0;
  6.     }

a) Hello World! x;
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!
Answer: c
Clarification: It results in an error since x is used without declaring the variable x.
Output:
$ cc pgm1.c
pgm1.c: In function ‘main’:
pgm1.c:4: error: ‘x’ undeclared (first use in this function)
pgm1.c:4: error: (Each undeclared identifier is reported only once
pgm1.c:4: error: for each function it appears in.)

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int y = 10000;
  5.         int y = 34;
  6.         printf("Hello World! %dn", y);
  7.         return 0;
  8.     }

a) Compile time error
b) Hello World! 34
c) Hello World! 1000
d) Hello World! followed by a junk value
Answer: a
Clarification: Since y is already defined, redefining it results in an error.
Output:
$ cc pgm2.c
pgm2.c: In function ‘main’:
pgm2.c:5: error: redefinition of ‘y’
pgm2.c:4: note: previous definition of ‘y’ was here

4. Which of the following is not a valid variable name declaration?
a) float PI = 3.14;
b) double PI = 3.14;
c) int PI = 3.14;
d) #define PI 3.14
Answer: d
Clarification: #define PI 3.14 is a macro preprocessor, it is a textual substitution.

5. What will happen if the following C code is executed?

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

a) It will cause a compile-time error
b) It will cause a run-time error
c) It will run without any error and prints 3
d) It will experience infinite looping
Answer: c
Clarification: A C program can have same function name and same variable name.
$ cc pgm3.c
$ a.out
3

6. What is the problem in the following variable declaration?

float 3Bedroom-Hall-Kitchen?;

a) The variable name begins with an integer
b) The special character ‘-‘
c) The special character ‘?’
d) All of the mentioned
Answer: d
Clarification: A variable name cannot start with an integer, along with that the C compiler interprets the ‘-‘ and ‘?’ as a minus operator and a question mark operator respectively.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int ThisIsVariableName = 12;
  5.         int ThisIsVariablename = 14;
  6.         printf("%d", ThisIsVariablename);
  7.         return 0;
  8.     }

a) The program will print 12
b) The program will print 14
c) The program will have a runtime error
d) The program will cause a compile-time error due to redeclaration
Answer: b
Clarification: Variable names ThisIsVariablename and ThisIsVariableName are both distinct as C is case sensitive.
Output:
$ cc pgm4.c
$ a.out
14

8. Which of the following cannot be a variable name in C?
a) volatile
b) true
c) friend
d) export
Answer: a
Clarification: volatile is C keyword.

250+ TOP MCQs on Assignment Operators & Expressions and Answers

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

Here is a listing of C test questions on “Assignment Operators & Expressions” along with answers, explanations and/or solutions:

1. What is the type of the following assignment expression if x is of type float and y is of type int?

 y = x + y;

a) int
b) float
c) there is no type for an assignment expression
d) double
Answer: a
Clarification: None.

2. What will be the value of the following assignment expression?

 (x = foo())!= 1 considering foo() returns 2

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

3. Operation “a = a * b + a” can also be written as ___________
a) a *= b + 1;
b) (c = a * b)!=(a = c + a);
c) a = (b + 1)* a;
d) All of the mentioned
Answer: d
Clarification: None.

4. What will be the final value of c in the following C statement? (Initial value: c = 2)

a) c = 1;
b) c = 2;
c) c = 3;
d) c = 4;
Answer: d
Clarification: None.

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

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

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

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int a = 4, n, i, result = 0;
  5.         scanf("%d", n);
  6.         for (i = 0;i < n; i++)
  7.         result += a;
  8.     }

a) Addition of a and n
b) Subtraction of a and n
c) Multiplication of a and n
d) Division of a and n
Answer: c
Clarification: None.

7. Which of the following is an invalid assignment operator?
a) a %= 10;
b) a /= 10;
c) a |= 10;
d) None of the mentioned
Answer: d
Clarification: None.

250+ TOP MCQs on Break and Continue and Answers

C Objective Questions on “Break and Continue statements”. 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 Objective Questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C Objective Questions on “Break and Continue statements” along with answers, explanations and/or solutions:

1. Which keyword can be used for coming out of recursion?
a) break
b) return
c) exit
d) both break and return
Answer: b
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int a = 0, i = 0, b;
  5.         for (i = 0;i < 5; i++)
  6.         {
  7.             a++;
  8.             continue;
  9.         }
  10.     }

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

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int a = 0, i = 0, b;
  5.         for (i = 0;i < 5; i++)
  6.         {
  7.             a++;
  8.             if (i == 3)
  9.                 break;
  10.         }
  11.     }

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

4. The keyword ‘break’ cannot be simply used within _________
a) do-while
b) if-else
c) for
d) while
Answer: b
Clarification: None.

5. Which keyword is used to come out of a loop only for that iteration?
a) break
b) continue
c) return
d) none of the mentioned
Answer: b
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         int i = 0, j = 0;
  5.         for (i = 0;i < 5; i++)
  6.         {
  7.             for (j = 0;j < 4; j++)
  8.             {
  9.                 if (i > 1)
  10.                     break;
  11.             }
  12.             printf("Hi n");
  13.         }
  14.     }

a) Hi is printed 5 times
b) Hi is printed 9 times
c) Hi is printed 7 times
d) Hi is printed 4 times
Answer: a
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         int i = 0;
  5.         int j = 0;
  6.         for (i = 0;i < 5; i++)
  7.         {
  8.             for (j = 0;j < 4; j++)
  9.             {
  10.                 if (i > 1)
  11.                     continue;
  12.                     printf("Hi n");
  13.             }
  14.         }
  15.     }

a) Hi is printed 9 times
b) Hi is printed 8 times
c) Hi is printed 7 times
d) Hi is printed 6 times
Answer: b
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         int i = 0;
  5.         for (i = 0;i < 5; i++)
  6.             if (i < 4)
  7.             {
  8.                 printf("Hello");
  9.                 break;
  10.             }
  11.     }

a) Hello is printed 5 times
b) Hello is printed 4 times
c) Hello
d) Hello is printed 3 times
Answer: c
Clarification: None.