Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 1 1 pts How many iterations of the for loop does this program perform? int main(void) { int a[] = {1, 2, 3, 4};

Question 1 1 pts

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; }

Four.

One.

No iterations.

The program crashes.

The program does not compile.

Question 2 1 pts

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; }

It fills the array with the squares of the integers from 1 to 10.

It does not compile.

It crashes when it is run because no memory is reserved for the array.

It fills the array with the first 10 powers of 2.

Question 3 1 pts

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; }

It doesn't compile because one cannot declare an int variable and an int array on the same line.

There is no problem. The code will run just fine.

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.

It crashes because a[1] is accessed before a[0].

Question 4 1 pts

If a is declared with

int a[10];

what value does sizeof(a) have?

10*sizeof(int)

Whatever is the size of a pointer to an integer.

10

14

Question 5 1 pts

What does the following program do?

#include int main(void) { int a[] = {1,2,3,4}; int b[4]; b = a; printf("%4d ", b); return 0; }

It does not compile. You cannot copy arrays like that.

It prints 1 2 3 4.

The program incurs a segmentation fault when run.

It does not compile. You cannot print an entire array like that.

Question 6 1 pts

Arrays are just pointers in disguise.

True

False

Question 7 1 pts

Declaring too large an array in a function may cause a stack overflow when the function is called.

True

False

Question 8 1 pts

What does the following program do?

int main(void) { int a[4]; a = {1,2,3,4}; return a[3]; }

It crashes because main can only return 0.

It does not compile.

It returns 4.

It returns 3.

Question 9 1 pts

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(" "); return 0; }

It does not compile.

It prints: 1 1 1 1 1

It crashes because it causes a segmentation fault.

Prints: 1 2 3 4 5.

Question 10 1 pts

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(" "); return 0; }

It doesn't compile. You cannot assign an array to a pointer because they are of different types.

It prints: 0 1 2 3 4

It crashes because p != a+n accesses an address that is out of bounds.

It prints nonsense because %d is for integers, while p is a pointer.

Four.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions