CSE 374 Midterm Exam Sample Solution 2/6/12

Similar documents
CSE 303 Midterm Exam

CSE 374 Midterm Exam Sample Solution 2/11/13. Question 1. (8 points) What does each of the following bash commands do?

CSE 374 Midterm Exam 11/2/15. Name Id #

CSE 374 Midterm Exam 2/6/17 Sample Solution. Question 1. (12 points, 4 each) Suppose we have the following files and directories:

CSE 374 Midterm Exam 11/2/15 Sample Solution. Question 1. (10 points) Suppose the following files and subdirectories exist in a directory:

CSE 303, Winter 2007, Midterm Examination 9 February Please do not turn the page until everyone is ready.

CSE 303, Spring 2005, Midterm Examination 29 April Please do not turn the page until everyone is ready.

CSE 303: Concepts and Tools for Software Development

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Please wait to turn the page until everyone is told to begin.

York University Faculty Science and Engineering Fall 2008

File IO and command line input CSE 2451

CSE374 Winter 2016, Midterm Examination February 8, 2016 Please do not turn the page until the bell rings.

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

CSE 333 Midterm Exam 5/10/13

CSE 333 Midterm Exam July 24, Name UW ID#

Wildcards and Regular Expressions

The Unix Shell & Shell Scripts

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

: the User (owner) for this file (your cruzid, when you do it) Position: directory flag. read Group.

Introduction to Linux Spring 2014, Section 02, Lecture 3 Jason Tang

COL100 Lab 2. I semester Week 2, Open the web-browser and visit the page and visit the COL100 course page.

System Programming. Introduction to Unix

Creating a Shell or Command Interperter Program CSCI411 Lab

Exercise 1: Basic Tools

CSE 333 Midterm Exam 7/29/13

Principles of Bioinformatics. BIO540/STA569/CSI660 Fall 2010

Shells and Shell Programming

Homework 2 Answers. Due Date: Monday, April 29, 2002, at 11:59PM Points: 100

Final Exam 1 /12 2 /12 3 /10 4 /7 5 /4 6 /10 7 /8 8 /9 9 /8 10 /11 11 /8 12 /10 13 /9 14 /13 15 /10 16 /10 17 /12. Faculty of Computer Science

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

CS 223: Data Structures and Programming Techniques. Exam 2

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

CSCI 2132 Final Exam Solutions

CSE 303 Winter 2008 Midterm Key

ECE264 Fall 2013 Exam 1, September 24, 2013

CSE 333 Midterm Exam Sample Solution 5/10/13

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling

CSE 15L Winter Midterm :) Review

UNIX files searching, and other interrogation techniques

Final Exam. Fall Semester 2016 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

CSC209H1S Day Midterm Solutions Winter 2010

Common File System Commands

Introduction to Linux

Practical Session 0 Introduction to Linux

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

CS16 Midterm Exam 2 E02, 09F, Phill Conrad, UC Santa Barbara Wednesday, 11/18/2009

Lecture 7: file I/O, more Unix

Procedures, Parameters, Values and Variables. Steven R. Bagley

Part 1: Basic Commands/U3li3es

Lecture 8: Structs & File I/O

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

CSC209. Software Tools and Systems Programming.

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Shells and Shell Programming

Consider the following program.

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

CSC209S Midterm (L0101) Spring 1999 University of Toronto Department of Computer Science

Basic Linux (Bash) Commands

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

Operating Systems, Final exam May 2016 Bachelor's Degree in Computer Science and Engineering

Systems Programming/ C and UNIX

CENG 334 Computer Networks. Laboratory I Linux Tutorial

CSE 333 Midterm Exam 2/12/16. Name UW ID#

Introduction to Bash Programming. Dr. Xiaolan Zhang Spring 2013 Dept. of Computer & Information Sciences Fordham University

Answers to AWK problems. Shell-Programming. Future: Using loops to automate tasks. Download and Install: Python (Windows only.) R

CSE413 Midterm. Question Max Points Total 100

CS160A EXERCISES-FILTERS2 Boyd

CSE 333 SECTION 3. POSIX I/O Functions

On successful completion of the course, the students will be able to attain CO: Experiment linked. 2 to 4. 5 to 8. 9 to 12.

CSE 374: Programming Concepts and Tools. Eric Mullen Spring 2017 Lecture 4: More Shell Scripts

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

CS 455 Midterm Exam 1 Fall 2017 [Bono] Thursday, Sep. 28, 2017

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2018

ITST Searching, Extracting & Archiving Data

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 2

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

CSC209. Software Tools and Systems Programming.

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

CSCI 2132 Software Development. Lecture 7: Wildcards and Regular Expressions

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

CS270 Homework 5 (HW5)

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

Introduction to Supercomputing

Pointers and scanf() Steven R. Bagley

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

LAB 8 (Aug 4/5) Unix Utilities

Lecture 04 Introduction to pointers

C And Unix Programming: A Comprehensive Guide By N. S. Kutti

My malloc: mylloc and mhysa. Johan Montelius HT2016

A shell can be used in one of two ways:

Lecture 03 Bits, Bytes and Data Types

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Shell Programming Overview

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CSCI 4061: Pipes and FIFOs

Transcription:

Question 1. (12 points) Suppose we have the following subdirectory structure inside the current directory: docs docs/friends docs/friends/birthdays.txt docs/friends/messages.txt docs/cse374 docs/cse374/notes.txt docs/cse374/hw9 docs/cse374/hw9/package.h docs/cse374/hw9/package.c docs/cse374/hw9/main.c pictures pictures/big.jpg pictures/thumb.jpg (a) What are the final contents of the file answer after the user executes the following commands in the current directory: cd docs ls friends > temp ls cse374 >> temp sort temp >> answer birthdays.txt hw9 messages.txt notes.txt (b) Assuming we are back in the original directory, and without changing the current directory, give a single command line that will list the number of lines and characters in each of the C source files (files with names ending in.c ) in the hw9 directory. The output can contain additional detail or summary information if it is inconvenient to get rid of it. wc docs/cse374/hw9/*.c (Strictly speaking, we should use the -l and c options to count just lines and characters, but that is not something you would be expected to memorize.) (c) Assuming we are back in the original directory, and without changing the current directory, give a single command line that will make a complete copy of the docs/cse374 subtree (including all directories and files), and store the copy in a new directory docs/cse374-bak. cp -r docs/cse374 docs/cse374-bak Page 1 of 8

Question 2. (18 points) Write a shell script rmdups that deletes all files that are an exact byte-for-byte copy of a given file. The command rmdups f1 f2... fn should remove all of the files f2 through fn that are exact copies of f1. Use cmp to detect whether two files are identical. The command cmp -s x y will exit with a return code of 0 if the two files x and y are identical. The return code will be 1 if they are different. The -s option instructs cmp to perform the comparison silently and not produce any output on stdout or stderr. Your script should verify that there is at least one argument (f1) and that each of the files in the argument list exist and are regular files (if [[ -f file ]] ). If there are no arguments or if f1 is not an existing regular file, the script should print an appropriate message to stderr and exit. If any of the other arguments are not existing regular files, the script should print an appropriate error message to stderr and ignore that argument, continuing on to process the remaining files. Assume that whoever is running the script has sufficient permissions to remove any necessary files. Also assume that all files are readable if they exist. You do not need to worry about permissions issues. Additional space is provided on the next page if you need more room for your answer. Page 2 of 8

Question 2. (cont.) Additional space for your answer if needed. #!/bin/bash if [[ $# < 1 ]] then echo $0: at least one argument required >&2 exit 1 fi if [[! -f $1 ]] then echo $0: $1 not ordinary file >&2 exit 1 fi fn=$1 shift while [[ $# > 0 ]] do if [[ -f $1 ]] then cmp -s $fn $1 if [[ $? = 0 ]] then rm $1 fi else echo $0: $1 not ordinary file >&2 fi shift done The exact wording of error messages can be anything appropriate, but they did need to be written to stderr (stream 2). Page 3 of 8

Question 3. (14 points) Give basic regular expressions that could be used with grep (not egrep) to locate words with the following properties in a file containing a list of words, each line in the file containing a single word (like the words file demonstrated in class). (a) All words that would be legal identifiers in Java or C programs: that is, they start with an upper-case or lower-case letter and contain zero or more additional letters, digits, and underscore ( _ ) characters. Examples: x, xyzzy, a_long_name, Another_1, sillyquestion, but not 1more_thing (starts with a digit), etc. ^[a-za-z][a-za-z0-9_]*$ (b) All words that are 8 characters long and consist of the same 4 characters repeated twice. Examples: couscous, beriberi. ^\(...\)\1$ (c) All words that do not contain any lower-case English vowels. For this question, the vowels are the characters a, e, i, o, and u. ^[^aeiou]*$ An answer that used + instead of * would also be fine. On all of these problems, characters ^ and $ were needed surrounding each basic pattern to match only complete words. If those were missed on multiple parts of the question, points were only deducted once. Page 4 of 8

Question 4. (18 points) Several months before each quarter starts, faculty members are asked to send in a list of books for their upcoming courses. The bookstore only needs the 10-character ISBN codes (the codes in the barcode label on the back of each book), but usually faculty members send messages with extra information. For example: This year please get the Kernighan & Ritchie C book, isbn is 0-13-110362-8, 2nd ed Also include the Linux Pocket Guide 0-596-00628-0 by Barrett. For my other course please order Pragmatic Programmer 0-201-61622-X For this problem, write a shell script whose argument is the name of a file containing lines like the ones above. The output should be a list of ISBN codes extracted from the text. For example, given the above input, the output should be: 0131103628 0596006280 020161622X ISBN format: All ISBN codes in the input file contain 10 characters (mostly digits) plus 3 hyphens. There are 4 parts to the code separated by the 3 hyphens: region-publisher-title-check The region is always 0, and the check character is a single digit 0-9 or the upper-case letter X. The publisher and title codes are all digits 0-9 and there are 8 digits total (you do not need to check for the exact number of digits in these parts). The number of digits in these two parts are different for different publishers. Large publishers have short publisher codes with fewer digits and more digits in the title code; small publishers have more digits in the publisher code and fewer digits for the title code. Both the publisher and title codes have at least one digit each. You may assume that each line in the input file contains exactly one ISBN code somewhere in the line, and that nothing else in the line is formatted exactly like an ISBN code. You should assume that the script has one argument and it is the name of a file that exists and can be read (i.e., you don t need to check for a valid argument list in this problem). Hint: sed #!/bin/bash sed 's/.*0-\([0-9][0-9]*\)-\([0-9][0-9]*\)-\([0-9x]\).*/0\1\2\3/' $1 Note: Basic sed regular expressions do not include the + operator, but we allowed [0-9]+ to match the book and publisher fields. Just [0-9]* would not be sufficient because it would match strings without digits in those positions. Page 5 of 8

Question 5. (18 points) Consider the following C program. #include <stdio.h> void mystery(int *x, int *y, int *z) { *x = *y + *z; int main(int argc, char** argv) { int x, y; int *p1; int *p2; int *p3; p1 = &x; *p1 = 1; p3 = p1; p2 = &y; *p2 = 3; printf("%d %d %d \n", *p1, *p2, *p3); *p3 = (*p2) + 1; printf("%d %d %d \n", *p1, *p2, *p3); mystery(p1,p2,p3); printf("%d %d %d \n", *p1, *p2, *p3); return 0; What output does this program produce when it is executed? (It does execute successfully.) It would be useful to draw diagrams showing variables and pointers to help answer the question and to help us award partial credit if needed. 1 3 1 4 3 4 7 3 7 Page 6 of 8

Question 6. (20 points) (The small C programming exercise.) Write a on the next page a program that will read a file whose name is given as an argument on the command line and will print the following on stdout: The name of the file, The total number of lines in the file, and The contents of the last line in the file. The output should be formatted as shown in the following examples. Examples: If we run the program with the file hamlet.txt as input (which contains 4921 lines of text), the output should be: hamlet.txt line 4921 FINIS. The tragedie of HAMLET, Prince of Denmarke. If we run the program with a different file fairy-tale.txt, the output might be: fairy-tale.txt line 263 And they all lived happily ever after. Details: If the program is given no arguments or more than one argument, print a suitable message to stderr and exit. If there is one argument, you can assume it names an existing file that can be successfully opened for reading. You don t need to check for this. You can assume that the input file contains lines of text and no input line contains more than 200 characters, including any trailing newline (\n) and terminator (\0) characters. Your answer may be written as a single main function, but you can define additional functions if you wish. Assume that any #include directives for any necessary standard C library files are already provided you do not need to write these. Some useful functions: fopen(filename, r ), fgets(line, max_length, file) You may not need nearly as much space as is available for your answer. Write your answer on the next page (You may detach this page from the exam if that is convenient.) Page 7 of 8

Question 6. (cont). Write your answer here. #define MAX_LINE_SIZE 200 int main(int argc, char ** argv) { int nlines; /* number of lines read */ char line[max_line_size]; /* contents of line nlines */ FILE *in; /* input file */ /* exit if not exactly 1 filename argument */ if (argc!= 2) { fprintf(stderr, "exactly one argument required\n"); return 1; /* open file, read and count lines */ in = fopen(argv[1], "r"); nlines = 0; while (fgets(line, MAX_LINE_SIZE, in)) { nlines++; /* print number of lines and last line in file */ printf("%s line %d\n%s", argv[1], nlines, line); return 0; Note: Solutions on an exam would not be expected to contain extensive comments or #define constants as shown above. Page 8 of 8