Docsity

Programming language notes - Lab for Lecture | CS 112, Exams of Computer Science

Toggle sidebar
bg1
CS112%MidTem%Exam%Solutions%
!
Part!1:!
!!1)!A!!!2)!B!!!3)!B!!!4)!C!!!5)!B!!!6)!D!!!7)!B!!!8)!A!!!9)!C!10)!D!
11)!C!12)!B!13)!C!14)!D!15)!X!16)!C!17)!C!18)!B!19)!A!20)!B!!!
21)!A!22)!C! 23)!C! 24)!D!25)!B!26)!C!!27)!C!28)!B!29)!C!30)!A!
31)!C!32)!C!33)!B!34)!B!35)!A! 36)!C!37)!D! 38)!B!39)!B!40)!A!
41)!D!42)!B!43)!D!44)!B!45)!D!46)!B!47)!A!48)!C!49)!A!50)!D!
Note:!Question!15!had!no!correct!answer,!so!everyone!received!credit!for!this!question.!
Part!2!(Sections!001!&!002):!
1)!
Assumptions:!
I Main!menu!option!choices!by!user!will!be!limited!to!the!integers!1,!2!or!3.!!
I Nested!(sub)!menu!option!choices!by!user!will!be!limited!to!the!integers!1!or!2.!
I Professor’s!names!are!known!(i.e.,!not!entered!via!standard!input)!
I Error!handling!for!data!types!other!than!integers!is!not!required.!
!!
Data:!
I main!menu!text!(string)!
I professor!names!(list!I!immutable)!
I messages!(list!of!lists!of!strings!–!mutable)! !
!
Operation:!
!
I initialize:!
o main!menu!text!(Options:!Type!New!Message,!View!Messages,!Exit)!!
o main!loop!sentinel!value!
o immutable!list!of!professor!names!
o mutable!list!of!message!strings!belonging!to!each!professor!
!
I do!while!user!chooses!not!to!terminate!application!(Exit!option)!
o display!menu!&!get!option!from!user!
o if!option!==!“Type!New!Message”!
§ display!professor’s!names!and!get!user!selection!
§ get!message!
§ add!message!to!professor’s!list!of!messages!
o if!option!==!“Display!Messages”!
§ display!professor’s!names!and!get!user!selection!
§ display!messages!for!selected!professor!
o if!option!==!“Exit”!
§ display!exit!message!
o else!
§ display!error!message!
!
pf3
pf4

Partial preview of the text

Download Programming language notes - Lab for Lecture | CS 112 and more Exams Computer Science in PDF only on Docsity!

CS112 MidTem Exam Solutions Part 1:

  1. A 2) B 3) B 4) C 5) B 6) D 7) B 8) A 9) C 10) D
  2. C 12) B 13) C 14) D 15) X 16) C 17) C 18) B 19) A 20) B
  3. A 22) C 23) C 24) D 25) B 26) C 27) C 28) B 29) C 30) A 3 1) C 32) C 33) B 34) B 35) A 36) C 37) D 38) B 39) B 40 ) A
  4. D 42) B 43) D 44) B 45) D 46) B 47) A 48) C 49) A 50) D Note : Question 15 had no correct answer, so everyone received credit for this question. Part 2 (Sections 001 & 002):

Assumptions: -­‐ Main menu option choices by user will be limited to the integers 1, 2 or 3. -­‐ Nested (sub) menu option choices by user will be limited to the integers 1 or 2. -­‐ Professor’s names are known (i.e., not entered via standard input) -­‐ Error handling for data types other than integers is not required. Data: -­‐ main menu text (string) -­‐ professor names (list -­‐ immutable) -­‐ messages (list of lists of strings – mutable) Operation: -­‐ initialize: o main menu text (Options: Type New Message, View Messages, Exit) o main loop sentinel value o immutable list of professor names o mutable list of message strings belonging to each professor -­‐ do while user chooses not to terminate application (Exit option) o display menu & get option from user o if option == “Type New Message” § display professor’s names and get user selection § get message § add message to professor’s list of messages o if option == “Display Messages” § display professor’s names and get user selection § display messages for selected professor o if option == “Exit” § display exit message o else § display error message

def main(): menu = "\nMessage Machine\n-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐\n" +
" 1) Type New Message\n" +
" 2) View Messages\n" +
" 3) Exit\n" option = 0 professor = ("Prof. Hofbrau","Prof. Ali") message = [[],[]] while option != 3: print menu option = input("Enter Option => ") if option == 1: print "Leave a message for:\n\n" +
" 1) " + professor[0] + "\n" +
" 2) " + professor[1] + "\n" option = int(raw_input("==> ")) text = raw_input("\nEnter Message => ") message[option-­‐1].append(text) elif option == 2: print "\nView messages for:\n\n" +
" 1) Prof. Hofbrau\n" +
" 2) Prof. Ali\n" option = int(raw_input("==> ")) print "\nMessages for: " +
professor[option-­‐1] + "\n" for each_message in message[option-­‐1]: print each_message elif option == 3: print "\nGoodbye!\n" else: print "\nInvalid Option!\n" main()

def main(): menu = "\nVoting Machine\n-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐\n" +
" 1) Vote\n" +
" 2) View Current Results\n" +
" 3) Exit\n" option = 0 candidate = ("George Patriot","Cassandra Mason") vote_tally = [0,0] while option != 3: print menu option = input("Enter Option => ") if option == 1: print "Vote for:\n\n" +
" 1) " + candidate[0] + "\n" +
" 2) " + candidate[1] + "\n" vote = input("Enter Vote => ") vote_tally[vote-­‐1] = vote_tally[vote-­‐1] + 1 elif option == 2: print "\nView Current Results:\n" for i in range(len(vote_tally)): print candidate[i] +
" => " + str(vote_tally[i]) elif option == 3: print "\nGoodbye!\n" else: print "\nInvalid Option!\n" main() (NOTE) – There numerous ways to write a pseudocode algorithm that provides a solution to a particular programming problem. There are also many ways to implement a program specification. The samples provided represent one perspective.