Sunteți pe pagina 1din 14

1.Arătați eroarea în program pe platforma pe 16 biți?

#include<stdio.h>

int main()
{
struct bits
{
int i:40;
}bit;

printf("%d\n", sizeof(bit));
return 0;
}

RASPUNS : Eroare: câmpul de biți este prea mare

2.
What is the output of the program?

#include<stdio.h>
int main()
{
extern int fun(float);
int a;
a = fun(3.14);
printf("%d\n", a);
return 0;
}
int fun(int aa)
{
return (int)++aa;
}

RASPUNS: 2 erori

1. Neconcordanța de tip în redeclararea FUN

2. Nesatisfacerea tipului în parametrul aa

Compile Error
E.

3. Indicați afirmația corectă vă va permite să accesați elementele matricei utilizând "p" în următorul
program?

#include<stdio.h>
#include<stdlib.h>

int main()
{
int i, j;
int(*p)[3];
p = (int(*)[3])malloc(3*sizeof(*p));
return 0;
}

RASPUNS CORECT C:

for(i=0; i<3; i++)


{
for(j=0; j<3; j++)
printf("%d", p[i][j]);
}

4. Indicați eroarea, dacă există în program.

#include<stdio.h>
int main()
{
int i = 1;
switch(i)
{
printf("This is c program.");
case 1:
printf("Case1");
break;
case 2:
printf("Case2");
break;
}
return 0;
}

RASPUNS: No Error and prints "Case1"

EXPLICATIE: SWICH (i) devine SWICH (1), apoi cazul 1: bloc este executat. Prin urmare, aceasta imprimă
"Case1".printf ("Acesta este un program c"); este ignorat de compilator.

Prin urmare, nu există nici o eroare și imprimă "Case1".

INVATA : https://www.indiabix.com/c-programming/control-instructions/

5. What will be the output of the program ?


#include<stdio.h>

int main()
{
struct byte
{
int one:1;
};
struct byte var = {1};
printf("%d\n", var.one);
return 0;
}

RASPUNS: -1

INVATA : https://www.indiabix.com/c-programming/structures-unions-enums/

6. What will be the output of the program?


#include<stdio.h>
#define SQUARE(x) x*x

int main()
{
float s=10, u=30, t=2, a;
a = 2*(s-u*t)/SQUARE(t);
printf("Result = %f", a);
return 0;
}

A. Result = -100.000000
EXPLICATIE : Funcția macro SQUARE (x) ,x * x calculează pătratul numărului dat "x". (Ex: 102)

Pasul 1: float s = 10, u = 30, t = 2, a; Aici variabilele s, u, t, a sunt declarate ca un tip de virgulă mobilă și
variabila s, u, t este inițializată la 10, 30, 2.

Step 2: a = 2*(s-u*t)/SQUARE(t); becomes,


=> a = 2 * (10 - 30 * 2) / t * t; Here SQUARE(t) is replaced by macro to t*t .
=> a = 2 * (10 - 30 * 2) / 2 * 2;
=> a = 2 * (10 - 60) / 2 * 2;
=> a = 2 * (-50) / 2 * 2 ;
=> a = 2 * (-25) * 2 ;
=> a = (-50) * 2 ;
=> a = -100;
Step 3: printf("Result=%f", a); It prints the value of variable 'a'.

INVATA: https://www.indiabix.com/c-programming/c-preprocessor/

7.THE OUTPUT

#include<stdio.h>
#define MAN(x, y) ((x)>(y)) ? (x):(y);

int main()
{
int i=10, j=5, k=0;
k = MAN(++i, j++);
printf("%d, %d, %d\n", i, j, k);
return 0;
}
RASPUNS SI EXPLIC: RASPUNS A

macroul MAN (x, y) ((x)> (y))? (X y); returnează cel mai mare număr de numere date.

Pasul 1: int i = 10, j = 5, k = 0; Variabila i, j, k este declarată ca un tip întreg și inițializată la valoarea 10, 5,
respectiv 0.

Pasul 2: k = MAN (++ i, j ++); devine,

=> k = ((+ i)> (j ++))? (++ i) :( j ++);

=> k = ((11)> (5))? (12) :( 6);

=> k = 12

Pasul 3: printf ("% d,% d,% d \ n", i, j, k); Se tipărește variabila i, j, k.

În pasul 2 de mai sus, valoarea variabilei i este incrementată cu 2, iar valoarea j variabilă este
incrementată cu 1.

Prin urmare, ieșirea programului este de 12, 6, 12

8.What will be the output of the program?


#include<stdio.h>
int main()
{
int i=2;
int j = i + (1, 2, 3, 4, 5);
printf("%d\n", j);
return 0;
}

Deoarece operatorul virgula a fost folosit în expresia i (1, 2, 3, 4, 5). Operatorul virgula are asociativitate
stânga-dreapta. Operandul stang este intotdeauna evaluat mai intai si rezultatul evaluarii este aruncat
inainte ca operandul drept sa fie evaluat. În această expresie 5 este cel mai mare operand drept, deci
după evaluarea expresiei (1, 2, 3, 4, 5) rezultatul este 5, care la adăugarea la i rezultă în 7.

Invata: https://www.indiabix.com/c-programming/expressions/
9. #include<stdio.h>

int main()
{
typedef int arr[5];
arr iarr = {1, 2, 3, 4, 5};
int i;
for(i=0; i<4; i++)
printf("%d,", iarr[i]);
return 0;
}

Raspuns: 1,2,3,4

Invata typedef : https://www.indiabix.com/c-programming/typedef/

10. Care dintre următoarele este ordinea corectă dacă apelați funcțiile din codul de mai jos?

a = f1 (23, 14) * f2 (12/4) + f3 ();

Raspuns: Comanda poate varia de la compilator la compilator

Explicatie :

Aici, înmulțirea se va întâmpla înainte de adăugarea, dar în ce ordine se vor numi funcțiile este
nedefinită. Într-o expresie aritmetică, paranteza spune compilatorului operanții care merg cu operatorii
care nu forțează compilatorul să evalueze mai întâi totul în paranteză.

Invata: https://www.indiabix.com/c-programming/expressions/

11.
Which of the following are unary operators in C?
1. !
2. sizeof
3. ~
4. &&
Raspuns: 1,2,3

O operație cu un singur operand se numește operație unară.

Operatori unari:

! Operatorul logic NOT.

~ operatorul NOT bitwise.

dimensiunea dimensiunii operatorului.

&& Logical AND este un operator logic.


Sizeof este mult folosit în limba de programare C. Este un operator unar de compilare care poate fi
folosit pentru a calcula dimensiunea operandului. Rezultatul dimensiunii este de tip unsigned_integral,
care este de obicei marcat cu size_t. sizeof poate fi aplicat oricărui tip de date, inclusiv tipuri primitive,
cum ar fi tipurile de puncte întregi și de tip floating point, tipuri de indicatori sau tipuri de date compuse
cum ar fi structura, uniunea etc.

12. #include<stdio.h>
int main()
{
int i=-3, j=2, k=0, m;
m = ++i && ++j && ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}

Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and
variable i, j, k are initialized to -3, 2, 0 respectively.
Step 2: m = ++i && ++j && ++k;
becomes m = -2 && 3 && 1;
becomes m = TRUE && TRUE; Hence this statement becomes TRUE. So it returns '1'(one). Hence
m=1.
Step 3: printf("%d, %d, %d, %d\n", i, j, k, m); In the previous step the value of i,j,k are
increemented by '1'(one).

13. Assuming, integer is 2 byte, What will be the output of the program?
#include<stdio.h>

int main()
{
printf("%x\n", -2<<2);
return 0;
}

The integer value 2 is represented as 00000000 00000010 in binary system.

Negative numbers are represented in 2's complement method.

1's complement of 00000000 00000010 is 11111111 11111101 (Change all 0s to 1 and 1s to 0).

2's complement of 00000000 00000010 is 11111111 11111110 (Add 1 to 1's complement to obtain
the 2's complement value).

Therefore, in binary we represent -2 as: 11111111 11111110.

After left shifting it by 2 bits we obtain: 11111111 11111000, and it is equal to "fff8" in hexadecimal
system.

14. #include<stdio.h>
int main()
{
int i=-3, j=2, k=0, m;
m = ++i || ++j && ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}

Pasul 1: int i = -3, j = 2, k = 0, m; aici variabilele i, j, k, m sunt declarate ca un tip întreg și variabilele i, j, k
sunt inițializate la -3, 2, respectiv 0.

Pasul 2: m = ++ i || ++ j && ++ k; aici (++ j && ++ k;) acest cod nu va fi executat deoarece ++ i are valoare
non-zero.

devine m = -2 || ++ j && ++ k;

devine m = TRUE || ++ j && ++ k; Prin urmare, această declarație devine TRUE. Deci se întoarce '1'
(unul). Prin urmare, m = 1.

Pasul 3: printf ("% d,% d,% d,% d \ n", i, j, k, m); În pasul anterior, valoarea variabilei "i" este
incrementată doar cu "1" (una). Variabila j, k nu este incrementată.

Prin urmare, ieșirea este "-2, 2, 0, 1".

16. #include<stdio.h>
int main()
{
int x=12, y=7, z;
z = x!=4 || y == 2;
printf("z=%d\n", z);
return 0;
}

Pasul 1: int x = 12, y = 7, z; aici variabilele x, y și z sunt declarate ca întreg și variabilele x și y sunt
inițializate la 12, respectiv 7.

Pasul 2: z = x! = 4 || y == 2;

devine z = 12! = 4 || 7 == 2;

atunci z = (condiție adevărată) || (condiție falsă); Prin urmare, returnează 1. Deci, valoarea z = 1.

Pasul 3: printf ("z =% d \ n", z); Prin urmare, ieșirea programului este "z = 1".

17. #include<stdio.h>
int main()
{
static int a[20];
int i = 0;
a[i] = i ;
printf("%d, %d, %d\n", a[0], a[1], i);
return 0;
}

Step 1: static int a[20]; here variable a is declared as an integer type and static. If a
variable is declared as static and it will be automatically initialized to value '0'(zero).
Step 2: int i = 0; here vaiable i is declared as an integer type and initialized to '0'(zero).
Step 3: a[i] = i ; becomes a[0] = 0;
Step 4: printf("%d, %d, %d\n", a[0], a[1], i);
Here a[0] = 0, a[1] = 0(because all staic variables are initialized to '0') and i = 0.
Step 4: Hence the output is "0, 0, 0".

18. #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;
}

tep 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".

19. #include<stdio.h>
int main()
{
int i=-3, j=2, k=0, m;
m = ++i && ++j || ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}

Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and
variable i, j, k are initialized to -3, 2, 0 respectively.
Step 2: m = ++i && ++j || ++k;
becomes m = (-2 && 3) || ++k;
becomes m = TRUE || ++k;.
(++k) is not executed because (-2 && 3) alone return TRUE.
Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1.
Step 3: printf("%d, %d, %d, %d\n", i, j, k, m); In the previous step the value of i,j are
increemented by '1'(one).
Hence the output is "-2, 3, 0, 1".

20. #include<stdio.h>
int main()
{
int x=4, y, z;
y = --x;
z = x--;
printf("%d, %d, %d\n", x, y, z);
return 0;
}

Step 1: int x=4, y, z; here variable x, y, z are declared as an integer type and variable x is
initialized to 4.
Step 2: y = --x; becomes y = 3; because (--x) is pre-decrement operator.
Step 3: z = x--; becomes z = 3;. In the next step variable x becomes 2, because (x--) is post-
decrement operator.
Step 4: printf("%d, %d, %d\n", x, y, z); Hence it prints "2, 3, 3".

21. #include<stdio.h>
int main()
{
int a=100, b=200, c;
c = (a == 100 || b > 200);
printf("c=%d\n", c);
return 0;
}

Step 1: int a=100, b=200, c;


Step 2: c = (a == 100 || b > 200);
becomes c = (100 == 100 || 200 > 200);
becomes c = (TRUE || FALSE);
becomes c = (TRUE);(ie. c = 1)
Step 3: printf("c=%d\n", c); It prints the value of variable i=1
Hence the output of the program is '1'(one).

22. #include<stdio.h>
int main()
{
int x=55;
printf("%d, %d, %d\n", x<=55, x=40, x>=10);
return 0;
}

Pasul 1: int x = 55; aici variabila x este declarată ca un tip întreg și inițializată la '55'.

Pasul 2: printf ("% d,% d,% d \ n", x <= 55, x = 40, x> = 10);

În printf, execuția expresiilor este de la dreapta la stânga.

aici x> = 10 returneaza TRUE, prin urmare, imprima '1'.


x = 40 aici x este atribuit la 40 Prin urmare, imprimă "40".

x <= 55 returnează TRUE. prin urmare, imprimă "1".

Pasul 3: Prin urmare, ieșirea este "1, 40, 1".

23.#include<stdio.h>
int main()
{
int i=2;
printf("%d, %d\n", ++i, ++i);
return 0;
}

Ordinea de evaluare a argumentelor transmise unui apel pentru funcții este nespecificată.

Oricum, considerăm că ++ i, + i sunt asociativitatea de la dreapta la stânga. Rezultatul programului este


4, 3.

În TurboC, producția va fi de 4, 3.

În GCC, producția va fi de 4, 4.

24. int main()


{
int k, num=30;
k = (num>5 ? (num <=10 ? 100 : 200): 500);
printf("%d\n", num);
return 0;

Pasul 1: int k, num = 30; aici variabilele k și num sunt declarate ca un tip întreg și variabila num este
inițializată la '30'.

Etapa 2: k = (num> 5 (num <= 10 - 100: 200): 500); Această declarație nu afectează ieșirea programului.
Pentru că vom imprima variabila num în următoarea instrucțiune. Deci, noi sămânțăm această
declarație.

Pasul 3: printf ("% d \ n", num); Se imprimă valoarea variabilei num '30'

Pasul 3: De aici rezultatul programului este '30'

25.
#include<stdio.h>

int main()
{
printf("%x\n", -1>>1);
return 0;

Negative numbers are treated with 2's complement method.

1's complement: Inverting the bits ( all 1s to 0s and all 0s to 1s)


2's complement: Adding 1 to the result of 1's complement.
Binary of 1(2byte) : 0000 0000 0000 0001
Representing -1:
1s complement of 1(2byte) : 1111 1111 1111 1110
Adding 1 to 1's comp. result : 1111 1111 1111 1111
Right shift 1bit(-1>>1): 1111 1111 1111 1111 (carry out 1)
Hexadecimal : f f f f
(Filled with 1s in the left side in the above step)
Note:
1. Fill with 1s in the left side for right shift for negative numbers.
2. Fill with 0s in the right side for left shift for negative numbers.
3. Fill with 0s in the left side for right shift for positive numbers.
4. Fill with 0s in the right side for left shift for positive numbers.

26. #include<stdio.h>

int main()
{
printf("%x\n", -1<<3);
return 0;
}

Assuming a integer 2-bytes, What will be the output of the program?


#include<stdio.h>

int main()
{
printf("%x\n", -1<<3);
return 0;
}

A. ffff

B. fff8

C. 0

D. -1

Answer: Option B
Explanation:
The system will treat negative numbers in 2's complement method.

Example:

Assume the size of int is 2-bytes(16 bits). The integer value 1 is represented as given below:
Binary of 1: 00000000 00000001 (this is for positive value of 1)

1's complement of binary 1: 11111111 11111110


2's complement of binary 1: 11111111 11111111

Thy system will store '11111111 11111111' in memory to represent '-1'.

If we do left shift (3 bits) on 11111111 11111111 it will become as given below:

11111111 11111111 ---(left shift 3 times)---> 11111111 11111000.

So, 11111111 11111000 ---(binary to hex)---> FF F8. (Required Answer)


As stated above, -1 is represented as '11111111 11111111' in memory.

So, the system will take 2's complement of '11111111 11111111' to the get the original negative
value back.

Example:

Bit Representation of -1: 11111111 11111111

Since the left most bit is 1, it is a negative number. Then the value is

1's complement: 00000000 00000000


2's complement: 00000000 00000001 (Add 1 to the above result)

Therefore, '00000000 00000001' = 1 and the sign is negative.

Hence the value is -1.

27. #include<stdio.h>

int main()
{
char c=48;
int i, mask=01;
for(i=1; i<=5; i++)
{
printf("%c", c|mask);
mask = mask<<1;
}
return 0;
}

Raspuns 12480

#include int r(); int main(){ for(r();r();r())


{ printf("%d
",r()); } return 0; } intr(){ int static num=
7; return num--; }
47
52
41
Feedback
Output: 5 2
Explanation:
First iteration:
Loop initial value: r() = 7
Loop condition: r() = 6
Since condition is true so printf function will print r() i.e. 5
Loop incrimination: r() = 4
Second iteration:
Loop condition: r() = 3
Since condition is true so printf function will print r() i.e. 2
Loop incrimination: r() = 1
Third iteration:
Loop condition: r() = 0
Since condition is false so program control will come out of the for loop

44.

int main()
{
char c=48;
int i, mask=01;
for(i=1; i<=5; i++)
{
printf("%c", c|mask);
mask = mask<<1;
}
return 0;

Here mask means unsigned int i.e no negative values....mask 01 means 32 bit value.it is
"00000000000000000000000000000001"
c=48 it is written as "00000000000000000000000000110000"
now "or" operation is performed then value becomes 49,
mask=mask<<1 means it shifts 1 to 1-bit before then it becomes
"000000000000000000000000000000010".now do o operation with 32digit binary bit of 48.

Now value becomes 50.again mask=mask<<1 "00000100"(last 8bits)similarly do till for loop
fails..then we get values as 49,50,52,56,48....the %c converts int to char thatmeans it prints the
ASCII values of these nunbers ASCII of 48=0,49=1,50=2......57=9..replace the values u get o/p
as 12480......

S-ar putea să vă placă și