Question

In: Computer Science

How many iterations of the for loop does this program perform? int main(void) { int a[]...

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

Solutions

Expert Solution

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


Related Solutions

How many race conditions does attackers have to win in the following program? int main() {...
How many race conditions does attackers have to win in the following program? int main() { struct stat stat1, stat2; int fd1, fd2; if (access("/tmp/XYZ", O_RDWR)) { fprintf(stderr, "Permission denied\n"); return -1; } else fd1 = open("/tmp/XYZ", O_RDWR); if (access("/tmp/XYZ", O_RDWR)) { ' fprintf(stderr, "Permission denied\n"); return -1; } else fd2 = open("/tmp/XYZ", O_RDWR); // Check whether fd1 and fd2 have the same inode. fstat(fd1, &stat1); fstat(fd2, &stat2); if(stat1.st_ino == stat2.st_ino) { write_to_file(fd1); } else { fprintf(stderr, "Race condition detected\n");...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
REWRITE THE FOLLOWING CODES USING FOR LOOP. PLS USE C LANGUAGE FORMAT #include <stdio.h> int main(void)...
REWRITE THE FOLLOWING CODES USING FOR LOOP. PLS USE C LANGUAGE FORMAT #include <stdio.h> int main(void) {      int count ; scanf("%d",&count);           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                            return 0; }
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {     ...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {      int count =2; // COUNT TAKEN 2 AS TO PRINT 2 TIMES           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                                 return 0; }
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
public void printNumbers(int low, int high) { // using a for loop, print all numbers from...
public void printNumbers(int low, int high) { // using a for loop, print all numbers from low to high } public int sumOfNumbers(int n) { // using a for loop, calculate and return the sum of first n numbers // i.e n = 5, answer = 5+4+3+2+1 = 15 } public void printMultiplicationTable(int num) { // using a for loop, print the multiplication table of num (up to first 10!) // i.e. num = 5, 5*1=5, 5*2=10, 5*3=15, 5*4=20, 5*5=25,...
public static void main(String [] args)     {         int[] a = new int[20];         a[0]...
public static void main(String [] args)     {         int[] a = new int[20];         a[0] = 0;         a[1] = 1;         for(int i = 2; i < 20; i++){             a[i] = a[i - 1] + a[i - 2];         }         for(int i = 0; i < a.length; i++)             System.out.println(a[i]);     } what would be the code when you convert this code, which is java into assembly code? Convert java into assembly code for the code...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT