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:

The default storage class of a local variable in C is 'auto'. The scope of 'auto' variables is limited to the block in which they have been declared. When the program control exits the scope, 'auto' variables become inactive or 'dead,' and they are re-initialized upon re-entering the scope.

It's important to note that if two variables share the same name but have different scopes, the local variable will take precedence over the outer variable with the same name.


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 in C are stored in CPU registers and, as such, they do not have a memory address. Therefore, attempting to take the address of a register variable, as in '&a', is incorrect.

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' variables outside of any function in C since 'auto' variables are created, i.e., allocated memory, at runtime. They are local to a function and don't have a meaningful existence outside the scope of a function.

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 in C are stored in CPU registers and, as such, do not have a memory address. Therefore, attempting to obtain the address of a register variable, as in '&a', is incorrect.

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 the sizeof operator does not change the value of any variable. Therefore, the value of the variable 'i' will remain 3. After the evaluation of the expression inside the sizeof operator, we obtain an integer value. Consequently, the value of the variable 'val' will be the size of the int data type.

Note: The size of an int in Turbo C 3.0 and 4.5 is two bytes, while in the Linux GCC compiler, it is four bytes.


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: The increment operator '++' should not have a space between the two plus symbols.

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 the operation of any operator on an operand, it returns a constant value. In this case, we are performing the unary minus operator on the variable 'i,' so it will return a constant value, and we cannot perform the ++ operation on a 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: The '==' is a logical operator that 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: The pre-increment operator first increments the variable and then assigns the value, while the post-increment operator first assigns the value and then increments the variable.
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:

The string constant "abcde" will return the memory address of the first character of the string constant. The expression *"abcde" will return the value of the first character of the 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:

The memory address of string constants depends on the operating system.

Total correct answers:
Total incorrect answers:
Total not attempted questions:
Marks obtained:
Result:

These are objective type questions and answers for a C programming language online test or quiz, designed for interviews or written tests. If you have any queries or suggestions, please feel free to share them.

39 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.

Unknown 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

Unknown said...

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

Unknown said...

will you provide certificate for this test if we pass

Unknown said...

so why turbo c preferred here

Unknown 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

Unknown said...

Checking

Unknown 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 Bandarupalli said...
This comment has been removed by the author.
Anonymous said...

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

Anonymous said...

dev c++ gives 130

ECE IONS said...

first verify ur ans ,,lots of mistake

Unknown said...

rishi kahila BANDA haichi

Unknown said...

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

Unknown said...

ok

Unknown said...

Level is much better ...
Thanks...

Unknown said...

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

Unknown said...

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

Unknown 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

Unknown said...

if u include proper header file than output is 85.

Unknown said...

Pointer arithmetic cannot be performed on which pointer?

Unknown 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..

Unknown 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

Unknown 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;
}

Anonymous said...

hello anyone online can help me plzzz

Anonymous 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

Unknown 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

Dhanu shetty 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;
}

faegonjahnke said...

Borgata Hotel Casino & Spa - MapYRO
Hotel AddressPhone/PhoneAddress: 020 Casino Drive, 정읍 출장마사지 Baltic, MA, 89022City: United StatesOpening hours: 4:00 a.m. | Fri, Dec 17Previous 당진 출장마사지 names: 광양 출장안마 Borgata Hotel Casino & Spa, 양산 출장마사지 $28 서산 출장샵 million