1c) (iv) and (v) OR (v) only (hindsight showed that the question was more ambiguous than intended)

Similar documents
The University of Nottingham

1d: tests knowing about bitwise fields and union/struct differences.

Tokens, Expressions and Control Structures

G52CPP C++ Programming Lecture 15

G52CPP C++ Programming Lecture 9

The University of Nottingham

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

OBJECT ORIENTED PROGRAMMING USING C++

CS3157: Advanced Programming. Outline

Introduction to C++ Systems Programming

C++ Coding Standards and Practices. Tim Beaudet March 23rd 2015

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

STRUCTURING OF PROGRAM

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

Absolute C++ Walter Savitch

W3101: Programming Languages C++ Ramana Isukapalli

C++_ MARKS 40 MIN

CS11 Advanced C++ Fall Lecture 7

Interview Questions of C++

Increases Program Structure which results in greater reliability. Polymorphism

Variables. Data Types.

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

C++ (Non for C Programmer) (BT307) 40 Hours

Basic Types, Variables, Literals, Constants

C++ Programming: Polymorphism

CS201 Some Important Definitions

CS201 - Introduction to Programming Glossary By

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

Chapter 15 - C++ As A "Better C"

ECE 3574: Dynamic Polymorphism using Inheritance

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Object Oriented Programming. Solved MCQs - Part 2

Short Notes of CS201

Introduction to Programming

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

These new operators are intended to remove some of the holes in the C type system introduced by the old C-style casts.

What are the characteristics of Object Oriented programming language?

VALLIAMMAI ENGINEERING COLLEGE

G52CPP C++ Programming Lecture 18

The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured

Advanced C++ Programming Workshop (With C++11, C++14, C++17) & Design Patterns

The University of Nottingham

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

C++ Important Questions with Answers

CS 162, Lecture 25: Exam II Review. 30 May 2018

C++ without Classes. CMSC433, Fall 2001 Programming Language Technology and Paradigms. More C++ without Classes. Project 1. new/delete.

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Overload Resolution. Ansel Sermersheim & Barbara Geller ACCU / C++ June 2018

Problem Solving with C++

CPSC 427: Object-Oriented Programming

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

Fast Introduction to Object Oriented Programming and C++

AIMS Embedded Systems Programming MT 2017

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

C++ Coding Standards. 101 Rules, Guidelines, and Best Practices. Herb Sutter Andrei Alexandrescu. Boston. 'Y.'YAddison-Wesley

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

XII- COMPUTER SCIENCE VOL-II MODEL TEST I

Chapter 2. Procedural Programming

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Object-Oriented Principles and Practice / C++

Chapter 5 Object-Oriented Programming

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

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

Introduction to Programming Using Java (98-388)

Writing Functions in C

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Object-Oriented Programming

CPSC 427: Object-Oriented Programming

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Overload Resolution. Ansel Sermersheim & Barbara Geller Amsterdam C++ Group March 2019

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

10. Functions (Part 2)

by Pearson Education, Inc. All Rights Reserved. 2

TPF Users Group Spring 2005

G52CPP C++ Programming Lecture 16

CSE 333 Midterm Exam Sample Solution 5/10/13

Review: C++ Basic Concepts. Dr. Yingwu Zhu

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Axivion Bauhaus Suite Technical Factsheet MISRA

Where do we stand on inheritance?

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

CS304- Object Oriented Programming LATEST SOLVED MCQS FROM FINALTERM PAPERS. MC

Common Misunderstandings from Exam 1 Material

CPSC 427: Object-Oriented Programming

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

EL2310 Scientific Programming

CPSC 427: Object-Oriented Programming

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

A Generic Non-intrusive Smart Pointer Implementation

Transcription:

Answers to 2008/2009 G52CFJ exam Below are the answers to last year s exam, so that you can check how you did. In many cases I have given an example answer and an idea of what elements we would look for. Question 1 1a) (i) only. 1b) (ii) and (v) only 1c) (iv) and (v) OR (v) only (hindsight showed that the question was more ambiguous than intended) 1d) Problem 1: usage of argv[1] before you know it exists, or similar explanation Solution: Fix: Easiest solution is to remove arg1 variable and just use atoi(argv[1]) in the printf Alternatively, split the arg1 into an initial declaration of argv and an assignment of the value after the if block. Problem 2: Parameter type token in the printf should be %d not %c. Solution: Fix: Change %c to %d 1e) You could run this program and see the answer. Answer: 3 5 3 6 3 3 6 3 3 1f) 4 answers: Will accept either a default constructor or a no-parameter constructor A copy constructor An assignment operator A destructor

Question 2 This is a difficult question to mark. Basically we tested the answers given to determine whether the answer was correct, rather than whether it matched a model answer. 2a : class has private members by default, struct has public members by default Mark for mentioning private/public. Mark for mentioning that it is the members. 2b : typedef struct dle struct dle* pnext; struct dle* pprev; long lsaleid; long lproductid; long lsalespersonid; long lquantity; double dprice; Entry; typedef is optional, and without it the Entry will also be missing. int or possibly short could be used instead of long. Important part is the forward and backward pointers and the struct definition itself. Struct name should be propagated forwards into later questions. Note: If no typedef then they need to specify struct later, since this is C not C++ code.

2c : Things to check: Need correct function definition, i.e. params, name, brackets, etc Need malloc for allocating space. Should assign each element of the new structure Should check for special case of no items If no items then set pfirst and set the pprev of current item to NULL If existing item: set ptail->pnext to new entry, and newentry->pprev to the old last item Should set plast/ptail to point to new struct Should set pnext on new structure to NULL For full marks return success or failure depending upon outcome of malloc They need to specify a global head/first pointer, and preferably a tail/last pointer Entry* g_pfirst = NULL; or Entry* g_phead = NULL; Entry* g_plast = NULL; or Entry* g_ptail = NULL; They also need the function itself: int StoreSalesData( long lproductid, long lsalespersonid, long lquantity, double dprice ) Entry* pnewentry = malloc(sizeof(entry)); if ( pnewentry == NULL ) return 0; // Failed to allocate memory pnewentry->lsaleid = GenerateUniqueSalesId(); pnewentry->lproductid = lproductid; pnewentry->ssalespersonid = ssalespersonid; pnewentry->squantity = squantity; pnewentry->dprice = dprice; pnewentry->pnext = NULL; if ( g_pfirst == NULL ) g_pfirst = pnewentry; pnewentry->pprev = NULL; else g_plast->pnext = pnewentry; pnewentry->pprev = g_plast; g_plast = pnewentry; return 1; // success Note: This year s exam avoids having to write lots of code like this.

2d : Check for the following points in the answer: Find the relevant entry Use a new local variable of appropriate pointer type Start at head/first Iterate through using pnext on current item Check sale id correctly Upon finding entry: Unlink prev special case if first set phead Unlink next special case if last set ptail They must free() the entry that was unlinked int FindAndRemoveStoreSalesData( long lsaleid ) Entry* pentry = g_pfirst; while ( pentry!= NULL ) if ( pentry->lsaleid == lsaleid ) if ( pentry->pprev == NULL ) g_pfirst = pentry->pnext; else pentry->pprev->pnext = pentry->pnext; if ( pentry->pnext == NULL ) g_plast = pentry->pprev; else pentry->pnext->pprev = pentry->pprev; free(pentry); return 1; pentry = pentry->pnext; return 0; 2e: 3 marks total for sensible answers, 1 mark for each point made e.g.: Put all of the code into a class as class member functions and member data. Use new instead of malloc, delete instead of free. Encapsulation of data Use exception handling with new instead of checking for NULL. If returning a value then return bool (true/false) instead of int (1/0). Use an inner/nested class for the list items. Hide the head/first and tail/last variables by making them private, accessible only using the class members.

Question 3 3a (iii) and (iv) 3b ONLY (ii) 3c Mention #define of a UNIQUE label. Mention #ifndef and #endif. Mention #endif. Saying #define and #ifndef at top of file and #endif at bottom of file. Determine a unique label (called UNIQUE_LABEL below) for the file, such as the capitalized file path with underscores for the /s in it. Put the following code at the top of the file: #ifndef UNIQUE_LABEL #define UNIQUE_LABEL And this line at the bottom: #endif 3d long min( long val1, long val2 ) return (val1 < val2 )? val1 : val2; Optionally they could inline it Check: Returns long Two long parameters Correct use of ternary operator. Code is actually correct. i.e. would compile. 3e #define MIN(a,b) ((a < b)? a : b) Extra brackets may be used and are safer. Can be split across lines as long as they escape the line breaks. 3f When the evaluation of the argument has a side-effect, because the side-effect will be executed twice on the lesser of the values. 3g template <class mytype> inline mytype GetMin (mytype a, mytype b) return (a<b?a:b); The inline is optional. Keyword class could be replaced by typename

Question 4 4a : Examples (for 1 mark each, max 4): malloc merely allocates space, it is unaware of the details of the object and has to be told the size of the object new calls the constructor, hence initialization is performed new throws exception on failure, malloc returns NULL operator new can be overridden easily to change behaviour for individual types mention the [] form of new and delete 4b : Changing the meaning of an operator when applied to a type The meaning of the operator will be determined by the types of the operands For example, changing the meaning of the assignment operator is a case of operator overloading. (They do not need to give an example.) 4c delete should be delete[] (i.e. array delete) Array length is not big enough for the second usage. (Needs to be 40+) Note: The const is fine but they may think that this is the problem. 4d a comes before b bb comes before b ab comes after aa ab comes before bb aa is the same as aa a comes after ab (ii) Incorrect lines in output: b and bb are the wrong way around a and ab are the wrong way around Reason: Cause is that the return 1 and return -1 are the wrong way round, so it incorrectly sorts the values when one string is a prefix of the other. Solution: Can be fixed by switching the return values of 1 and -1. 4e The following are the compilation/linking errors: =NULL should be ==NULL FILE should be FILE* fshut should be fclose Runtime error: Array not big enough. Runs off the end because pointer is not set back to the beginning of array prior to the read.

Question 5 5a (8 marks total) 1 mark each for the name of each cast. 1 mark each for an explanation of when each case is used. dynamic_cast To perform a safe conversion from a base class to a sub-class while checking the type, on polymorphic references or pointers. Should mention safe or checking the type. Potentially mention returning NULL if conversion fails. Half mark if none of safety/checking/return NULL are mentioned. reinterpret_cast Reinterpret the value of the bits. Treat the bits in memory as a new type without any conversion at all of the bits const_cast Remove or add const-ness or volatile-ness. static_cast Normal casts between types which can be performed with only compile-time information, or where run-time checking is not required. Cannot be used to remove const-ness or require knowledge of sub-types. Probably the most commonly used cast. Explanation will probably be in terms of what it cannot do, or that it covers what the rest of the casts don t. 5b Base : 3 Derived : 4 Base : 1 Base : 4 Base : 3 Derived : 4 Base : 4 Note: Need to verify both the base/derived is correct and that the value is correct Give half a mark if they get just base/derived or just the number for each one. 5c Red Green Orange Indigo Violet Mark each position individually. If they put them in a column instead of next to each other then still give marks it s not marking cout knowledge, but should be marking the exceptions and virtual function knowledge. Testing knowledge that pointers can be thrown, knowledge of virtual functions and knowledge that base class pointer will match sub-class pointer in a catch clause. (Note: Warning is given by compiler due to this.)