Showing posts with label C-Programming questions. Show all posts
Showing posts with label C-Programming questions. Show all posts

Saturday, March 7, 2015

Ouput of the program part 2

 Q. What is the output of following program?

#include <iostream>
using namespace std;

// main() is where program execution begins.
int g,t;
float f,h;
int main()
{
   int a,b;
   float d,l;
cout<<"GLobal Int g="<<g<<endl;
cout<<"GLobal Int t="<<t<<endl;
cout<<"Local Int a="<<a<<endl;
cout<<"Local Int b="<<b<<endl;
cout<<"Local float d="<<d<<endl;
cout<<"Local float l="<<l<<endl;
cout<<"Global float f="<<f<<endl;
cout<<"Global float h="<<h<<endl;
   return 0;
}

Answer:-

GLobal Int g=0    //always 0 if global int 
GLobal Int t=0    //always 0 if global int 
Local Int a=0    //any garbage value if local and not assigned , in most case it can take 0 if only one int or one float
Local Int b=4196896   //any garbage value if local and not assigned 
Local float d=0  //any garbage value if local and not assigned 
Local float l=5.88211e-39   //any garbage value if local and not assigned 
Global float f=0    //always 0 if global float 
Global float h=0    //always 0 if global float 


Explanation :
Local variable is not defined by the system and need to be initialized. I not initialized then it can take any garbage value but the Global variables are initialized automatically by the system when we define them.

Saturday, December 27, 2014

Programming basic questions Part-2

1. Can we define a function with arguments as pointer as well as value??

I mean if a swap funcation is defined as :-

void swap(int num1 ,int*num2 );

is it correct to define like this??

Ans :-

Yes, we can. there is no issue in this case but as we know that the function does not allow to return values more than one. thus doing this wont change the value of num1 in the calling function and our main objective of swapping will remain incomplete.

Sunday, September 1, 2013

C-Programming questions part-1(basic)

1.How many characters are in the following string constant?

" \ t To continue, press the \"RETURN\" key\n"

Ans: total 38
27 normal characters
five blank spaces,
one special character(,)
four escape sequences (horizontal tab, two quotation marks and newline)
and the null character (\ 0) at the end of the string.


2.Which of the following are not valid identifires?
4th , _radius, value, the_tax_return,"My_name", area of circle

Ans.   4th,"My_name",area of circle

3.What is the differences between string constant "A"and character constant 'A'.

Ans. a character constant has an equivalent integer value, whereas a single-character string constant does not have an equivalent integer value and, in fact, consists of two characters -the specified character followed by the null character ( \ 0).

4.What will be the output of following programme?
 char array[11] = "Hello World";

    printf("%s\n",array);

Ans. Error

5. The unary operator always preceeds their operands (Trur/False)

Ans. Flase

i++,i-- are examples where unary operators comes after the operands

6. Write the unary operators.


Ans. ++,--,(type),sizeof,-,!

7. While considering the operators precedence and associativity , what is the difference between precedence and associativity?
Ans. Precedence tells the priority order of the operators in any expression.but ambuiguity arises while having same prirority  opereators occur in same expression.In that case an order from either right to left or left to right is followed to resolve this problem.

For example :
in     a=3+4-5
 + and - are of same priority.
then according to their associativity(Left to right) their operation will be performed in that order. Hence first addition is performed then subtraction.

NOTE: In all the operators only unary operators ,conditional operators and assignment operators are having associativity from right to left.

8.What is the value of following expression?
 i = 2 * 5 / 2
Ans. Since * and / are having same priority then according to their associativity left to right * operation is performed first.

hence i=2*5/2=10/2=5

if expression would be like this i = 2 * (5/2) then answer will be i=2*2=4 

9. In what general category do the #define and #include statements fall?

Ans. Preprocessor Statement