PSA Ques Objective
PSA Ques Objective
{
// size of int is 2 Bytes
int a=32768;
unsigned int b=65536;
printf("%d\n%d",a,b);
}
Output :
-32768
0
#include<stdio.h>
int main()
{
printf(“%d”, printf(“%s”,”Hello World!”));
return 0;
}
Out Put:-
Hello World!12
int main()
{
printf(“%10s”,”Hello”);
return 0;
}
Out Put:-
Hello
int main()
{
char ch = 255;
ch = ch + 10;
printf(“%d”,ch);
return 0;
}
Out Put:-
9
Which of the Following is correct.
1. signed int I;
2. signed I;
3. unsigned I;
4. long l;
5. long int I;
6. long long I;
int main() {
int var = 052;
printf(“%d”,var);
return 0;
}
Out Put:- 42
int main() {
int var = 052;
printf(“%o”,var);
return 0;
}
Out Put:- 52
int main() {
int var = 0x43FF;
printf(“%x”,var);
return 0;
int main() {
int var = 0x43FF;
printf(“%X”,var);
return 0;
}
void main()
{
char a=128;
unsigned char b=256;
printf("%d \n %d",a,b);
}
Output :
-128
0
void main()
{
printf("%d%d%d%d",6/5,-6/5,6/-5,-6/-5);
}
Output:-
1-1-11
void main()
{
printf("%d%d%d%d",6%5,-6%5,6%-5,-6%-5);
}
Output:-
1-11-1
0
void main()
{
int a=2,b=3,c=1,d;
d=a<b<c-1;
printf("%d",d);
}
Output:-
0
void main()
{
int a=10,b=20,c=30;
c=a==b;
printf("%d%d%d",a,b,c);
}
Output:- If Condition is true then print 1 else
print 0.
10 20 0
void main() {
int a=012,b=034;
int x=0x12,y=0x34;
int c,d,u,v;
c=a&b;
d=a|b;
u=x&y;
v=x|y;
printf("%d\t%d\t%d\t%d",c,d,u,v);
}
Output:-
8 30 16 54
void main() {
int a=012,b=034;
int x=0x12,y=0x34;
int c,d,u,v;
c=a&&b;
d=a||b;
u=x&&y;
v=x||y;
printf("%d\t%d\t%d\t%d",c,d,u,v);
}
Output:-
1 1 1 1
void main()
{
int c=10,d,e;
d=!c;
e=~c;
printf("%d\t%d",d,e);
}
Output:-
0 -11
void main()
{
int i=10,j=11,k,l;
k=i++ + j;
l=i++ + ++j;
printf("%d\t%d",l,k);
}
Output:-
23 21
void main()
{
int i=10,j=11,k,l;
k=i + ++j;
l=i++ + ++j;
printf("%d\t%d",l,k);
}
Output:-
23 22
void main()
{
int x=32767;
printf("%d",x<<1);
}
Output:-
65534
void main() {
float u=3.5;
int v,w,x,y;
v=(int)(u+0.5);
w=(int)u+0.5;
x=(int)((int)u+0.5);
y=(u+(int)0.5);
printf("%d\t%d\t%d\t%d",v,w,x,y);
}
Output:-
4 3 3 3
void main()
{
printf("value is = %d",(10++));
}
Error : L-value required
printf("%d,%d,%d\n",sizeof(a),sizeof(b),sizeof(125.50));
printf("%d,%d\n",sizeof(c),sizeof(65));
}
Output :
4,4,8
1,4
int main()
{
short int i = 20;
char c = 97;
printf("%d, %d, %d\n", sizeof(i), sizeof(c),
sizeof(c + i));
return 0;
}
Output :
2, 1, 4
void main()
{
char a = 'a';
int x = (a % 10)++;
printf("%d\n", x);
}
Error : L-value required
}
➢Output :
}
Output :
This will always get executed
void main()
{
if(printf("Hello"))
printf("Students");
}
Output :
HelloStudents
void main()
{
if(!printf(""))
printf("Okkk");
else
printf("Hiii");
}
Output :
Okkk
void main()
{
char val=1;
if(val--==0)
printf("TRUE");
else
printf("FALSE");
}
Output :
FALSE
void main()
{
int a=10;
if(a==10)
{
printf("Hello...");
break;
printf("Ok");
}
else
{
printf("Hii");
} }
Output :
Error : misplaced break/ illegal break
A break statement can be used with looping and
switch statements.
int main()
{
if( (-100 && 100)||(20 && -20) )
printf("%s","Condition is true.");
else
printf("%s","Condition is false.");
return 0;
}
Output :
Condition is true.
Any non zero value is treated as true for
conidion.
void main()
{ int TRUE=1;
if(TRUE)
printf("1");
printf("2");
else
printf("3");
printf("4");
}
Output :
[Error] 'else' without a previous 'if'
void main()
{
int a=100;
if(a>20)
if(a<20)
printf("Hello");
else
printf(“Bye");
}
Output :
Bye
void main()
{
int expr=10;
switch(expr)
printf("This is valid but will not get
executed");
}
Output :
No output
void main()
{
float expr=2.0;
switch(expr)
{
case 1:printf("One");
case 2:printf("Two");
default:printf("default");
}
}
Output :
Error : switch quantity not an integer
void main()
{
int expr=2,j=1;
switch(expr)
{
case j:
printf("This is case 1");
case 2:
printf("This is case 2");
default:
printf("This is default case"); }}
Output :
[Error] case label does not reduce to an integer
constant
void main()
{ char ch='A';
switch(ch)
{
case 'A':
printf("Case label is A");
case "B":
printf("Case label is B");
}
}
Output :
[Error] case label does not reduce to an integer
constant
void main()
{
int expr=1;
switch(expr)
{
case 1:printf("One\n");
case 2:printf("Two\n");
default:printf("Three\n");
}
}
Output :
One
Two
Three
void main()
{
int expr=3;
switch(expr)
{
default:printf("Three\n");
case 1:printf("One\n");
case 2:printf("Two\n");
}
}
Output :
Three
One
Two
void main()
{ int expr=2;
switch(expr)
{
case 1:
printf("This is case 1");
case 2-1:
printf("This is case 2");
}
}
Output :
[Error] duplicate case value
[Error] previously used here
void main()
{
int i=1,j=3;
switch(i)
{
case 1:
printf("This is outer case 1\n");
switch(j)
{
case 3:
printf("This is inner case 1\n");
break;
default:
printf("This in inner default case");
}
case 2:
printf("This is outer case 2");
}
}
Output :
This is outer case 1
This is inner case 1
This is outer case 2
void main()
{
int expr=2;
switch(expr)
{
case 1:
printf("This is case 1");
break;
case 2:
printf("This is case 2");
continue;
default:
printf("Default");
}
}
Output :
[Error] continue statement not within a loop
void main()
{
int i=1;
loop:
printf("%d",i++);
if(i==5)
break;
goto jump;
}
Output :
[Error] break statement not within loop or
switch.
[Error] label 'jump' used but not defined
void main()
{
short day=2;
switch(day)
{
case 2: || case 22:
printf("%d nd",day);
break;
default:
printf("%d th",day);
break;
}
}
Output :
Syntax Error
We can not use || operator between case
statements, case 2:||case 22: invalid
void main()
{
int x;
float y=7.0;
switch(x=y+1)
{
case 8: printf("It's Eight."); break;
default: printf("Oops No choice here!!!");
}
}
Output :
It's Eight.
Here, x=y+1 will be 8 , because x is an integer
variable so final value that will return through
this expression is 8.
void main()
{
int i=1;
loop:
printf("%d",i++);
if(i==5)
goto out;
goto loop;
out:
;
}
Output :
1234
main()
{
int i= 3;
goto label;
for(i=0;i<5;i++)
{
label:
printf("%d",i);
}
}
Output :
34
main()
{
int i=1;
while(i<=5)
printf("%d",i);
printf("The value of i after the loop is %d",i);
}
Output:-
1 infinite times
void main()
{
int n=0,a=5,b=10;
while(n<=(a^b))
{
n++;
}
printf("%d",n);
}
Output :
16
void main()
{
int a=16,n=0;
while(n<=~(~a))
{
n++;
}
a=n;
printf("%d",~a);
}
Output :
-18
main()
{
int i=1;
for( )
{
printf("%d",i);
if(i=5)
break;
}
}
Output:-
1 To 32767
void main()
{
int i=1;
for(;;)
{
printf("%d",i);
if(i==5)
break;
}
}
Output:-
1 infinite times
void main()
{
int i=1;
for(;;)
{
printf("%d",i);
if(i=5)
break;
}
}
Output:-
1
void main()
{
int i=1;
for(;i<=5;printf("%d",i++));
}
Output:-
12345
void main()
{
int i=1;
for(;i<=10;i++)
{
if(i%2==0)
continue;
printf("%d",i);
}
}
Output:-
13579
void main()
{
int i,j;
for(i=1;i<3;i++)
for(j=1;j<4;j++)
{
if(i==2)break;
printf("%d%d\n",i,j);
}
}
Output:-
11
12
13
void main()
{
int i,j;
for(i=1;i<3;i++)
for(j=1;j<4;j++)
{
if(i==2)continue;
printf("%d%d\n",i,j);
}
}
Output:-
11
12
13
void main()
{
int i=0;
for(;++i;)
printf("%d",i);
}
Output:-
1234…….up to infinite
void main()
{
int i=0;
for(;i++;)
printf("%d",i);
}
Output:-
Nothing is printed
void main()
{
int i=3,j=3;
for(;i<6,j<4;i++,j++)
printf("%d%d\n",i,j);
}
Output:-
33
void main()
{
int i=5;
do
{
printf("%d",i);
i++;
}while(i<10);
}
Output:-
56789
void main()
{
int i=5;
do
{
printf("%d",i);
i++;
}while(i<0);
}
Output:-
5
int main()
{
int i=2,j=2;
while(i+1?--i:j++)
printf("%d",i);
return 0;
}
Output:-
1
int main()
{
int i,j;
i=j=2,3;
while(--i&&j++)
printf("%d %d",i,j);
return 0;
}
Output:-
1 3
int main()
{
int i;
for(i=0;i<=5;i++);
printf("%d",i);
return 0;
}
Output:-
6
int main()
{
char c=125;
do
printf("%d ",c);
while(c++);
return 0;
}
Output:-
C++ 0 1 2 3
int main(){
int m=10;
int x=printf("%d ",m);
printf("%d",x);
return 0;
}
Output:-
10 3
41
Gargabe value
void main()
{
int a;
a=printf("Hello")+printf("Readers!!");
printf("\n%d characters printed",a);
}
Output:-
HelloReaders!!
14 characters printed
void main()
{
int a=10,b=20,c;
c=add(a,b);
printf("The result after addition is %d",c);
}
int add(int a,int b)
{
return a+b;
}
Output:-
6 times MENU
#include<stdio.h>
main()
{
int const a = 5;
a++;
printf(“%d”,a);
}
Ans:- Compile error
#include<stdio.h>
main()
{
const int a = 5;
a++;
printf("%d", a);
}
Ans:- Compile error
#include<stdio.h>
main()
{
char s[]="hello", t[]="hello";
if(s==t){
printf("eqaul strings");
}
}
Ans:- No output
#include<stdio.h>
main()
{
int a = 1;
float b = 1.3;
double c;
c = a + b;
printf("%.2lf", c);
}
Ans:- 2.30
#include<stdio.h>
main()
{
char c = 'A'+256;
printf("%c", c);
}
Ans:- A
main()
{
int x = 5;
if(x==5)
{
if(x==5) break;
printf("Hello");
}
printf("Hi");
}
Ans:- Compile error
#include<stdio.h>
main()
{
int x = 5;
if(x=5)
{
if(x=5) break;
printf("Hello");
}
printf("Hi");
}
Ans:- Compile error
#include<stdio.h>
main()
{
int x = 5;
if(x=5)
{
if(x=5) printf("Hello");
}
printf("Hi");
}
Ans:- HelloHi
#include<stdio.h>
main()
{
for(1;2;3)
printf("Hello");
}
Ans:- Infinite loop
#include<stdio.h>
void f()
{
static int i;
++i;
printf("%d", i);
}
main()
{
f();
f();
f();
}
Ans:- 1 , 2 , 3
• What is right way to Initialize array?
return 0;
}
Answer: Compile Time Error
return 0;
}
Answer: 2004 , 2020
Name of array in C gives the address(except in sizeof operator) of the first
element. Adding 1 to this address gives the address plus the sizeof type the
array has. Applying the Address-of operator before the array name gives the
address of the whole array. Adding 1 to this address gives the address plus
the sizeof whole array.
void print(int arr[])
{
int n = sizeof(arr)/sizeof(arr[0]);
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
}
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
print(arr);
return 0;
}
Answer:
According to 32 bit compiler : 1
According to 64 bit compiler : 1, 2
int main()
{
int i;
int arr[5] = {1};
for (i = 0; i < 5; i++)
printf("%d ", arr[i]);
return 0;
}
Answer: 1 0 0 0 0
int main()
{
int i;
int arr[5] = {0};
for (i = 0; i <= 5; i++)
printf("%d ", arr[i]);
return 0;
}
0 0 0 0 0 GV
# include <stdio.h>
int main()
{
char str1[] = "HelloQuiz";
char str2[] = {'H', 'e', 'l', 'l', 'o', 'Q', 'u', 'i', 'z'};
int n1 = sizeof(str1)/sizeof(str1[0]);
int n2 = sizeof(str2)/sizeof(str2[0]);
printf("n1 = %d, n2 = %d", n1, n2);
return 0;
}
The size of str1 is 10 and size of str2 9.
#include <stdio.h>
int main()
{
char arr[] = “HelloQuiz";
printf("%s", ?);
return 0;
}
Answer: (arr+5)
int main()
{
int a[][] = {{1,2},{3,4}};
int i, j;
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
printf("%d ", a[i][j]);
return 0;
}
Compile Time Error
void fun(int n)
{
int idx;
int arr1[n] = {0};
int arr2[n];
int main()
{
fun(4);
return 0;
}
Compile time error.
There’s no issue with definition of arr1 and arr2. In definition of these arrays,
the mention of array size using variable is ok as per C standard but these
types of arrays can’t be initialized at the time of definition. That’s why
initialization of arr1 is incorrect. But initialization of arr2 is done correctly
int size = 4;
int arr[size];
int main()
{
if(arr[0])
printf("Initialized to ZERO");
else
printf("Not initialized to ZERO");
return 0;
}
Compile Time Error
# include <stdio.h>
int main ()
{
char a [6] = "world";
int i, j;
for (i = 0, j = 5; i < j; a [i++] = a [j--])
printf ("%s", a);
}
# include <stdio.h>
int main ()
{
char a [6] = "world";
int i, j;
for (i = 0, j = 5; i < j; a [i++] = a [j--]);
printf ("%s", a);
}
No Out Put
#include <stdio.h>
int main()
{
char str[] = "%d %c", arr[] = "HelloQuiz";
printf(str, 0[arr], 2[arr + 3]);
return 0;
}
72 Q
#include<stdio.h>
int main()
{
char str[20] = "HelloQuiz";
printf ("%d", sizeof(str));
return 0;
}
20
#include <stdio.h>
int main()
{
char *str1 = "GeeksQuiz";
char str2[] = "GeeksQuiz";
return 0;
}
32 bit : 4 10
64 bit : 8 10
#include <stdio.h>
int main ()
{
int i, j;
int a [8] = {1, 2, 3, 4, 5, 6, 7, 8};
for(i = 0; i < 3; i++) {
a[i] = a[i] + 1;
i++;
}
i--;
for (j = 7; j > 4; j--) {
int i = j/2;
a[i] = a[i] - 1;
}
printf ("%d, %d", i, a[i]);
}
3,2
#include <stdio.h>
int main()
{
printf("%d", main);
return 0;
}
Explanation: Name of the function is actually a pointer variable to the
function and prints the address of the function. Symbol table is implemented
like this.
int main()
{
int i;
char s[]= "hello";
for(i=0; s[i]; ++i)
printf("%d ",i);
i=0;
printf("\n");
while(s[i++]);
printf("%d ",i);
}
01234
6
int main()
{
char ch[50],ch1[50]="hello";
ch = ch1;
printf("%s",ch);
}
Compile Time Error
int main()
{
char *s = "Abc";
while(*s)
printf("%c",*s++);
}
Abc
int main()
{
char s[] = "Fine";
*s = 'N';
printf("%s",s);
}
Nine
int main()
{
int arr[] = {10,20,30};
printf("%d",*arr+1);
}
11
void dynamic(int s, ...)
{
printf("%d ", s);
}
int main()
{
dynamic(2, 4, 6, 8);
dynamic(3, 6, 9);
return 0;
}
In c three continuous dots is known as ellipsis which is variable
number of arguments of function. The values to parameters are
assigned one by one
int fun()
{
static int num = 16;
return num--;
}
int main()
{
for(fun(); fun(); fun())
printf("%d ", fun());
return 0;
}
14 11 8 5 2
void fun(int x)
{
x = 30;
}
int main()
{
int y = 20;
fun(y);
printf("%d", y);
return 0;
}
20
void fun(int *x)
{
*x = 30;
}
int main()
{
int y = 20;
fun(&y);
printf("%d", y);
return 0;
}
30
int main()
{
int *ptr;
int x;
ptr = &x;
*ptr = 0;
ptr = &x;
*ptr = 0;
*ptr += 5;
printf(" x = %d\n", x);
printf(" *ptr = %d\n", *ptr);
Return 0;
}
5 5
int main()
{
int *ptr;
int x;
ptr = &x;
*ptr = 0;
*ptr += 5;
(*ptr)++;
printf(" x = %d\n", x);
printf(" *ptr = %d\n", *ptr);
return 0;
}
6 6
int main()
{
int arri[] = {1, 2 ,3};
int *ptri = arri;
return 0;
}
12
8
3
8
#include<stdio.h>
int main()
{
int arr[] = {10, 20, 30, 40, 50, 60};
int *ptr1 = arr;
int *ptr2 = arr + 5;
printf("Number of elements between two pointer are: %d.\n",
(ptr2 - ptr1));
printf("Number of bytes between two pointers are: %d\n",
(char*)ptr2 - (char*) ptr1);
return 0;
}
5
20
void fun(int arr[])
{
int i;
int arr_size = sizeof(arr)/sizeof(arr[0]);
for (i = 0; i < arr_size; i++)
printf("%d ", arr[i]);
}
int main()
{
int i;
int arr[4] = {10, 20 ,30, 40};
fun(arr);
return 0;
}
10 , 20
#include<stdio.h>
main()
{
int *p = 15;
printf("%d",*p);
}
Ans:- Runtime error
#include<stdio.h>
main()
{
register int x = 5;
int *p;
p=&x;
x++;
printf("%d",*p);
Ans:- Compile error
#include<stdio.h>
main()
{
int x = 65, *p = &x;
void *q=p;
char *r=q;
printf("%c",*r);
}
Ans:- 65
Thank You