Quiz 0 Answer Key. Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d. True or False.

Similar documents
Computer Science 50: Introduction to Computer Science I Harvard College Fall Quiz 0 Solutions. Answers other than the below may be possible.

This is CS50. Harvard University Fall Quiz 0 Answer Key

Quiz 0 Review Session. October 13th, 2014

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

CSE 374 Programming Concepts & Tools

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Lecture 3: C Programm

MPATE-GE 2618: C Programming for Music Technology. Unit 4.1

Kurt Schmidt. October 30, 2018

C Introduction. Comparison w/ Java, Memory Model, and Pointers

a data type is Types

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Lectures 5-6: Introduction to C

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

CS201- Introduction to Programming Current Quizzes

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

CS 610: Intermediate Programming: C/C++ Making Programs General An Introduction to Linked Lists

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

C: How to Program. Week /Mar/05

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

CS 237 Meeting 19 10/24/12

Dynamic Memory Allocation and Command-line Arguments

CSE 12 Spring 2016 Week One, Lecture Two

Pointers, Dynamic Data, and Reference Types

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

CS 261 Data Structures. Introduction to C Programming

377 Student Guide to C++

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

ECE551 Midterm Version 1

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

[0569] p 0318 garbage

Systems Programming and Computer Architecture ( )

C & Data Structures syllabus

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

CENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

POINTER AND ARRAY SUNU WIBIRAMA

(T) x. Casts. A cast converts the value held in variable x to type T

ECE264 Spring 2013 Final Exam, April 30, 2013

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

Ch. 3: The C in C++ - Continued -

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Lecture 2: C Programm

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Tail recursion. Decision. Assignment. Iteration

ECE551 Midterm Version 2

Lab 3. Pointers Programming Lab (Using C) XU Silei

CS61, Fall 2012 Section 2 Notes

CS 61c: Great Ideas in Computer Architecture

CIS 190: C/C++ Programming. Lecture 3 Memory Management in C

Array Initialization

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Chapter 2 - Introduction to C Programming

Variation of Pointers

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

CS61C : Machine Structures

CS201 Some Important Definitions

BASIC ELEMENTS OF A COMPUTER PROGRAM

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

CS16 Exam #1 7/17/ Minutes 100 Points total

BBM 201 DATA STRUCTURES

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

Procedural programming with C

Review! Lecture 5 C Memory Management !

SOFTWARE Ph.D. Qualifying Exam Fall 2017

CS61C : Machine Structures

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Objectives. In this chapter, you will:

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CS 11 C track: lecture 6

C programming basics T3-1 -

Lecture 07 Debugging Programs with GDB

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Run Time Environment

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm

Transcription:

Quiz 0 Answer Key Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d True or False. 6. T or F 7. T 8. F 9. T 10. T or F Itching for Week 0? 11. 00011001 + 00011001 00110010 12. #include <stdio.h> int main(void) int n = 10; while (n > 0) printf("%d", n); n--; printf("blastoff!"); 0 < 8

Role Reversal. 13. This program always prints zero, no matter its input. Removing the semicolon on the same line as if fixes the problem. 14. This program always prints one, no matter its input. Changing the = to == in the program s condition fixes the problem. Big OMG. 15. Algorithm O Ω sorting an array with Merge Sort n log n n log n sorting an array with Selection Sort n 2 n 2 inserting into a sorted linked list n 1 searching a sorted array with Binary Search log n 1 searching a sorted linked list with Linear Search n 1 Bit Wise. 16. bits char 8 char * 32 double 64 double * 32 int 32 int * 32 unsigned int 32 long long 64 1 < 8

Temp Variables. 17. Because both 9 and 5 are of int, (9 / 5) evaluates to 1, since the remainder is truncated. And so the formula currently implemented is F = 1 C + 32 = C + 32. To fix the problem, it suffices to rewrite 9 as 9.0, rewrite 5 as 5.0, or cast either to a float. This is CS50. 18. Whereas the #include (a pre- processor directive) informs gcc that a program might call functions declared in cs50.h, the -lcs50 (a linker flag) tells gcc to incorporate the bits that result from compiling cs50.c (wherever they are) into the program. Quickies. 19. Pseudocode is an amalgam of English- and code- like syntax used to define algorithms or outline programs in a language- neutral way. 20. Whereas \n moves a cursor down to a new line, \r moves a cursor to the beginning of the current line. In the context of files, Linux uses \n to end lines, Mac OS uses \r, and Windows uses \r\n. 21. Because an int is, by default, signed, it s intended to represent negative and positive numbers alike. While half of its range is allocated toward non- negative numbers (0 through 2 31 1), the rest is reserved for negative numbers. 22. You can trigger a segfault by indexing into an array far beyond its bounds, by dereferencing an invalid (e.g., garbage or NULL) pointer, and even by calling a function recursively too many times (the result of which is that the stack overruns the heap). User Error. 23. The user must have inputted something other than a floating- point number (potentially preceded and followed by whitespace), the result of which is that the input does not match the format string s expectation of a leading %f, and so neither f nor c get filled with a value. 24. The user must have inputted a floating- point number (potentially preceded and followed by whitespace), followed by one or more non- numeric characters, the result of which is that the input matches the whole format string, and so both f and c get filled with values. 2 < 8

Programmer Error. 25. Because the parameter, c, to capitalize is passed by value, the function receives a copy of whatever character is passed in. That character is indeed capitalized within the scope of that function, but as soon as the function returns, the capitalized character is lost (because it s popped off the stack). This problem can be fixed by having capitalize return the capitalized character, as in the below. char capitalize(char c) if (islower(c)) return toupper(c); return c; With capitalize so defined, the caller must now retain this return value, as in the below. c = capitalize(c); Alternatively, capitalize can be declared as expecting a pointer, as in the below. void capitalize(char *c) if (islower(*c)) *c = toupper(*c); With capitalize so defined, the caller must now call the function as follows. capitalize(&c); Bottle(s). 26. Because the s in line 15 inside of a branch, its scope is limited to lines 14 through 16; although s is assigned a value, that value goes unused. Similarly does the s in line 19 only exist between lines 18 and 20; its value, too, goes unused. And so in line 21, which outside of those scopes, s appears to be undeclared. 27. This program is intended to print bottle if the user inputs 1, bottles if the user inputs 0 or an integer greater than 1. 3 < 8

Pointer Fun without Binky. 28. GetString returns the address in memory of a string inputted by a user. And so if GetString is called twice, it returns two different (i.e., unequal) addresses, one for each string, even if both happen to comprise the same characters. 29. statement what would be printed printf("%d", i); 1 printf("%d", j); 3 printf("%d", *p); 3 printf("%d", k); 7 30. int strlen(char *s) int n = 0; if (s == NULL) return n; for (int i = 0; s[i]!= '\0'; i++) n++; return n; 4 < 8

(Proto)typical Alex. 31. proto bool isalnum(char c) char tolower(char c) bool isupper(char c) bool isdigit(char c) function function(parameter) if (isalpha(c) isdigit(c)) return true; return false; function(parameter) if ('A' <= c && c <= 'Z') return c - 'A' + 'a'; return c; function(parameter) if ('A' <= c && c <= 'Z') return true; return false; function(parameter) if ('0' <= c && c <= '9') return true; return false; 5 < 8

Ah, memories. 32. text program itself initialized data global variables declared with values uninitialized data global variables declared without values heap memory that s been dynamically allocated with malloc stack functions parameters and local variables environment variables special variables like the user s username 6 < 8

Argh. 33. statement printf("%s", argv[0]); what would be printed./a.out printf("%c", argv[0][1]); / printf("%c", argv[1][0]); f printf("%s", &argv[1][1]); oo printf("%d", strlen(argv[0])); 7 printf("%d", strlen(&argv[0][2])); 5 printf("%d", argc); 4 From Floor to Ceiling. 34. int floor(float x) return (int) x; 35. int ceil(float x) if (x (int) x > 0.0) return (int) (x + 1.0); return (int) x; PC LOAD LETTER. 36. double steal(double interest) return interest - round(interest * 100) / 100; 7 < 8