Search Placementyogi.com
 

C Multiple Choice Advanced Questions and Answers

  :: Topics ::

1

Which of the following is the correct output of below program? 

#include<stdio.h>
int main()
{
extern int x;
x=50;
printf("%d",sizeof(x));
return 0;
}
A.

2

B.

4

C.

Result vary from compiler to compiler

D.

error, x undeclared

See Answer & Explanation Lets Discuss
Correct answer is : D
Explanation

extern x is a declaration, not definition. Hence error!

During compilation when compiler sees extern int x, it understand that int x is defined somewhere and will be available at the time of linking.

But while linking x definition is not available so it throws undeclared error.

 

suppose above code is in file 1.c

Below command will give no error because it is just the compilation.

gcc -c 1.c

 

but gcc 1.c will throw error as it is compiling and linking. To correct the error in above program define x in a file and compile together.

 

/*header.c*/

int x;

 

Now,  gcc 1.c header.c

 

and it compiles and links with no error and generate executable( a.out)

 
2

Find the output of below programs.

A. #include<stdio.h>
    int main()
    {
      printf("%d\n",x);
      return 0;
    }
  
    int x;

  B. #include<stdio.h>
      int main()
     {
       extern int x;
       printf("%d\n",x);
       return 0;
     }
  
     int x;
A.

garbage value , garbage value

B.

0, 0

C.

x undeclared, 0

D.

0, x undeclared

E.

x undeclared , x undeclared

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

 In program A during compilation when compiler reaches to printf statement x is not known so it throws error undeclared x.

 

 In program B during compilation first extern int x is executed and now comiler knows that x is defined somewhere, so it compiles. 

 In linking phase int x; is used. x is a global variable and by default it is initialized to zero.

 
3

What will be the output of below program?

#include<stdio.h>
int main()
{

 displayOut();

 return 0;
}

void displayOut()
{
 printf("Hi from placementyogi.com\n");
}
A.

compiles with no warning and error message

B.

compiles with warning: conflicting types for dispalyOut

C.

does not compile, gives error : dispalyOut not declared

D.

compiles but throws error while linking

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

Here displayOut is called before it is defined. In this case the compiler assumes that the function dispayOut() prototype is declared as int displayOut();

but when in definition compiler sees that it is returning void then it reports warning : conflicting types for dispalyOut

 

it means compiles by default assumes return type of undeclared function to int and accept unspecified number of argument.

 

Example: 

/* program1.c*/
#include<stdio.h>
int main()
{

 displayOut();

 return 0;
}

int dispalyOut()
{
 printf("Hi from placementyogi.com\n");
}

 

In program1.c no warning and error appears as return type of function displayOut() is int. Which is same as the assumption of compiler during compilation.

 
 

Banner to LogicGuns.com

Hide Page Information

Tough, difficult and tricky C Objective Questions and Answers with explanation

Here you can find Tough, difficult and tricky C Declarations and Initializations interview questions with answers and explanation.

Why C Declarations and Initializations?

In this section you can learn and practice Tough, difficult and tricky C (Questions with Answers) with explanation to improve your skills in order to face the interview, competitive examination and various entrance test (CAT, GATE, GRE, MAT, Bank Exam, Railway Exam etc.) with full confidence.

Objective questions on extern keyword, scope of variables in C

Where can I get Tough, difficult and tricky C Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved Tough, difficult and tricky C questions and answers with explanation. Fully solved examples with detailed answer description, explanation are given and it would be easy to understand. All students, freshers can download Tough, difficult and tricky C quiz questions with answers as PDF files and eBooks.

Where can I get Tough, difficult and tricky C Declarations and Initializations Interview Questions and Answers (objective type, multiple choice) with explanation?

Here you can find objective type Tough, difficult and tricky C questions and answers for interview and entrance examination with explanation. Multiple choice and true or false type questions are also provided.

How to solve Tough, difficult and tricky C problems?

You can easily solve all kinds of questions based on Tough, difficult and tricky C Declarations and Initializations by practicing the exercises (including shortcut methods to solve problems) given below.