This is CS50. Harvard University Fall Quiz 0 Answer Key

Similar documents
Quiz 0 Review Session. October 13th, 2014

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.

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

CS50 Supersection (for those less comfortable)

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Array Initialization

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Week 3. This is CS50. Harvard University. Fall Cheng Gong

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Question 2. [5 points] Given the following symbolic constant definition

Subject: Fundamental of Computer Programming 2068

ESC101N: Fundamentals of Computing End-sem st semester

else return for return

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

C for C++ Programmers

Functions. Arash Rafiey. September 26, 2017

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Make sure the version number is marked on your scantron sheet. This is Version 1

ECE264 Fall 2013 Exam 3, November 20, 2013

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

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

BİL200 TUTORIAL-EXERCISES Objective:

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

Dynamic memory allocation

return return else return

Arrays and Applications

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Computer System and programming in C

ECE264 Spring 2013 Final Exam, April 30, 2013

MIDTERM EXAM. CS 217 October 28, Name: Precept: Honor Code: Score: Problem Score Max

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Advanced Pointer Topics

Lecture 04 Introduction to pointers

Arithmetic Expressions in C

Language comparison. C has pointers. Java has references. C++ has pointers and references

Programs in memory. The layout of memory is roughly:

CS-211 Fall 2017 Test 1 Version Practice For Test on Oct. 2, Name:

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

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

Computer Programming: Skills & Concepts (CP) Strings

As stated earlier, the declaration

CSC 2400: Computer Systems. Arrays and Strings in C

Kurt Schmidt. October 30, 2018

Motivation was to facilitate development of systems software, especially OS development.

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

The type of all data used in a C (or C++) program must be specified

Procedural programming with C

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

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

CS349/SE382 A1 C Programming Tutorial

BSM540 Basics of C Language

Here's how you declare a function that returns a pointer to a character:

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

cs50.c /**************************************************************************** * CS50 Library 6 *

CSC 2400: Computer Systems. Arrays and Strings in C

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

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Bit Manipulation in C

Topics: Material through example 19 (types, operators, expressions, functions, selection, loops, arrays)

CS240: Programming in C

Fundamentals of Programming

Computer Security. Robust and secure programming in C. Marius Minea. 12 October 2017

CS2141 Software Development using C/C++ C++ Basics

Armide Documentation. Release Kyle Mayes

Data Types. Data Types. Integer Types. Signed Integers

CS201 Some Important Definitions

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

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

Intermediate Programming, Spring 2017*

Motivation was to facilitate development of systems software, especially OS development.

Lecture 3: C Programm

Tutorial 5. Overflow. Data Types. Structures. Call stack. Stack frames. Debugging Tips. CS 136 Winter 2019 Tutorial 5 1

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

CS 33. Introduction to C. Part 5. CS33 Intro to Computer Systems V 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Do not start the test until instructed to do so!

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

This%is%CS50.% Harvard*University*Fall%2013* Quiz*0* out*of*84*points* Print*your*name*on*the*line*below.* % % % % % % % % % % %

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

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

Week 4. This is CS50. Harvard University. Fall Anna Whitney

Principles of C and Memory Management

Functions. (transfer of parameters, returned values, recursion, function pointers).

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Fundamental of Programming (C)

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Topic 6: A Quick Intro To C

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

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Transcription:

Quiz 0 Answer Key Answers other than the below may be possible. Binary Bulbs. 0. Bit- Sized Questions. 1. Because 0 is non- negative, we need to set aside one pattern of bits (000) for it, which leaves us with only seven other patterns, with which we can thus represent 1 (001) through 7 (111). 2. 2 n This is CS80. 3. 1010000 4. 80 0 < 6

Looping back to Scratch. 5. #include <cs50.h> #include <stdio.h> int main(void) for (int i = 0; i < 4; i++) printf("peanut butter jelly!\n"); 6. #include <cs50.h> #include <stdio.h> int main(void) int i = 0; while (true) printf("%i\n", i); i++; Itsa Mario again. 7. #include <stdio.h> void PrintGrid(int width, int height); int main(void) PrintGrid(5, 3); void PrintGrid(int width, int height) for (int i = 0; i < height; i++) for (int j = 0; j < width; j++) printf("#"); printf("\n"); 8. A return value is data that's passed from one function back to another, whereas a side effect is some modification of state by a function that persists beyond the function's call (e.g., displaying text on a screen via printf, as is the case with PrintGrid). 1 < 6

hello, C. 9. In cs50.h are a definition (i.e., typedef) of string and a declaration of (i.e., prototype for) GetString. Only by including cs50.h atop main can we use both. 10. In stdio.h is a declaration of (i.e., prototype for) printf. Only by including stdio.h above main can we use it within main (without compiler warnings). 11. It signifies that this program will not take any command- line arguments. 12. Assuming sufficient memory is available, GetString returns the address of the first byte of memory that's been allocated to store a user's input. Real Problems. 13. Because 1 and 10 are both of type int, the expression 1 / 10 evaluates to an int as well, in which case everything after the decimal is discarded, and so 0.1 becomes 0, which is then formatted by printf as 0.0. 14. A floating- point value (i.e., a float or, in this case, a double) is inherently imprecise, because with only finitely many bits, it can only take on a finite number of real numbers. 0.1000000000000000000000000000 is not among those numbers, and so the nearest approximation is what's outputted by printf, which happens to be 0.1000000000000000055511151231. One Plus One. 15. 2 16. Because '1' and '1' are of type char, this line will add the corresponding ASCII values (which happen to be 49 and 49), and so a value other than 2 (specifically, 98) will be printed. An Odd Question. 17. bool odd(unsigned int n) if (n % 2 == 1) return true; else return false; 2 < 6

Curses, recursive! 18. Factorial of n (i.e., n!). 19. int f(int n) int product = 1; for (int i = 2; i <= n; i++) product *= i; return product; O(MG). 20. Ω O linear search 1 n merge sort n log n n log n bubble sort n n 2 selection sort n 2 n 2 stupid sort n Overflowing with Questions. 21. Integer overflow is said to occur when a program attempts to store a value in a variable (e.g., an int) that's too large to fit in that variable (because its representation would require more bits than are used for that variable's type). For instance, if i were an int with a value of 2 31-1 (i.e., 2147483647), adding 1 (or more) to that value would overflow i. 22. A buffer overflow is said to occur when a program attempts to write data beyond the boundary of some buffer (e.g., an array). For instance, if a were an array of size n, writing to a beyond a[n-1] would overflow a. 3 < 6

Forgetful. 23. int strlen(char* s) int n = 0; while (s[n]!= '\0') n++; return n; Having said that... 24. Having said that, merge sort requires twice as much space (to store values while merging). 25. Having said that, a linked list does not allow for binary search, even if sorted, since it doesn't support direct access to nodes by index. 26. Having said that, binary search requires that its input be sorted, which might not be the case (and sorting it would require additional time). Swapping Stories. 27. 1 " 1 x y a b tmp 2 " 1 2 3 " 1 2 0x10 0x14 4 " 1 2 0x10 0x14 1 5 " 2 2 0x10 0x14 1 6 " 2 1 0x10 0x14 1 7 " 2 1 4 < 6

Role Reversal. 28. Be sure to pass -lcs50 as a command- line argument to clang (as via make). 29. Be sure to have #include <string.h> atop the program. 30. Be sure to pass n + 1 arguments in total to printf, where n is the number of format codes (that begin with %) in the first argument to (i.e., format string for) printf. 31. Be sure to free (as with free) any memory allocated by malloc. 32. Be sure not to touch memory beyond the boundary of any array. 5 < 6