In: Computer Science
How many iterations of the for loop does this program perform?
int main(void) {
int a[] = {1, 2, 3, 4};
int i;
for (i = 0; i < 4; i++) {
if (a[i] < 0) return 1;
else return 0;
}
return 0;
}
Question 1 options:
| A |
No iterations. |
| B |
The program crashes. |
| C |
One. |
| D |
The program does not compile. |
| E |
Four. |
Question 2
What does the following program do?
int main(void) {
int i, a[];
a[0]=1;
for (i=0; i<10; i++) {
a[i] = a[i-1] + 2*i + 1;
}
return 0;
}
Question 2 options:
| A |
It crashes when it is run because no memory is reserved for the array. |
| B |
It does not compile. |
| C |
It fills the array with the squares of the integers from 1 to 10. |
| D |
It fills the array with the first 10 powers of 2. |
Question 3
What's the problem with this code?
int main(void) {
int i, a[5];
for (i=1; i <= 5; i++) {
a[i] = i+1;
}
return 0;
}
Question 3 options:
| A |
The main problem is that the loop accesses a non-existing a[5]. This will cause some memory location to be inadvertently written. The secondary problem is that a[0] is left uninitialized. |
| B |
It doesn't compile because one cannot declare an int variable and an int array on the same line. |
| C |
There is no problem. The code will run just fine. |
| D |
It crashes because a[1] is accessed before a[0]. |
Question 4
If a is declared with
int a[10];
what value does sizeof(a) have?
Question 4 options:
| A |
14 |
| B |
Whatever is the size of a pointer to an integer. |
| C |
10 |
| D |
10*sizeof(int) |
Question 5
What does the following program do?
#include
int main(void) {
int a[] = {1,2,3,4};
int b[4]; b = a;
printf("%4d\n", b);
return 0;
}
Question 5 options:
| A |
It prints 1 2 3 4. |
| B |
The program incurs a segmentation fault when run. |
| C |
It does not compile. You cannot copy arrays like that. |
| D |
It does not compile. You cannot print an entire array like that. |
question 6
Arrays are just pointers in disguise.
Question 6 options:
| A | True |
| B | False |
Question 7
Declaring too large an array in a function may cause a stack overflow when the function is called.
Question 7 options:
| A | True |
| B | False |
Question 8
What does the following program do?
int main(void) {
int a[4];
a = {1,2,3,4};
return a[3];
}
Question 8 options:
| A |
It crashes because main can only return 0. |
| B |
It returns 3. |
| C |
It returns 4. |
| D |
It does not compile. |
Question 9
What does the following program do?
#include
int main(void) {
int i = 2;
int a[] = {0,1,2,3,4};
int n = sizeof(a) / sizeof(a[0]);
for (i = 0; i < n; i++) {
(*(a+i))++;
printf(" %d", a[i]);
}
printf("\n");
return 0;
}
Question 9 options:
| A |
It does not compile. |
| B |
It prints: 1 1 1 1 1 |
| C |
Prints: 1 2 3 4 5. |
| D |
It crashes because it causes a segmentation fault. |
Question 10
What does the following program do?
#include
int main(void) {
int * p;
int a[] = {0,1,2,3,4};
int n = sizeof(a) / sizeof(a[0]);
for (p = a; p != a+n; p++) {
printf(" %d", *p);
}
printf("\n");
return 0;
}
Question 10 options:
| A |
It crashes because p != a+n accesses an address that isout of bounds. |
| B |
It doesn't compile. You cannot assign an array to a pointer because they are of different types. |
| C |
It prints nonsense because %d is for integers, while p is a pointer. |
| D |
It prints: 0 1 2 3 4 |
Q1:
C.One
Explanation:When it enters in loop for first time,it checks condition then it fails and cause to return 0.Hence it terminates loop as well as code
====================================================================
Question 2:
Ans:
| A |
It crashes when it is run because no memory is reserved for the array. expanation:It is neccessary to give size of array otherwise causes syntax error =============================================================================== |
Q3:
Ans:
| C |
There is no problem. The code will run just fine. Explanation :As there is no syntax error ,code runs fine .There may be run time error or exception when we try do operations on array ================================================================================= Q.4 Ans:d 10sizeof(int) Expanation:Above will give 40 because 10 * 4(size of int is 4) which is same as sizeof(a) as a is array of 10 integers |