Online C Programming Test - C Programming Test 8
- This is a FREE online test. Beware of scammers who ask for money to attend this test.
- Total number of questions: 20.
- Time allotted: 20 minutes.
- Each question carries 1 mark; there are no negative marks.
- DO NOT refresh the page.
- All the best!
Marks : 2/20
Test Review : View answers and explanation for this test.
The switch/case statement in the c language is defined by the language specification to use an int value, so you can not use a float value.
switch( expression )
{
case constant-expression1: statements 1;
case constant-expression2: statements 2;
case constant-expression3: statements3 ;
...
...
default : statements 4;
}
The value of the 'expression' in a switch-case statement must be an integer, char, short, long. Float and double are not allowed.
#include<stdio.h>
int main()
{
int a = 10, b;
a >=5 ? b=100: b=200;
printf("%d\n", b);
return 0;
}
Variable b is not assigned.
It should be like:
b = a >= 5 ? 100 : 200;
#include<stdio.h>
int main()
{
int x = 30, y = 40;
if(x == y)
printf("x is equal to y\n");
else if(x > y)
printf("x is greater than y\n");
else if(x < y)
printf("x is less than y\n")
return 0;
}
This program will result in error "Statement missing ;"
printf("x is less than y\n") here ; should be added to the end of this statement.
#include<stdio.h>
int main()
{
int i=4, j=-1, k=0, w, x, y, z;
w = i || j || k;
x = i && j && k;
y = i || j &&k;
z = i && j || k;
printf("%d, %d, %d, %d\n", w, x, y, z);
return 0;
}
Step 1: int i=4, j=-1, k=0, w, x, y, z; here variable i, j, k, w, x, y, z are declared as an integer type and the variable i, j, k are initialized to 4, -1, 0 respectively.
Step 2: w = i || j || k; becomes w = 4 || -1 || 0;. Hence it returns TRUE. So, w=1
Step 3: x = i && j && k; becomes x = 4 && -1 && 0; Hence it returns FALSE. So, x=0
Step 4: y = i || j &&k; becomes y = 4 || -1 && 0; Hence it returns TRUE. So, y=1
Step 5: z = i && j || k; becomes z = 4 && -1 || 0; Hence it returns TRUE. So, z=1.
Step 6: printf("%d, %d, %d, %d\n", w, x, y, z); Hence the output is "1, 0, 1, 1".
1. | a <= 20 ? (b = 30): (c = 30); |
2. | (a <=20) ? b : (c = 30); |
No, the expressions 1 and 2 are not same.
1. a <= 20 ? (b = 30) : (c = 30); This statement can be rewritten as,
if(a <= 20)
{
b = 30;
}
else
{
c = 30;
}
2. (a <=20) ? b : (c = 30); This statement can be rewritten as,
if(a <= 20)
{
//Nothing here
}
else
{
c = 30;
}
False, The scope of macros is globals and functions. Also the scope of macros is only from the point of definition to the end of the file.
#include<stdio.h>
int main()
{
char *str;
str = "%d\n";
str++;
str++;
printf(str-2, 300);
return 0;
}
int i=0;
char *q=(char*)i;
#include<stdio.h>
int main()
{
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
printf("%u, %u\n", a+1, &a+1);
return 0;
}
Step 1: int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0}; The array a[3][4] is declared as an integer array having the 3 rows and 4 colums dimensions.
Step 2: printf("%u, %u\n", a+1, &a+1);
The base address(also the address of the first element) of array is 65472.
For a two-dimensional array like a reference to array has type "pointer to array of 4 ints". Therefore, a+1 is pointing to the memory location of first element of the second row in array a. Hence 65472 + (4 ints * 2 bytes) = 65480
Then, &a has type "pointer to array of 3 arrays of 4 ints", totally 12 ints. Therefore, &a+1 denotes "12 ints * 2 bytes * 1 = 24 bytes".
Hence, begining address 65472 + 24 = 65496. So, &a+1 = 65496
Hence the output of the program is 65480, 65496
strrev(s) Reverses all characters in s
Example:
#include <string.h>
#include <stdio.h>
int main(void)
{
char *str = "IndiaBIX";
printf("Before strrev(): %s\n", str);
strrev(str);
printf("After strrev(): %s\n", str);
return 0;
}
Output:
Before strrev(): IndiaBIX
After strrev(): XIBaidnI
#include<stdio.h>
#include<string.h>
int main()
{
char sentence[80];
int i;
printf("Enter a line of text\n");
gets(sentence);
for(i=strlen(sentence)-1; i >=0; i--)
putchar(sentence[i]);
return 0;
}
#include<stdio.h>
struct course
{
int courseno;
char coursename[25];
};
int main()
{
struct course c[] = { {102, "Java"},
{103, "PHP"},
{104, "DotNet"} };
printf("%d ", c[1].courseno);
printf("%s\n", (*(c+2)).coursename);
return 0;
}
struct emp
{
int ecode;
struct emp *e;
};
#include<stdio.h>
int main()
{
int i, fss;
char ch, source[20] = "source.txt", target[20]="target.txt", t;
FILE *fs, *ft;
fs = fopen(source, "r");
ft = fopen(target, "w");
while(1)
{
ch=getc(fs);
if(ch==EOF)
break;
else
{
fseek(fs, 4L, SEEK_CUR);
fputc(ch, ft);
}
}
return 0;
}
The file source.txt is opened in read mode and target.txt is opened in write mode. The file source.txt contains "To err is human".
Inside the while loop,
ch=getc(fs); The first character('T') of the source.txt is stored in variable ch and it's checked for EOF.
if(ch==EOF) If EOF(End of file) is true, the loop breaks and program execution stops.
If not EOF encountered, fseek(fs, 4L, SEEK_CUR); the file pointer advances 4 character from the current position. Hence the file pointer is in 5th character of file source.txt.
fputc(ch, ft); It writes the character 'T' stored in variable ch to target.txt.
The while loop runs three times and it write the character 1st and 5th and 11th characters ("Trh") in the target.txt file.
cmd> sample 1 2 3
/* sample.c */
#include<stdio.h>
int main(int argc, char *argv[])
{
int j;
j = argv[1] + argv[2] + argv[3];
printf("%d", j);
return 0;
}
Here argv[1], argv[2] and argv[3] are string type. We have to convert the string to integer type before perform arithmetic operation.
Example: j = atoi(argv[1]) + atoi(argv[2]) + atoi(argv[3]);
tcc myprog wildargs.obj
int *f();
#include<stdio.h>
int main()
{
char huge *near *far *ptr1;
char near *far *huge *ptr2;
char far *huge *near *ptr3;
printf("%d, %d, %d\n", sizeof(ptr1), sizeof(**ptr2), sizeof(ptr3));
return 0;
}