Exception Handling & Generate Functions

Sponsor Area

Question
CBSEENCO12011544

Write Addnew(Book) and Remove(Book) methods in Python to Add a new Book and Remove a Book from a List of Books, considering them to act as PUSH and POP operations of the data structure Stack.

Solution

class stack:
	Book=[]
	def Addnew(self):
		Name=input('Enter Book Name :')
		stack.Book.append(Name)
	def Remove(self):
		if (stack.Book==[]):
			print 'Stack Empty'
		else:
			print 'Deleted Book is : ',stack.Book.pop()

Sponsor Area

Question
CBSEENCO12011545

Write the definition of a Method AFIND(CITIES) to display all the city names from a list of CITIES, which are starting with alphabet A.

For example:
If the list CITIES contains ['AHMEDABAD','CHENNAI','NEW DELHI','AMRITSAR','AGRA']

The following should get displayed
AHMEDABAD
AMRITSAR
AGRA

Solution

def AFIND(CITIES):
	for i in CITIES:
		if i[0]=='A':
			print i

Question
CBSEENCO12011547

Differentiate between file modes r+ and w+ with respect to Python.

Solution

Difference between r+ and w+ is  given below:
r+ Opens a file for both reading and writing. The file pointer placed at the beginning of the file.
w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

Question
CBSEENCO12011550

Observe the following table CANDIDATE carefully and write the name of the RDBMS operation out of (i) SELECTION (ii) PROJECTION (iii) UNION (iv) CARTESIAN PRODUCT, which has been used to produce the output as shown in RESULT ? Also,find the Degree and Cardinality of the RESULT.

TABLE: CANDIDATE

No NAME STREAM
C1 AJAY LAW
C2 ADITI MEDICAL
C3 ROHAN EDUCATION
C4 RISHAB ENGINEERING

RESULT

No NAME
C3 ROHAN

Solution

(i) SELECTION
OR
(ii) PROJECTION
The Degree and Cardinality of the RESULT.
DEGREE = 2
CARDINALITY = 1