Docsity

AdvancedProgramming in C++, Exams of Object Oriented Programming

Toggle sidebar
bg1
Applied Programming
Midterm, Fall 2014
Date: 22th Oct 2014 Marks: 60 Time: 2hrs
Roll Number: _______________ Section: ____________
Department of Computer Science
National University of Computer & Emerging Sciences, Lahore Page 1 of 11
Complete paper on the question paper. No answer sheet is required for solving this
paper. Query is not allowed. Understanding is part of paper. Good Luck!
Question #1 Marks (20)
1. A two dimensional array is stored in linear fashion in physical memory. Consider logical/ view
representation is in row major order. Write indexing formula for accessing an element in a two
dimensional array in column major order. Consider M as base address (normally, it is 0), i as specific
row number, j as column number, m as total number of rows and n as total number of columns. (5)
2. Suppose we are given the following specification for a Stack class of integers:
class Stack
{
public:
Stack();
//allocates sufficient memory to stack
~Stack();
//deallocates memory
bool isEmpty( );
// return true if stack is empty
bool isFull( );
// return true if stack is full
bool push(int x);
// push x on the stack, return true if successful else return false
bool pop(int &x);
// pops from stack into x, returns true if successful else returns false
private:
…
};
Implement a non-member & non recursive function Reverse which takes an object of Stack class as a
parameter & reverses its contents by using only the above mentioned member functions of the Stack
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download AdvancedProgramming in C++ and more Exams Object Oriented Programming in PDF only on Docsity!

Midterm, Fall 2014 Date: 22th^ Oct 2014 Marks: 60 Time: 2hrs

Roll Number: _______________ Section: ____________

Department of Computer Science

Complete paper on the question paper. No answer sheet is required for solving this

paper. Query is not allowed. Understanding is part of paper. Good Luck!

Question #1 Marks (20)

1. A two dimensional array is stored in linear fashion in physical memory. Consider logical/ view

representation is in row major order. Write indexing formula for accessing an element in a two

dimensional array in column major order. Consider M as base address (normally, it is 0), i as specific

row number, j as column number, m as total number of rows and n as total number of columns. (5)

2. Suppose we are given the following specification for a Stack class of integers:

class Stack { public: Stack(); //allocates sufficient memory to stack ~Stack(); //deallocates memory bool isEmpty( ); // return true if stack is empty bool isFull( ); // return true if stack is full bool push(int x); // push x on the stack, return true if successful else return false bool pop(int &x); // pops from stack into x, returns true if successful else returns false private: … };

Implement a non-member & non recursive function Reverse which takes an object of Stack class as a

parameter & reverses its contents by using only the above mentioned member functions of the Stack

Midterm, Fall 2014 Date: 22th^ Oct 2014 Marks: 60 Time: 2hrs

Roll Number: _______________ Section: ____________

Department of Computer Science

class. You can only use one extra stack and constant number of variables. You are not allowed to use

arrays in the function. (15)

For example

A

A

A

An

An

An-

A

A

Stack A Stack A

Midterm, Fall 2014 Date: 22th^ Oct 2014 Marks: 60 Time: 2hrs

Roll Number: _______________ Section: ____________

Department of Computer Science

Question #2 Marks (20)

1. We have a circular doubly linked list with a pointer list pointing to the first node in the list as shown

below:

After each of the following assignments, indicate changes made in the list by showing which links

have been modified by drawing the modified list. The second assignment should make changes in

the list modified by the first assignment and so on. (10)

a. list->next->next = list->prev;

b. list->prev->prev->prev = list;

c. list->next->prev = list->prev->prev;

Midterm, Fall 2014 Date: 22th^ Oct 2014 Marks: 60 Time: 2hrs

Roll Number: _______________ Section: ____________

Department of Computer Science

d. list->next->next = list->prev->prev;

2. A sparse matrix is type of matrix having the value of majority elements as 0. For example

To avoid waste of space LinkedList representation is preferred. Draw a node for storing an element

in sparse matrix. Also represent linked list representation of above matrix. Store total number of

rows and total number of columns without using any extra data member. (10)

Midterm, Fall 2014 Date: 22th^ Oct 2014 Marks: 60 Time: 2hrs

Roll Number: _______________ Section: ____________

Department of Computer Science

//preparation of the socket address servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR _ANY); servaddr.sin_port = htons(SERV_PORT);

//bind the socket bind (listenfd, (struct sockaddr *) &ser vaddr, sizeof(servaddr));

listen (listenfd, LISTENQ);

for ( ; ; ) {

clilen = sizeof(cliaddr); connfd = accept (listenfd, (struct soc kaddr *) &cliaddr, &clilen); // write code here

//preparation of the socket address servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_A NY); servaddr.sin_port = htons(SERV_PORT);

//bind the socket bind (listenfd, (struct sockaddr *) &serv addr, sizeof(servaddr));

listen (listenfd, LISTENQ);

for ( ; ; ) {

clilen = sizeof(cliaddr); connfd = accept (listenfd, (struct soc kaddr *) &cliaddr, &clilen); // write code here

Midterm, Fall 2014 Date: 22th^ Oct 2014 Marks: 60 Time: 2hrs

Roll Number: _______________ Section: ____________

Department of Computer Science

Extra Sheet

Midterm, Fall 2014 Date: 22th^ Oct 2014 Marks: 60 Time: 2hrs

Roll Number: _______________ Section: ____________

Department of Computer Science

Midterm, Fall 2014 Date: 22th^ Oct 2014 Marks: 60 Time: 2hrs

Roll Number: _______________ Section: ____________

Department of Computer Science