8 Replies - 428 Views - Last Post: 19 April 2013 - 07:06 AM Rate Topic: -----

#1 TParker  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 30
  • Joined: 21-March 13

Quick Multiple Choice Problems

Posted 18 April 2013 - 05:06 PM

Hello,

I am taking an introduction to C course at college, and we were recently assigned homework problems that consisted of around 10 multiple choice problems. Since I can't post them below, I attached the problems to a PDF file. Below I will post my solutions. It would be greatly appreciated if you could help me out. Also, please do explain the answers because I am looking to actually understand the concepts

1) Not sure.

2) I have A because it is performing the formula 5-i, and the for statement is starting with i = 0 and ending with i=5.

3) I have B, since it is calling numbers from 0 to 9, then adding 1.

4) Not sure.

5) I have C. After 1 and 2, it will be forced to generate 3 zeros because x[5] is stating that the array has 5 integers.

6) I think it is C, but not sure.

7) I have A.

8) Not sure.

9) I have C because the statement is calling numbers from 0 to 5, and then adding 1.

10) I have D. a[0] = 1, a[1] = 2, and a[2] = 3.

Any hints would be great! Thanks again and have a good day!

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Quick Multiple Choice Problems

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: Quick Multiple Choice Problems

Posted 18 April 2013 - 06:07 PM

Before looking into the problems you don't know, I will tell you that your answer to number 9 is incorrect.
Was This Post Helpful? 0
  • +
  • -

#3 TParker  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 30
  • Joined: 21-March 13

Re: Quick Multiple Choice Problems

Posted 18 April 2013 - 07:34 PM

View Postjjl, on 18 April 2013 - 06:07 PM, said:

Before looking into the problems you don't know, I will tell you that your answer to number 9 is incorrect.


Alright, how about the other problems I answered? Also, if #9 is incorrect, does that also mean #3 is incorrect?
Was This Post Helpful? 0
  • +
  • -

#4 TParker  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 30
  • Joined: 21-March 13

Re: Quick Multiple Choice Problems

Posted 18 April 2013 - 07:46 PM

Thanks a lot btw!
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: Quick Multiple Choice Problems

Posted 18 April 2013 - 09:22 PM

3 is correct. For 9, you said the range of 1 <= i <=6 can be found using

rand() % 5 + 1;



However, rand % 5 will give you i in the range of 0 <= i <= 4; Adding one to that will give you a range of, 1 <= i <= 5.

The solution is

rand() % 6 + 1



For number 1, test the extreme cases to get the range. What is the result if rand() is 0? How about when rand() is RAND_MAX?

This post has been edited by jjl: 18 April 2013 - 09:24 PM

Was This Post Helpful? 0
  • +
  • -

#6 TParker  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 30
  • Joined: 21-March 13

Re: Quick Multiple Choice Problems

Posted 18 April 2013 - 09:42 PM

View Postjjl, on 18 April 2013 - 11:22 PM, said:

3 is correct. For 9, you said the range of 1 <= i <=6 can be found using

rand() % 5 + 1;



However, rand % 5 will give you i in the range of 0 <= i <= 4; Adding one to that will give you a range of, 1 <= i <= 5.

The solution is

rand() % 6 + 1



For number 1, test the extreme cases to get the range. What is the result if rand() is 0? How about when rand() is RAND_MAX?


For #1, following your advice, I got -5 to 5. Is this right?

Thanks a lot!

Also, now I am down to #4 and #8.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Quick Multiple Choice Problems

Posted 19 April 2013 - 06:42 AM

1) Given the statement:
int i = 10*(rand()/(double)RAND_MAX)-5;
i will be in the range:
a) -10 ≤ i ≤ 10
b) 10 ≤ i ≤ -5
c) -5 ≤ i ≤ 10
d) -5 ≤ i ≤ 5
e) None of the above

2) Given the declaration:
int i, x[5];
which loop will initialize x with the values { 5, 4, 3, 2, 1 }?
a) for( i = 0; i <= 5; ++i) x[i] = 5-i;
b) for( i = 0; i <= 5; ++i) x[i] = i;
c) for( i = 5; i <= 0; ++i) x[i] = i;
d) for( x = { 5, 4, 3, 2, 1}) ++i;
e) None of the above

3) Given the statement:
int i = rand() % 10 + 1;
i will be in the range:
a) 0 ≤ i ≤ 9
b) 1 ≤ i ≤ 10
c) 1 ≤ i ≤ 11
d) 0 ≤ i ≤ 11
e) None of the above

4) Given the declaration:
int i, z[6] ={ 0, 1 };
which loop will initialize z with the values { 0, 1, 1, 2, 3, 5 };
a) for( i = 2; i <= 5; ++i) x[i] += x[i-1];
b) for( i = 0; i <= 5; ++i) x[i] = i+1;
c) for( i = 1; i < 6; ++i) x[i] = i-1;
d) for( i = 2; i < 6; ++i) x[i] = x[i-1] + x[i-2];
e) None of the above

5) Which declaration will initialize array x with the values { 1, 2, 0, 0, 0 }?
a) int x = { 1, 2, 0, 0, 0 };
b) int x[] = { 1, 2, 0 };
c) int x[5] = { 1, 2 };
d) int x[] = { 0 };
e) None of the above

6) Given the statement:
double r = rand()/RAND_MAX+1;
r will be in the range:
a) 1 ≤ r ≤ RAND_MAX
b) 0 ≤ r < 1
c) 1 ≤ r ≤ 2
d) 0 ≤ r ≤ 2
e) None of the above

7) Which statements will produce r in the range: 1.0 ≤ r ≤ 2.0 ?
a) double r = rand(); r = r/RAND_MAX + 1;
b) double r = rand(); r = r*RAND_MAX + 1;
c) double r = rand(); r = r + r;
d) double r = rand(); r = 1/RAND_MAX + r;
e) None of the above

8) Which of the following function definitions correctly computes and return the sum of n elements of the array x?
a) int sum( int x, int n) { int i, r = 0; for( i = 0; i < n-1; ++i) r += x[i]; return r; }
b) int sum( int x[], int n) { int r = 0; while( n > 0) { --n; r += x[n]; } return r; }
c) int sum( int x[], int n) { int i, r = 0; while( i <= n) { r += x[i]; ++i; } return r; }
d) int sum( int x[], int n) { int i = 0, r = 0; for( i < n) r += sum( x, i-1); return r; }
e) None of the above

9) Which statement will produce i in the range: 1 ≤ i ≤ 6 ?
a) int i = 6 + rand() % 1;
b) int i = rand() * 6;
c) int i = rand() % 5 + 1;
d) int i = rand() % 6;
e) None of the above

10) Given the declaration:
int a[] = { 1, 2, 3};
what is the value of a[1]?
a) 0
b) 3
c) 1
d) 2
e) None of the above
Was This Post Helpful? 1
  • +
  • -

#8 TParker  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 30
  • Joined: 21-March 13

Re: Quick Multiple Choice Problems

Posted 19 April 2013 - 06:59 AM

^ Did you just re-post it?
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7962
  • View blog
  • Posts: 13,580
  • Joined: 19-March 11

Re: Quick Multiple Choice Problems

Posted 19 April 2013 - 07:06 AM

Looks like he did. Remember: text is the default format of the internet. Posting a picture is useless to someone who's blind, or who disables images for any number of good reasons. Text always works. Post text.

So for the problems you're not sure about, probably it's best for you to start by analyzing the statements and reasoning through them.
You could also try executing them, I suppose, and observing their behavior, but it's probably better to start with a hyptothesis.

For example:
1) Given the statement:
int i = 10*(rand()/(double)RAND_MAX)-5;
i will be in the range:
a) -10 ≤ i ≤ 10
B) 10 ≤ i ≤ -5
c) -5 ≤ i ≤ 10
d) -5 ≤ i ≤ 5
e) None of the above

What does rand() return? The function is defined: what does the definition say? What is the definition of RAND_MAX? What is the order of evaluation of these terms?

If you answer those questions, you should be able to answer this question without guessing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1