Thursday, July 18, 2019

Class Test – 15th July 2019 (Computer Science) - 12 SC


Question and Answers for the class test 1  15th july – COMPUTER SCIENCE - 12 SC

Q1. Write a function called my_buzz that takes a number.
If the number is divisible by 3, it should return “Fizz”.
If it is divisible by 5, it should return “Buzz”.
If it is divisible by both 3 and 5, it should return “FizzBuzz”.Otherwise, same no.
Ans1.     Program:
1.  def my_buzz(n):  
2.      if n % 3==0 and n % 5==0:  
3.          return 'FizzBuzz'  
4.      elif n % 3 == 0:  
5.          return 'Fizz'  
6.      elif n % 5 == 0:  
7.          return 'Buzz'  
8.      else:  
9.          return n  
10.   
11. a=int(input('enter a number'))  
12. print(my_buzz(a))  
Input and Output:
1.  ==== RESTART: D:\pythonVirtualEnv\practicePro\examplePro\classTestPro.py ====  
2.  enter a number15  
3.  Fizz  
4.  >>>   
5.  ==== RESTART: D:\pythonVirtualEnv\practicePro\examplePro\classTestPro.py ====  
6.  enter a number20  
7.  Buzz  
8.  >>>   
9.  ==== RESTART: D:\pythonVirtualEnv\practicePro\examplePro\classTestPro.py ====  
10. enter a number11  
   11  
Q2. Find the output of the following:

a.    1.  def increment(n):  

2.      n.append([4])  
3.      return n  
4.  l=[1,2,3]  
5.  m=increment(l)  
6.  print(l,m)                                                                b. 


 Ans (a). [123, [4]] [123, [4]] 
      

    1.  def increment(n):  
2.      n.append([49])  
3.      return n[0], n[1], n[2], n[3]  
4.  l=[23,35,47]  
5.  m1,m2,m3,m4=increment(l)  
6.  print(l)  
7.  print(m1,m2,m3,m4)  
8.  print(l[3] == m4)  


Ans (b). [233547, [49]]
           23 35 47 [49]
            True

Q3. Write a python program to read last 2 lines of a text file.
Ans3. Program:
1.  f=open('newFile.txt','r')  
2.  ls=f.readlines()  
3.  print('The Last two lines of the file are')  
4.  print(str(ls[-2]),end='')  
5.  print(str(ls[-1]),end='')  
Output:
1.  ==== RESTART: D:\pythonVirtualEnv\practicePro\examplePro\classTestPro.py ====  
2.  The Last two lines of the file are  
3.  so its a rainy day  
4.  so you get a raincoat  
Q4. Write a method in python to write multiple lines of text contents into a text file mylife.txt.
Ans4. Program:
1.  f=open('newFile.txt','a')  
2.  ls=['but its a nice day\n','so its a fun\n','lets have fun then\n']  
3.  f.writelines(ls)  
4.  f.close()  
Q5. Write a method in python to read the content from a text file diary.txt line by line and display the same on screen.
Ans5. Program:
1.  f=open('newFile.txt','r')  
2.  print(f.read())  
Output:
1.  ==== RESTART: D:\pythonVirtualEnv\practicePro\examplePro\classTestPro.py ====  
2.  but its a nice day  
3.  so its a fun  
4.  lets have fun then  
5.  but its a nice day  
6.  so its a fun  
7.  lets have fun the  





Share:

Sunday, July 14, 2019

Class Test 3 CS/IP 15-7-2018 - CLASS 11

Question and Answers of Class Test 3 – 15th July 11 

Q1. Distinguish internal and external memory of a computer?
Ans1.
Internal Memory
External Memory
This type of memory is usually fixed on the motherboard itself This type of memory is connected to the motherboard via some cables and not directly on it
The read/write speed is very fast compared to External memory The read/write speed is slow as compared to Internal memory
Cost per bit is high compared to external Memory Cost per bit is low compared to internal Memory
More connecting pins to transfer data at higher rate Less connecting pins
Less portable Highly portable
Example: RAM, eMMC(embedded MultiMediaCard) Example: HDD, DVD, Pendrive

Q2. Differentiate compiler and interpreter.
Ans2.
Compiler
Interpreter
Read the complete program entirely and generates an executable(usually bytecode) if no errors exists Converts one statement to machine executable code at a time
Code analysis time is more but the execution time is less Code analysis time is less but execution is more
Once the executable is generated,  compiler is not required for the execution Interpreter is required for the entire process of execution
Less memory efficient as an bytecode gets generated More memory efficient as no bytecode is generated
Modification of the features of the program is lengthy Modification of the features of the program is easy and less time consuming
Removing of bugs in the code is complicated as all the errors are presented at once at the end Removing of bugs are easy as errors are generated instantly
Example: c, c++, Example: Python, javascript, MATLAB

Q3. What is the role of input unit in a computer?
Ans3. The role of input device is to take data and instruction form the user or from any other source and convert the data/instruction in such a format (through encoding) that is usable by the computer. Example of input unit are-Keyboard, mouse, microphone etc.

Q4. What are the jobs of the Operating System?
Ans4. Few jobs of the operating system is listed below
  1. Processor management
  2. Process management
  3. Memory management
  4. Device management
  5. Storage management
  6. Application interface
  7. User interface
Q5. What are the types of Operating System with example?
Ans5. One of the classifications of OS can be:
  • Single-User, Single Task Operating System: only one user and one application can be handled by the operating system. Example:- CPM (Control program for microprocessor), DOS(Disk operating system).
  •     Single-User, Multi-Task Operating System: Only one user but multiple applications can be handled by the OS. Example:- windows 95 etc.
  •   Multiuser Operating System: This type operating system is able handle multiple users and multiple applications. Eg:- Linux (can support 4294967296 (2^32) hosts)
  •  Multiprocessing Operating System: The OS is able to schedule (i.e. handle) multiple processor. Eg:- linux, windows 7
  •  Embedded Operating System: These types of OS are tailored to do one job and does not support all the features general purpose operating OS. Eg:- OS for cars, Traffic lights, POS(point of sale) terminal, elevators etc
  •  Distributed Operating System: A distributed operating system runs over a collection of independent, networked, communicating, and physically separate computational nodes. These OS is able handle jobs which are serviced by multiple CPUs. Each individual node holds a specific software subset of the global aggregate operating system. Eg:- WWW is the largest distributive operating system, DYSEAC.

Share:

Sunday, July 7, 2019

File handling with python programming language:

Why do we need to study this?
                  Most of the scripts (i.e. python programs) we use takes input from the user and printed the result on the shell but nothing could be saved or retrieved
                  With Files we can store result on the HDD (i.e. secondary storage) to be used later
What is a File?
              A File is a collection of records i.e. a collection of data on a secondary storage with associated information (i.e. Metadata) like the Filename and the type of file, permission etc.
               A File is used to store data like an image file consisting of pixel data
Types of Files that we will work with
     Data Files – are the files that store data from an application or to be used by an application later
  1. Text Files: Stores data in ASCII or Unicode character and each text line is terminated by EOL(End Of Line). In Python the Default EOL is ‘\n’
  2. Binary File: A binary file is dump of the data in the same format as was held in memory by an application i.e. the file content that is returned is RAW. There is no delimiter for a line of a data.
OPERATION ON FILES:
Opening  files:
Syntax:
                <file_objectname>=open(<filename>,<mode>)
 Example:
  1. fl=open('newFile1.txt''w'#when opening new file 'w' i.e. write mode should be used  
  2.                              #when the file to be opened is in the same directory as the script  
  3. fl1=open('c:\\main\\newFile.txt','w'# to be opened from a particular directory 
             
File open modes:     
Text File Mode Binary File Mode Description Notes
'r' 'rb' read only File must exist already, otherwise Python raise I/O error
'w' 'wb' write only 1. if the file does not exists, file is created
2. if the file exists, Python will truncate existing data and over-write in the     file.  So this mode must be used with caution
 'a' 'ab' append 1. File is in write only mode.
2. if the file exits, the data in the file is retained and new data being written will be appended to the end
3. if the file does not exist, Python will create a new file
'r+' 'r+b'  or  'rb+' read and write 1. File must exist otherwise error is raised
2. Both reading and writing operations can takes place
'w+' 'w+b' or 'wb+' write and read 1. File is created if does not exits.
2. If file exits, file is truncated (past data is lost)
3. Both reading and writing operatins can take place
'a+' 'a+b' or 'ab+' write and write 1. File is created if does not exits
2. If file exists, file's existing data is retained; new data is appended.
3. Both reading and writing operations can take place
Two ways to give path in filenames correctly are:
   1.   Double the slashes e.g.
  1.            f=open('c:\\temp\\data.txt','r')  
   2.   Give raw string by prefixing the file-path string with an r e.g.       
  1.            f=open(r'c:\temp\data.txt','r')  

File object/File Handle:
         A file object (also known as File handle) is a reference to a file on disk. It opens and makes it available for a number of different task.

Closing a File:
Syntax:
                 <fileHandle>.close()
Example:
                  
  1. f=open('file.txt','w')  
  2. f.close()  

Handling Errors that may occur:
                      There may be condition where the file we are trying to open in not there or we does not have permission to access it, then an Exception may occur which if not handled properly may crash the entire program. So we have to open a file inside a try finally block

  1. try:  
  2.    f = open("test.txt","w")  
  3.    # perform file operations  
  4. finally:  
  5.    f.close()  
Writing in Files:
              Function that can be used to write onto files are:
  1. write():     
           Syntax:
                      <filehandle>.write(str)              #writes string str to file referenced by <filehandle>
           Example:
                       
  1. f = open("test.txt","w")  
  2. f.write("this is a line that will be written "    

     2.   writelines():
            Syntax:
                         <filehandle>.writeline(L)     #writes all the string in list L as lines to file
            Example:
  1. f = open("test.txt","w")  
  2. ls=["Jack and Jill went up the hill \n","To fetch a pail of water \n"]  
  3. f.writelines(ls)  
  4. f.close()

Appending to Files:
                When we open a file in 'w' (i.e. in write mode ), we overwrite the existing data in the file which may be undesirable so we use the append mode. so it allows to write in the file without overwriting the existing data:
         Syntax:
                     <filehandle>=open(<filename>, 'a')
                       <filehandle>.write(str)
         Example:
               
  1. fl = open("myfile.txt","a")   #append mode   
  2. fl.write("its a new data \n")   
  3. fl.close()  

Reading From a File:
            ways to read from are listed below:
  • read():
                      The function returns the bytes from the file in the form of a string. Reads n number of bytes , if nothing is specified as argument to the function then the complete file is returned as string
          Syntax:
                 <filehandle>.read(n)
          Example:
  1. fl = open("myfile.txt","r+")   #append mode   
  2. print(fl.read(20))  
  3. fl.close() 
  •  readline(n):
                       Reads a line of input. If n is specified reads at most n bytes. returns the read bytes in the form of a string ending within ln(line) character or return a blank string if no more bytes are left for reading in the file
           Syntax:
                        <filehandle>.readline(n)
           Example:
  1. fl=open('newFile.txt','r+')  
  2. sti=fl.readline()  
  3. print(sti)  
  4. fl.close()  
           Output:
  1. ==== RESTART: D:/pythonVirtualEnv/practicePro/examplePro/fileHandling.py ====  
  2. we work we try to be better  
  • readlines():
                          Reads all lines and returns them in a list
             Syntax:
                        <filehandle>.readlines()
             Example:
  1. fl=open('newFile.txt','r+')  
  2. ls=fl.readlines()  
  3. print(type(ls))  
  4. print(ls)  
             Output:
  1. ==== RESTART: D:/pythonVirtualEnv/practicePro/examplePro/fileHandling.py ====  
  2. <class 'list'>  
  3. ['we work we try to be better\n''we work we try to be better\n''we work we try to be better']  


Share: