f11-exam2

11
ENGR 101: Intro to Computers & Programming Fall 2011 Page 1 of 11 Exam 2 Fall 2011 Semester Engr101: Introduction to Algorithms and Programming Section 100 / 200 / 300 You are allowed to use the books and your notes. You may not use electronic devices: including, but not limited to, calculators, laptops, and cell phones. You may not access the internet in any way. You may not use any device that can interpret, compile, or run program code in any way. Write your uniqname on the top of each page of the exam (excluding the first page). You are responsible for the legibility of your work. Any work that is not clear to the grader due to sloppiness or poor handwriting will be marked incorrect. You must read and sign the Honor Code below after you have completed the exam. Printed Uniqname: ___________________________ Printed Name: ____________________________ Circle your Lab Section below (you will lose 1 point if you don’t do this or it is incorrect): 101: Cat – Th, 8:3010:30a 201: Lisa – Tu, 8:3010:30a 301: Oz – Mon, 8:3010:30a 102: Rob – Th, 10:30a12:30p 202: Nader – Tu, 10:30a12:30p 302: Oz – Mon, 10:30a12:30p 103: Cat – Th, 12:302:30p 203: Apoorva – Tu, 12:302:30p 303: Jeff – Mon, 12:302:30p 104: Cassie – Th, 2:304:30p 204: Apoorva – Tu, 2:30p4:30p 304: Sumit – Mon, 4:30p6:30p 105: Rob – Th, 4:306:30p 205: Nader – Tu, 4:30p6:30p 305: Oz – Wed, 8:3010:30a 106: Cassie – Fri, 8:3010:30a 206: Nader – Fri, 8:3010:30a 306: Jeff – Wed, 10:30a12:30p 107: Cassie – Fri, 10:30a12:30p 207: Apoorva – Fri, 10:30a12:30p 307: Jeff – Wed, 12:302:30p 108: Cat – Fri, 12:302:30p 208: Lisa – Fri, 12:302:30p 308: Sumit – Wed, 2:304:30p 109: Rob – Fri, 2:304:30p 209: Lisa – Fri, 2:304:30p 309: Sumit – Wed, 4:30p6:30p Number I II III IV V Total Grade I have neither given nor received aid on this examination, nor have I concealed a violation of the Honor Code. Signature:_________________________

Upload: krishna-mahajan

Post on 18-Jul-2016

212 views

Category:

Documents


0 download

DESCRIPTION

cse exam

TRANSCRIPT

ENGR  101:  Intro  to  Computers  &  Programming  -­‐  Fall  2011                    Page  1  of  11  

   

    Exam  2  Fall  2011  Semester  

Engr101:    Introduction  to  Algorithms  and  Programming  

Section  100  /  200  /  300  

• You  are  allowed  to  use  the  books  and  your  notes.  • You  may  not  use  electronic  devices:  including,  but  not  limited  to,  calculators,  laptops,  and  cell  phones.  • You  may  not  access  the  internet  in  any  way.  • You  may  not  use  any  device  that  can  interpret,  compile,  or  run  program  code  in  any  way.  

   • Write  your  uniqname  on  the  top  of  each  page  of  the  exam  (excluding  the  first  page).  • You  are  responsible  for  the  legibility  of  your  work.    Any  work  that  is  not  clear  to  the  grader  due  to  sloppiness  

or  poor  handwriting  will  be  marked  incorrect.  • You  must  read  and  sign  the  Honor  Code  below  after  you  have  completed  the  exam.      Printed  Uniqname:    ___________________________    Printed  Name:    ____________________________  

 Circle  your  Lab  Section  below  (you  will  lose  1  point  if  you  don’t  do  this  or  it  is  incorrect):    101:    Cat  –  Th,  8:30-­‐10:30a     201:    Lisa  –  Tu,  8:30-­‐10:30a     301:    Oz  –  Mon,  8:30-­‐10:30a  102:    Rob  –  Th,  10:30a-­‐12:30p     202:    Nader  –  Tu,  10:30a-­‐12:30p     302:    Oz  –  Mon,  10:30a-­‐12:30p  103:    Cat  –  Th,  12:30-­‐2:30p     203:    Apoorva  –  Tu,  12:30-­‐2:30p     303:    Jeff  –  Mon,  12:30-­‐2:30p  104:    Cassie  –  Th,  2:30-­‐4:30p     204:    Apoorva  –  Tu,  2:30p-­‐4:30p     304:    Sumit  –  Mon,  4:30p-­‐6:30p  105:    Rob  –  Th,  4:30-­‐6:30p     205:    Nader  –  Tu,  4:30p-­‐6:30p     305:    Oz  –  Wed,  8:30-­‐10:30a  106:    Cassie  –  Fri,  8:30-­‐10:30a     206:    Nader  –  Fri,  8:30-­‐10:30a     306:    Jeff  –  Wed,  10:30a-­‐12:30p  107:    Cassie  –  Fri,  10:30a-­‐12:30p   207:    Apoorva  –  Fri,  10:30a-­‐12:30p   307:    Jeff  –  Wed,  12:30-­‐2:30p  108:    Cat  –  Fri,  12:30-­‐2:30p     208:    Lisa  –  Fri,  12:30-­‐2:30p     308:    Sumit  –  Wed,  2:30-­‐4:30p  109:    Rob  –  Fri,  2:30-­‐4:30p     209:    Lisa  –  Fri,  2:30-­‐4:30p     309:    Sumit  –  Wed,  4:30p-­‐6:30p    

Number   I   II   III   IV   V   Total  

Grade              

   

I have neither given nor received aid on this examination, nor have I concealed a violation of the Honor Code.

Signature:_________________________

Uniqname:    ____________________________    

ENGR  101:  Intro  to  Computers  &  Programming  -­‐  Fall  2011                  Page  2  of  11      

 

The  remainder  of  this  page  is  intentionally  left  blank.    

Uniqname:    ____________________________    

ENGR  101:  Intro  to  Computers  &  Programming  -­‐  Fall  2011                  Page  3  of  11      

 

Question  1  -­‐  Matching  (20  points)  

Match  each  concept  on  the  left-­‐hand  side  with  the  phrase  on  the  right  that  fits  best.  Note  that  there  are  more  items  in  the  right  column  than  in  the  left,  so  some  answers  will  not  be  used.       _____   alias         A.  statement  executed  on  each  iteration       _____   loop  body       B.    extracts  characters  until  next  '\n'  into  string  variable       _____   member  function     C.    keyword  for  defining  generic  routines       _____   getline         D.    encloses  condition  in  if  or  while  statement       _____   exit  statement       E.    vector  of  vectors  of  generic  types       _____   local  scope       F.    method  associated  with  instances  of  a  class       _____   template       G.    alternate  name  declared  using  reference  operator       _____   floating  point       H.    terminates  program       _____   ()         I.      terminates  function  or  procedure       _____   ;         J.    marks  end  of  simple  statement    

K.    represent  binary  number  in  terms  of  mantissa,  exponent,  and  sign  

 L.    textual  context  for  identifier  introduced  within  

procedure  or  function  

Uniqname:    ____________________________    

ENGR  101:  Intro  to  Computers  &  Programming  -­‐  Fall  2011                  Page  4  of  11      

 

Question  2  -­‐  Multiple  Choice  and  Short  Answer  (20  points)  

For  parts  a)  through  e)  below,  check  the  single  most  appropriate  answer  for  each.      a)    Consider  the  predicate  declaration:    bool myPred(const vector<int> & theVector);  

What  is  true  about  the  input  argument  to  this  routine?  (3  pts)  

____   theVector  cannot  be  modified  within  myPred  ____   the  variable  passed  as  theVector  cannot  be  modified  within  the  caller  ____   theVector  has  global  scope  ____   All  of  the  above  ____   None  of  the  above        b)    Which  of  the  following  sorting  algorithms  prominently  exploited  recursion  in  lecture?  (3  pts)  

____   BubbleSort  ____   InsertionSort  ____   MergeSort  ____   All  of  the  above  ____   None  of  the  above        c)    Suppose  that  G  is  the  greatest  (most  positive)  value  expressible  in  an  integer  representation,  and  L  is  the  least  (most  negative)  value.  What  is  the  result  of  evaluating  the  expression  L-5  ?    (3  pts)  

____   G  +  5  ____   G  –  5  ____       G  –  4  ____       L  +  4  ____       None  of  the  above      d)    Suppose  we  have  declared  s  as  an  empty  string.  Which  of  the  following  prints  GoBlue!  on  a  single  line  of  output?    (3  pts)    ____   s = "GoBlue!"; s[3]='l'; cout << s << endl;  ____   s = "GoBlue!"; s.push_back('!'); cout << s << endl; ____     s = "GoBlue!"; cout << s[1] << "oBlue!" << endl;  ____     s = "Blue!"; cout << s.append("Go") << endl; ____     None  of  the  above                  

Uniqname:    ____________________________    

ENGR  101:  Intro  to  Computers  &  Programming  -­‐  Fall  2011                  Page  5  of  11      

 

e)    Consider  the  following  iteration  code,  assuming  i  has  been  declared  as  an  int:  i=0; while (i+1 < 100) {

i++; cout << i;

}  Which  of  the  following  for  statements  is  equivalent?  (3  pts)  

____   for (i=0; i<99; i++) cout << i+1;  ____   for (i=1; i<100; i++) cout << i;  ____   for (i=1; i+1<=100; i++) cout << i;  ____   All  of  the  above  ____   None  of  the  above      

f)  Rank the following 8-bit two's complement numbers from 1 to 5, where the most negative number is labeled with a 1 and the most positive number is labeled with a 5.  (5  pts)  

____   00000000  ____   01010101  ____       10101010  ____       11111111  ____       01100000      

Uniqname:    ____________________________    

ENGR  101:  Intro  to  Computers  &  Programming  -­‐  Fall  2011                  Page  6  of  11      

 

Answer:  

Answer:  

Question  3  –  What  does  the  code  do?  (20  points)    In  the  space  provided  beside  each  of  the  following  four  programs,  write  one  of  the  following:  

• The  screen  output  of  the  program,  using  underscore  to  denote  any  spaces  (e.g.  6_24_42)  • “Won’t  finish”  if  the  program  would  enter  an  infinite  loop  or  potentially  crash  or  otherwise  fail  to  

terminate  normally.    Assume  that  each  code  has  all  necessary  libraries  included,  and  using namespace std;  at  the  beginning.  Do  not  put  any  “scratch  work”  in  the  answer  box.                    int main() { ofstream a("zzzzz"); a << 1.2 << " 3.4 " << endl; a.close(); ifstream b("zzzzz"); while(not b.fail()) { double a; b >> a; cout << a << endl; } return 0; }                         int main() { string s = "November_2"; string s1 = "dissect?"; int j=5, k=6; for (int i=2; i<s.size(); i=i+4) { s1.at(j) = s.at(i); s1.at(k) = s.at(i+1); j--;

k++; } cout << s1 << endl; return 0; }

Uniqname:    ____________________________    

ENGR  101:  Intro  to  Computers  &  Programming  -­‐  Fall  2011                  Page  7  of  11      

 

Answer:  

Answer:  

                int main() { vector<int> v(6,1); for (int i=0; i<v.size(); i++) { v.at(i) = v.at(i) + v.at(i-1); cout << v.at(i) << " "; } cout << endl; return 0; }                 void s2(string s) { if (s.size()>2) { s2(s.substr(1,s.size()-2)); cout << s[0] << s[s.size()-1]; } else {

cout << s; } return; } int main() { string s = "November_2"; s2(s); cout << endl; return 0; }                

Uniqname:    ____________________________    

ENGR  101:  Intro  to  Computers  &  Programming  -­‐  Fall  2011                  Page  8  of  11      

 

Question  4  –  Write  some  code  (20  points)    For  this  question,  you  must  write  the  complete  definition  of  a  procedure,  called  flipflops,  on  the  following  page  that  takes  a  single  string  composed  entirely  of  lowercase  letters  of  any  length,  determines  if  the  string  flipflops,  and  finds  the  lowest  and  highest  letters  in  the  string  if  it  does.    A  string  of  lowercase  letters  is  said  to  flipflop  if  each  character  in  the  string  is  alternately  higher  in  the  alphabet  or  lower  in  the  alphabet  than  the  character  before  it.    The  first  character  in  the  string  must  always  be  lower  than  the  second,  the  second  must  be  higher  than  the  third,  the  third  must  be  lower  than  the  fourth,  etc.    An  empty  string  does  not  flipflop  and  a  string  with  one  character  always  flipflops.    An  example  of  a  string  that  flipflops  would  be  “abate”  since  the  first  ‘a’  is  lower  than  ‘b’,  ‘b’  is  higher  than  the  second  ‘a’,  the  second  ‘a’  is  lower  than  ‘t’,  and  ‘t’  is  higher  than  ‘e’.    Note  that  “abated”  does  NOT  flipflop,  because  ‘e’  is  not  lower  than  ‘d’  and  “chatter”  does  NOT  flipflop,  because  the  first  ‘t’  is  not  higher  than  the  second  ‘t’.    The  procedure’s  declaration  is  as  follows:    void flipflops(const string & str, bool & flip, char & low, char & high);  If  the  procedure  determines  that  the  input  string  flipflops,  it  must  set  the  flip  variable  to  true,  the  low  variable  to  the  lowest  letter  found  in  the  string,  and  the  high  variable  to  the  highest  letter  found  in  the  string.    If  the  procedure  determines  that  the  input  string  does  not  flipflop,  it  must  set  the  flip  variable  to  false  and  the  low  and  high  variables  are  left  undefined.    Notes:  

• You  may  assume  that  “using namespace std;”  and  all  necessary  libraries  are  included  at  the  beginning  of  the  program.  

• This  procedure  can  be  implemented  in  fewer  than  25  lines  of  code.      

Uniqname:    ____________________________    

ENGR  101:  Intro  to  Computers  &  Programming  -­‐  Fall  2011                  Page  9  of  11      

 

   

Uniqname:    ____________________________    

ENGR  101:  Intro  to  Computers  &  Programming  -­‐  Fall  2011                  Page  10  of  11    

 

Question  5  –  Write  some  code  (20  points)    For  this  question,  you  must  write  the  complete  definition  of  a  procedure,  called  findLongest,  on  the  following  page  that  analyzes  the  contents  of  a  vector  of  integers  to  find  the  longest  sequence  of  increasing  values  in  the  vector,  writes  this  sequence  to  another  vector,  and  then  sums  the  contents  of  the  sequence.    The  initial  vector  will  only  consist  of  positive  integers  except  for  the  last  value  in  the  vector  that  will  be  set  equal  to  the  value  -­‐1.      An  example  of  the  longest  sequence  of  increasing  values  in  the  vector  [1 4 5 2 5 7 9 10 5 6 -1]  would  be  [2 5 7 9 10]  and  the  sequence’s  sum  would  be  33.    Additionally,  the  longest  sequence  of  increasing  values  in  the  vector  [5 6 7 1 2 3 5 7 8 4 3 -1]  would  be  [1 2 3 5 7 8]  and  the  sequence’s  sum  would  be  26.    Finally,  note  that  two  adjacent  integers  with  the  same  value  constitute  a  break  in  a  sequence.    Thus,  the  longest  sequence  of  increasing  values  in  the  vector  [1 2 4 4 5 -1]  would  be  [1 2 4]  and  the  sequence’s  sum  would  be  7.    The  procedure’s  declaration  is  as  follows:    void findLongest(const vector <int> & inVec, vector <int> & outVec, int & sum);  Once  the  procedure  finds  the  longest  sequence  of  increasing  values  in  the  vector  inVec,  it  must  first  clear  the  contents  of  outVec  and  then  write  the  contents  of  the  sequence  into  outVec  in  the  order  that  the  sequence  appeared  in  inVec.    After  writing  the  contents  of  the  sequence  into  outVec,  the  procedure  must  write  the  sum  of  the  integers  contained  in  the  sequence  into  the  variable  sum.    Important  Notes:  

• You  may  assume  that  “using namespace std;”  and  all  necessary  libraries  are  included  at  the  beginning  of  the  program.  

• Your  solution  must  use  only  those  vectors  that  are  in  the  procedure’s  parameter  list  and  no  more.    In  other  words,  you  may  not  declare  and/or  use  any  vectors  other  than  inVec  and  outVec  in  your  solution.  

• You  may  assume  that  there  will  be  at  least  one  sequence  of  increasing  values  in  inVec  whose  length  is  greater  than  or  equal  2.    For  example,  [3 2 1 -1]  and  [7 7 7 -1]  would  both  be  considered  invalid  inputs  for  inVec.  

• You  may  assume  that  there  will  be  no  ties  for  a  longest  sequence.    For  example,  [5 6 2 3 -1]  and  [4 5 6 1 2 3 -1]  would  both  be  considered  invalid  inputs  for  inVec.  

• This  procedure  can  be  implemented  in  fewer  than  30  lines  of  code.        

Uniqname:    ____________________________    

ENGR  101:  Intro  to  Computers  &  Programming  -­‐  Fall  2011                  Page  11  of  11