ECE 551 PRACTICE Midterm Exam

Similar documents
Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Illustration 1: Diagram of program function and data flow

Dalhousie University CSCI 2132 Software Development Winter 2015 Lab 7, March 11

Stacks. Linear data structures

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Sources: On the Web: Slides will be available on:

7th Marathon of Parallel Programming WSCAD-SSC/SBAC-PAD-2012

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Software Development Tools for Embedded Systems. Hesen Zhang

Chapter 13 Storage classes

Building Embedded Systems

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

System Calls and Standard I/O

C++ Programming Language

Informatica e Sistemi in Tempo Reale

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Specific Simple Network Management Tools

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Object Oriented Software Design II

ECS 165B: Database System Implementa6on Lecture 2

Module 816. File Management in C. M. Campbell 1993 Deakin University

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

C++ INTERVIEW QUESTIONS

/* File: blkcopy.c. size_t n

Leak Check Version 2.1 for Linux TM

IPC. Semaphores were chosen for synchronisation (out of several options).

Embedded Systems Design Course Applying the mbed microcontroller

8.5. <summary> Cppcheck addons Using Cppcheck addons Where to find some Cppcheck addons

Sample CSE8A midterm Multiple Choice (circle one)

Software security. Buffer overflow attacks SQL injections. Lecture 11 EIT060 Computer Security

For a 64-bit system. I - Presentation Of The Shellcode

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Lecture 12 Doubly Linked Lists (with Recursion)

CS 241 Data Organization Coding Standards

Section IV.1: Recursive Algorithms and Recursion Trees

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

How To Understand How A Process Works In Unix (Shell) (Shell Shell) (Program) (Unix) (For A Non-Program) And (Shell).Orgode) (Powerpoint) (Permanent) (Processes

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Introduction to Programming II Winter, 2014 Assignment 2

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

COS 217: Introduction to Programming Systems

WIZnet S2E (Serial-to-Ethernet) Device s Configuration Tool Programming Guide

C Programming Review & Productivity Tools

Lab 5: BitTorrent Client Implementation

arrays C Programming Language - Arrays

Application Note AN0008. Data Logging Extension. For. Venus 8 GPS Receiver

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

10CS35: Data Structures Using C

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

Lab 2: Swat ATM (Machine (Machine))

Networks and Protocols Course: International University Bremen Date: Dr. Jürgen Schönwälder Deadline:

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Fortify on Demand Security Review. Fortify Open Review

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Computer Programming I

Application Note. Introduction AN2471/D 3/2003. PC Master Software Communication Protocol Specification

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

Introduction to Java

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Dynamic Behavior Analysis Using Binary Instrumentation

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Programming languages C

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Splicing Maps and Sets

#pragma omp critical x = x + 1; !$OMP CRITICAL X = X + 1!$OMP END CRITICAL. (Very inefficiant) example using critical instead of reduction:

Basics of I/O Streams and File I/O

Off-by-One exploitation tutorial

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Computer Programming Tutorial

CSE 333 SECTION 6. Networking and sockets

C++FA 5.1 PRACTICE MID-TERM EXAM

Data Structures and Algorithms Written Examination

16 Collection Classes

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

CryptoAPI. Labs. Daniil Leksin

Simple C Programs. Goals for this Lecture. Help you learn about:

Caml Virtual Machine File & data formats Document version: 1.4

6.S096 Lecture 1 Introduction to C

Frama-C s value analysis plug-in

PES Institute of Technology-BSC QUESTION BANK

Keil C51 Cross Compiler

Numerical Algorithms Group

AP Computer Science Java Mr. Clausen Program 9A, 9B

Static Code Analysis Procedures in the Development Cycle

Embedded C Programming

Transcription:

ECE 551 PRACTICE Midterm Exam This is a full length practice midterm exam. If you want to take it at exam pace, give yourself 75 minutes to take the entire test. Just like the real exam, each question has a point value. There are 75 points from 7 question, so pace yourself accordingly. Questions: 1. Multiple Choice: 8 pts 2. Tracing C Code: 12 pts 3. Algorithmic Basics: 12 pts 4. 4: Dynamic Allocation: 10 pts 5. Coding 1: 11 pts 6. Coding 2: 11 pts 7. Recursion: 11 pts 1

Question 1 Multiple Choice [8 pts] For each of the following questions, circle the best answer: 1. Conceptually, what does the unary * operator mean in C? (a) Follow the arrow. (b) Give an arrow pointing at something. (c) Allocate memory for something. (d) Concatentate two strings. (e) None of these 2. Which tool helps you find memory leaks? (a) gdb (b) gcc (c) svn (d) valgrind (e) None of these 3. A recursive function is... (a)...guaranteed to run in at most O(N 3 ) time. (b)...one that calls itself. (c)...one that uses the ncurses library. (d)...incompatible with gdb. (e) None of these 4. When white box testing... (a)...if you have statement coverage, then you are guaranteed to have path coverage too. (b)...you cannot have statement coverage and path coverage at the same time. (c)...if you have decision coverage, then you are guaranteed to need at most one more test case to have path coverage. (d)...you cannot have statement coverage and decision coverage at the same time. (e) None of these 2

Question 2 Tracing C Code [12 pts] What is the output when the following C code is executed? #include <stdio.h> #include <stdlib.h> int g(int * p) { *p = p[1] + 20; p++; int ans = *p; p[0] = 456; return ans; Your Output: int f(int * q, int ** p) { *q = 99; int n = g(*p); printf("**p = %d\n", **p); *p = q; return n; int main (void) { int a = 4; int b[] = {6, 9, 88; int * p = &b[1]; int c = f(&a, &p); printf("c = %d\n", c); if (p == &a) { printf("*p aliases a, both = %d\n", *p); else { printf("*p is %d\n", *p); printf("a is %d\n", a); for (int i = 0; i < 3; i++) { printf("b[%d] = %d\n", i, b[i]); return EXIT_SUCCESS; 3

Question 3 Algorithmic Basics [12 pts] The diagrams shown below are the result of executing an algorithm (parameterized by N) which draws black squares, grey squares, and grey Xes on a 10x10 grid of white boxes. X X N=0 N=1 N=2 X N=3 N=4 N=5 Determine the algorithm used to create these diagrams, and write the algorithm below in clear-step-by-step English for any N. Note that the next page has blank grids and space for scratch work. 4

This page is for scratch work. It will not be graded. X X X This page is for scratch work. It will not be graded. 5

Question 4 4: Dynamic Allocation [10 pts] Consider the following C code (which has errors in it): 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 struct _my_struct { 5 int nnums; 6 int * nums; 7 ; 8 typedef struct _my_struct my_struct; 9 10 void f(my_struct * ptr) { 11 for (int i = 0; i <= ptr->nnums; i++) { 12 printf("%d\n", ptr->nums[i]); 13 14 free(ptr); 15 16 int main(void) { 17 my_struct * s = malloc(sizeof(s)); 18 s->nnums = 5; 19 s->nums = malloc(5 * sizeof(*s->nums)); 20 for (int i = 0; i < s->nnums; i++) { 21 s->nums[i] = i + 4; 22 23 f(s); 24 free(s); 25 return EXIT_SUCCESS; 26 valgrind reports the following information about this program (which you may find useful in helping you find the problems): 6

Invalid write of size 8 at 0x400612: main (q4.c:19) Address 0x51f1048 is 0 bytes after a block of size 8 alloc d at 0x4C2B6CD: malloc (in...) by 0x4005F2: main (q4.c:17) Invalid read of size 8 at 0x400623: main (q4.c:21) Address 0x51f1048 is 0 bytes after a block of size 8 alloc d at 0x4C2B6CD: malloc (in...) by 0x4005F2: main (q4.c:17) Invalid read of size 8 at 0x40059D: f (q4.c:12) by 0x400656: main (q4.c:23) Address 0x51f1048 is 0 bytes after a block of size 8 alloc d at 0x4C2B6CD: malloc (in...) by 0x4005F2: main (q4.c:17) Invalid read of size 4 at 0x4005AE: f (q4.c:12) by 0x400656: main (q4.c:23) Address 0x51f10a4 is 0 bytes after a block of size 20 alloc d at 0x4C2B6CD: malloc (in...) by 0x40060A: main (q4.c:19) Invalid free() / delete / delete[] / realloc() at 0x4C2A82E: free (in...) by 0x400662: main (q4.c:24) Address 0x51f1040 is 0 bytes inside a block of size 8 free d at 0x4C2A82E: free (in...) by 0x4005DE: f (q4.c:14) by 0x400656: main (q4.c:23) 20 bytes in 1 blocks are definitely lost in loss record 1 of 1 at 0x4C2B6CD: malloc (in...) by 0x40060A: main (q4.c:19) 7

Identify and fix the four errors in the code. For each error (1) state the line number that the error is on, (2) state the problem with this line (reason it is an error), and (3) state the correction that fixes this error: 1. Error 1 (a) Line Number: (b) Problem: (c) Fix: 2. Error 2 (a) Line Number: (b) Problem: (c) Fix: 3. Error 3 (a) Line Number: (b) Problem: (c) Fix: 4. Error 4 (a) Line Number: (b) Problem: (c) Fix: 8

Question 5 Coding 1 [11 pts] Write the function copyarray which takes an array of pointers to info structs (array), and the number of items in that array (n) and makes a DEEP copy of the array. This function should return the copied array. Hint: you may wish to use strdup on this question. struct _info { int nstrs; //number of strings in strs char ** strs; //array of strings ; typedef struct _info info; info ** copyarray (info ** array, int n) { Then write a freearray function which frees all memory associated with an array of info struct pointers. Note that freearray(p,n) should free all memory allocated by copyarray if p is the return value of copyarray and n is the same value as was passed to copyarray. void freearray(info ** p, int n) { 9

Question 6 Coding 2 [11 pts] Write a program which takes one command line argument (a file name), reads all of the lines of text in the file named by the command line argument, and prints out the longest line in the file (and ONLY the longest line). That is, your program should have one single line of output, which is the longest line in the file. If the input file is empty (does not contain any lines) then your program should print no output at all. You may find getline and strlen useful on this problem (though you may use any standard library functions you wish). For this problem, we will relax the no assumptions restriction by allowing you to assume that (1) a command line argument is provided (2) the file requested exists and is readable (so fopen succeeds) (3) malloc and realloc always succeed (4) fclose succeeds. Answer on the next page 10

#include <stdio.h> #include <stdlib.h> #include <string.h> 11

Question 7 Recursion [11 pts] Write the recursive function strxpos which makes a copy of a string, transposing (flipping) every pair of characters. If the string has odd length, then the last character is not transposed with the \0 character (the output string is always the same length as the input string). For example, given an input (src) of "abcde" the string produced (written into dest) would be "badce". As another example, if the input were 1234, then the function would produce the string 2143. You may assume that dest points to a writeable area with space for each character in src when you function is first called. You may not use any library functions (strlen, strdup, malloc, etc). You MUST use only recursion for this problem. You may not use iteration (no for, while, or do-while loops). void strxpos ( char * dest, const char * src ) { 12