C programming online test


C programming online test questions answers and explanation for freshers



Topic:
Total questions:
Total marks:
Correct answer:
Total time:
Incorrect answer:
Passing Marks:
1
What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    int i;
    for(i=0;i<5;i++){
         int i=10;
         printf(" %d",i);
         i++;
    }
    return 0;
}
(A)
10 11 12 13 14
(B)
10 10 10 10 10
(C)
0 1 2 3 4
(D)
Compilation error
                         
Explanation:
Default storage class of local variable is auto. Scope of auto variables are block in which it has been declared. When program control goes out of the scope auto variables are dead. So variable i which has been declared inside for loop has scope within loop and in each iteration variable i is dead and re-initialized.
Note: If we have declared two variables of same name but different scope then local variable will have higher priority.
2
What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    register a,b,x;
    scanf("%d %d",&a,&b);
    x=a+~b;
    printf("%d",x);
    return 0;
}
(A)
0
(B)
It will be difference of a and b
(C)
It will be addition of a and b
(D)
Compilation error
                         
Explanation:
Register variables are stored in CPU. So it has not memory address. Hence it is incorrect to write &a.
3
What will be output if you will execute following c code?

#include<stdio.h>
auto int a=5;
int main(){
    int x;
    x=~a+a&a+a<<a;
    printf("%d",x);
    return 0;
}
(A)
5
(B)
0
(C)
153
(D)
Compilation error
                         
Explanation:
We cannot declare auto variable outside of any function since it auto variables gets are created (i.e. gets memory) at run time.
4
What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    register int a,b;
    int c;
    scanf("%d%d",&a,&b);
    c=~a + ~b + ++a + b++;
    printf(" %d",c);
    return 0;
}
//User input is: 1 2
(A)
-1
(B)
0
(C)
1
(D)
Compilation error
                         
Explanation:
Register variables are stored in CPU. So it has not memory address. Hence it is incorrect to write &a.
5
What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    int arr[3]={10,20,30};
    int x=0;
    x = ++arr[++x] + ++x + arr[--x];
    printf("%d ",x);
    return 0;
}
(A)
22
(B)
23
(C)
43
(D)
44
                         
Explanation:
In Turbo C 3.0 and 4.5 compilers
Output: 43

Consider on expression:

= ++arr[++x] + ++x + arr[--x] //x = 0 + 1
= ++arr[++x] + ++x + arr[--x] //x = 1 + 1
= ++arr[++x] + ++x + arr[--x] //x = 2 - 1
= ++arr[1] + 1 + arr[1] //x = 1
= ++arr[1] + 1 + arr[1]  //arr[1] = 20+1
= arr[1] + 1 + arr[1] //arr[1] = 21
= 21 + 1 + 21
= 43
In Linux GCC complier
Output: 44

Consider on expression:
= ++arr[++x] + ++x + arr[--x] //x = 0 + 1
= ++arr[1] + ++x + arr[--x] ////x = 1 + 1
= ++arr[++x] + 2 + arr[--x] //x = 2 - 1
= ++arr[1] + 2 + arr[1] //arr[1] = 20+1
= arr[1] + 1 + arr[1] //arr[1] = 21
= 21 + 2 + 21
= 44
6
What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    int a[]={10,20,30,40};
    int i=3,x;
    x=1*a[--i]+2*a[--i]+3*a[--i];
    printf("%d",x);
    return 0;
}
(A)
30
(B)
60
(C)
90
(D)
Compilation error
                         
Explanation:
In Turbo C 3.0 and 4.5 compilers
Output: 60

Consider on expression:
= 1 * a[--i] + 2 * a[--i] + 3 * a[--i] //i = 3 - 2
= 1 * a[--i] + 2 * a[--i] + 3 * a[--i] //i = 2 - 1
= 1 * a[--i] + 2 * a[--i] + 3 * a[--i] //i = 1 - 1
= 1 * a[0] + 2 * a[0] + 3 * a[0] //i = 0
= 1 * 10 + 2 * 10 + 3 * 10 //a[0] = 10
= 10 + 20 + 30
= 60

In Linux GCC complier
Output: 90

Consider on expression:
= 1 * a[--i] + 2 * a[--i] + 3 * a[--i] //i = 3 - 2
= 1 * a[--i] + 2 * a[--i] + 3 * a[--i] //i = 2 - 1
= 1 * a[1] + 2 * a[1] + 3 * a[--i] //i = 1 - 1
= 1 * a[1] + 2 * a[1] + 3 * a[0]
= 1 * 20 + 2 * 20 + 3 * 10
= 20 + 40 + 30
= 90
7
What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    static int a[][2][3]={0,1,2,3,4,5,6,7,8,9,10,11,12};
    int i=-1;
    int d;
    d=a[i++][++i][++i];
    printf("%d",d);
    return 0;
}
(A)
9
(B)
10
(C)
11
(D)
Compilation error
                         
Explanation:
= a[i++][++i][++i] //i = -1 + 1
= a[i++][++i][++i] //i = 0 + 1
= a[1][1][1] //i = 1 + 1
= 10
8
What will be output if you will execute following c code?

#include<stdio.h>
int f(int);
int main(){
    int i=3,val;
    val=sizeof (f(i)+ +f(i=1)+ +f(i-1));
    printf("%d %d",val,i);  
    return 0; 
}
int f(int num){
        return num*5;
}
(A)
2 3
(B)
4 3
(C)
3 2
(D)
Compilation error
                         
Explanation:
Turbo C 3.0 and Turbo C 4.5 compiler:
2 3
Linux GCC complier:
4 3

Any expression inside sizeof operator is never changed the value of the any variable. So value of variable i will remain 3. After the evaluation of expression inside sizeof operator we will get an integer value. So value of variable val will be sizeof int data type.

Note: Size of into in turbo C 3.0 and 4.5 is two byte while Linux gcc complier is four byte
9
What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    int x,a=3;
    x=+ +a+ + +a+ + +5;
    printf("%d  %d",x,a);
    return 0;
}
(A)
10 3
(B)
11 3
(C)
10 5
(D)
Compilation error
                         
Explanation:
Consider on expression: + +a
Here both + are unary plus operation. So
= + +a+ + +a+ + +5;
= + +3+ + +3+ + 5
= 3+ 3+ 5
= 11

Note: Increment operator ++ cannot have space between two plus symbol.
10
What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    int num,i=0;
    num=-++i+ ++-i;
    printf("%d",num);
    return 0;
}
(A)
0
(B)
1
(C)
-2
(D)
Compilation error
                         
Explanation:
After operation of any operator on operand it returns constant value. Here we are performing unary minus operator on variable i so it will return a constant value and we can perform ++ operation on constant.  
11
What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    int num,a=5;
    num=-a--+ +++a;
    printf("%d  %d",num,a);
    return 0;
}
(A)
1 5
(B)
-1 6
(C)
1 6
(D)
0 5
                         
Explanation:
= -a--+ +++a
= -a-- + + ++a
= -a-- + + ++a
= -6 + + 6 //a = 6 -1
= -6 + 6 //a = 5
= 0
12
What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    int num,a=15;
    num=- - - -a--;
    printf("%d  %d",num,a);
    return 0;
}
(A)
15 14
(B)
14 15
(C)
14 14
(D)
15 15
                         
Explanation:
= - - - -a
= - - - -15 //a = 15 – 1
= 15  //a = 14
13
What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    int x,a=2;
    x=++a,++a,a++;
    printf("%d  %d",x,a);
    return 0;
}
(A)
5 5
(B)
3 5
(C)
4 5
(D)
5 4
                         
Explanation:
x = ++a, ++a, a++
x = 3, ++a, a++ // a = 2 + 1
x = 3, ++a, a++ // = operator has higher precedence than comma operator
x = 3, ++a, a++ // a = 3 + 1
x = 3, 4, a++ 
x = 3, 4, 4 // a = 4 + 1
x = 3 // a = 5
14
What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    int x,i=2;
    x=~-!++i;
    printf("%d",x);
    return 0;
}
(A)
-2
(B)
-1
(C)
0
(D)
1
                         
Explanation:
= ~-!++i
= ~-!3 //i = 2 + 1
= ~-0 //!3 = 0
= ~0 //-0 = 0
= -(0 + 1) //~ is 1's complement operator.
= -1 
15
What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    static double *p,*q,*r,*s,t=5.0;
    double **arr[]={&p,&q,&r,&s};
    int i;
    *p=*q=*r=*s=t;
    for(i=0;i<4;i++)
        printf("%.0f  ",**arr[i]);
    return 0;
}
(A)
5 5 5 5 5 
(B)
5 6 7 8 9
(C)
Infinite loop
(D)
Run time error

                         
Explanation:
Turbo C 3.0:
5 5 5 5 5 
Turbo C 4.5 and Linux GCC complier:
Run time error
16
What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    float x;
    x=0.35==3.5/10;
    printf("%f",x);
    return 0;
}
(A)
0.000000
(B)
1.000000
(C)
0.350000
(D)
Compilation error
                         
Explanation:
Turbo C 3.0 and Turbo C 4.5 compiler:
Output: 0.000000
3.5/10 is little greater than .35

Linux GCC compilers:
Output: 1.000000

Note: == is logic operator. It returns 1 if both operands are equal otherwise it returns 0.
17
#include<stdio.h>
int main(){
    int arr[]={6,12,18,24};
    int x=0;
    x=arr[1]+(arr[1]=2);
    printf("%d",x);
    return 0;
}
(A)
4
(B)
8
(C)
14
(D)
Compilation error
                         
Explanation:
= arr[1] + (arr[1] = 2) //arr[i] = 2
= arr[1] + arr[1]
= 2 + 2
= 4
18
What will be output if you will execute following c code?

#include<stdio.h>
int sq(int);
int main(){
    int a=1,x;
    x=sq(++a)+sq(a++)+sq(a++);
    printf("%d",x);
return 0;
}
int sq(int num){
    return num*num;
}
(A)
15
(B)
16
(C)
17
(D)
18
                         
Explanation:
= sq(++a) + sq(a++) + sq(a++) //a= 1 + 1
= sq(2) + sq(2) + sq(a++) //a = 2 + 1
= sq(2) + sq(2) + sq(3//a = 3 + 1
= 4 + 4 + 9
= 17


Note: Pre-increment fist increment then assign while post increment operator first assign then increment.
19
What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    printf("%c",*"abcde");
return 0;
}
(A)
acbcd
(B)
e
(C)
a
(D)
NULL
                         
Explanation:
String constant "abcde" will return memory address of first character of the string constant. *"abcde" will return the first character of string constant.  
20
What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    printf("%d","abcde"-"abcde");
return 0;
}
(A)
0
(B)
-1
(C)
1
(D)
Garbage
                         
Explanation:
Memory address of string constants depends upon operating system.
Total correct answers:
Total incorrect answers:
Total not attempted questions:
Marks obtained:
Result:

It is C programming language online test or quiz objective types questions answers with explanation for interview or written test. If you have any queries or suggestions, please share it.

38 comments:

Anonymous said...

which one is standard compiler? turbo c dev c++ or lunix?

sunil mundhara said...

can anybody explain this???


#include

main()
{
int **p,q;
p = (int **)5;
q = 10;
printf("%d",q+p);
return 0;
}

ashwini said...

linux of course.

jainary mare morante said...

i am a first year INFORMATION TECHNOLOGY student.at UE .this really help me to understand my course more..thank you :)

Anonymous said...

arr[ ]={4,5,8,8,4};
x=arr[1]+(arr[1]=2);
while executing this line which one should be executed first whether arr[ 1 ] or (arr[ 1 ]=2) helpme

Rangan M K said...

In q6 the gcc compilers output is 110 . pls verify and tell us the reason

keerthana rangaraj said...

will you provide certificate for this test if we pass

venkatesh kurla said...

so why turbo c preferred here

Srinivas Pinninti said...

#include
int main(){
int a[]={10,20,30,40};
int i=3,x;
x=1*a[--i]+2*a[--i]+3*a[--i];
printf("%d",x);
return 0;
}


here the o/p is 100 but you have given the ans as 60 can u explain this for me

Anonymous said...

I suggest you refactor this test and allow for different compiler treatment of undefined cases and other moot points. FYI, some of your "Compilation errors" actually compile on my machine, int size is not specified by the standard, your float expression evaluation on my machine was actually not done as float and so on...

Ashish Mishra said...

Its 100 Not 110.

Anonymous said...

chutiyo ans is 100 for sixth ques

Raam S said...

Checking

shahnaz shaik said...

yeaa :-/

ankitraj said...

ques no6 & 17 gives ans 100 and 14 according to devc++ compiler tell me which compiler i use to give correct ans of such complication...

Vyshnavi chowdary said...
This comment has been removed by the author.
sam said...

que no 6
dev c++ is giving ans 130 how can it.can any one explain

sam said...

dev c++ gives 130

ECE IONS said...

first verify ur ans ,,lots of mistake

pritesh garabadu said...

rishi kahila BANDA haichi

Nickk Haun said...

buzz of ur answers man.. so confusing answers honestly.

Jai Ram said...

ok

Narendra Prajapat said...

Level is much better ...
Thanks...

ramyareddy musuku said...

firstly (arr[1]=2) must be taken bcoz () have more precedence than other

ramyareddy musuku said...

firstly (arr[1]=2) must be taken bcoz () have more precedence than other

Li Pny said...

Most of the above are useless questions - they hardly test practical C knowledge : Eg.
in Q8: val=sizeof (f(i)+ +f(i=1)+ +f(i-1)); and in Q11: num=-a--+ +++a; --> who in sound mind will be programming like that. This is insane.

Also, 'test questions' shall require little need for arithmetics : eg. in Q3 there is this : x= ... a<<a, and a is = 5. How on earth am I suposed to quickly calculate '5 left shifted by 5 places'. Stupid.

And for some questions, ALL proposed answers are incorrect. Eg. in Q3 the (tested using Borland compiler) the answer is 320 - and it is NOT listed.

Time.Run() said...

in addition
number of questions has code with undefined/unspecific behavior and answers don't give that option

praveenkumar pujari said...

if u include proper header file than output is 85.

Moni Mahe said...

Pointer arithmetic cannot be performed on which pointer?

Prince Vijay Pratap said...

On void pointer because compiler doesn't know the size of the item on which void pointer is pointing to.

Unknown said...

if u run this code in gcc compiler than u will get o/p 7..bcz arr[1] value compiler take at compile time after assign the value in arr[1]..so 5+2..

soni abhishek said...

here compiler do optimization technique so its take latest value..compiler do optimization for faster execution....so its take i as 0..so 1*10+2*10+3*10=60

abhishek palrecha said...

Answer of question 6 is 110 which is not in options.
Explanation : 1*a[0] + 2*a[1] + 3*a[2]
10 + 40 + 60=110
Question was :
#include
int main(){
int a[]={10,20,30,40};
int i=3,x;
x=1*a[--i]+2*a[--i]+3*a[--i];
printf("%d",x);
return 0;
}

Aldane M said...

hello anyone online can help me plzzz

Aldane M said...

#include
#define MAX 1000

int main(){
char binaryNumber[MAX],hexaDecimal[MAX];
long int i=0;

printf("Enter any hexadecimal number: ");
scanf("%s",hexaDecimal);

printf("\nEquivalent binary value: ");
while(hexaDecimal[i]){
switch(hexaDecimal[i]){
case '0': printf("0000"); break;
case '1': printf("0001"); break;
case '2': printf("0010"); break;
case '3': printf("0011"); break;
case '4': printf("0100"); break;
case '5': printf("0101"); break;
case '6': printf("0110"); break;
case '7': printf("0111"); break;
case '8': printf("1000"); break;
case '9': printf("1001"); break;
case 'A': printf("1010"); break;
case 'B': printf("1011"); break;
case 'C': printf("1100"); break;
case 'D': printf("1101"); break;
case 'E': printf("1110"); break;
case 'F': printf("1111"); break;
case 'a': printf("1010"); break;
case 'b': printf("1011"); break;
case 'c': printf("1100"); break;
case 'd': printf("1101"); break;
case 'e': printf("1110"); break;
case 'f': printf("1111"); break;
default: printf("\nInvalid hexadecimal digit %c ",hexaDecimal[i]); return 0;
}
i++;
}

return 0;
}




WHY USE A CHAR DATA TYPE TO STORE THE BINARY VALUE

nanduri venkatesh said...

In Linux GCC complier
Output: 90

Consider on expression:
= 1 * a[--i] + 2 * a[--i] + 3 * a[--i] //i = 3 - 2
= 1 * a[--i] + 2 * a[--i] + 3 * a[--i] //i = 2 - 1
= 1 * a[1] + 2 * a[1] + 3 * a[--i] //i = 1 - 1
= 1 * a[1] + 2 * a[1] + 3 * a[0]
= 1 * 20 + 2 * 20 + 3 * 10
= 20 + 40 + 30
= 90


i think this solution is wrong... can any one explain it...
gcc compiler output:100

Techblog said...

nice post

sri said...

Explanation : 1*a[2] + 2*a[1] + 3*a[0]
30 + 40 + 30=100
Question was :
#include
int main(){
int a[]={10,20,30,40};
int i=3,x;
x=1*a[--i]+2*a[--i]+3*a[--i];
printf("%d",x);
return 0;
}