Tamilnadu State Board New Syllabus Samacheer Kalvi 12th Computer Science Guide Pdf Chapter 14 Importing C++ Programs in Python Text Book Back Questions and Answers, Notes.

Tamilnadu Samacheer Kalvi 12th Computer Science Solutions Chapter 14 Importing C++ Programs in Python

12th Computer Science Guide Importing C++ Programs in Python Text Book Questions and Answers

I. Choose the best answer (1 Marks)

Question 1.
Which of the following is not a scripting language?
a) JavaScript
b) PHP
c) Perl
d) HTML
Answer:
d) HTML

Question 2.
Importing C++ program in a Python program is called
a) wrapping
b) Downloading
c) Interconnecting
d) Parsing
Answer:
a) wrapping

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

Question 3.
The expansion of API is
a) Application Programming Interpreter
b) Application Programming Interface
c) Application Performing Interface
d) Application Programming Interlink
Answer:
b) Application Programming Interface

Question 4.
A framework for interfacing Python and C++ is
a) Ctypes
b) SWIG
c) Cython
d) Boost
Answer:
d) Boost

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

Question 5.
Which of the following is a software design technique to split your code into separate parts?
a) Object oriented Programming
b) Modular programming
c) Low Level Programming
d) Procedure oriented Programming
Answer:
b) Modular programming

Question 6.
The module which allows you to interface with the Windows operating system is
a) OS module
b) sys module
c) csv module
d) getopt module
Answer:
a) OS module

Question 7.
getopt() will return an empty array if there is no error in splitting strings to
a) argv variable
b) opt variable
c) args variable
d) ifile variable
Answer:
c) args variable

Question 8.
Identify the function call statement in the following snippet.
if_name_ ==’_main_’:
main(sys.argv[1:])
a) main(sys.argv[1:])
b) _name_
c) _main_
d) argv
Answer:
a) main(sys.argv[1:])

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

Question 9.
Which of the following can be used for processing text, numbers, images, and scientific data?
a) HTML
b) C
c) C++
d) PYTHON
Answer:
d) PYTHON

Question 10.
What does _name_ contains ?
a) C++ filename
b) main() name
c) python filename
d) os module name
Answer:
c) python filename

II. Answer the following questions (2 Marks)

Question 1.
What is the theoretical difference between Scripting language and other programming languages?
Answer:
The theoretical difference between the two is that scripting languages do not require the 228 compilation step and are rather interpreted. For example, normally, a C++ program needs to be compiled before running whereas, a scripting language like JavaScript or Python needs not to be compiled. A scripting language requires an interpreter while a programming language requires a compiler.

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

Question 2.
Differentiate compiler and interpreter.
Answer:
Compiler:

  1. It converts the whole program at a time
  2. It is faster
  3. Error detection is difficult. Eg. C++

Interpreter:

  1. line by line execution of the source code.
  2. It is slow
  3. It is easy Eg. Python

Question 3.
Write the expansion of (i) SWIG (ii) MinGW
Answer:
i) SWIG – Simplified Wrapper Interface Generator
ii) MINGW – Minimalist GNU for Windows

Question 4.
What is the use of modules?
Answer:
We use modules to break down large programs into small manageable and organized files. Furthermore, modules provide reusability of code. We can define our most used functions in a module and import it, instead of copying their definitions into different programs.

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

Question 5.
What is the use of cd command? Give an example.
Answer:

  • cd command is used to change the directory.
  • cd command refers to the change directory and absolute path refers to the coupling path.

Syntax:
cd<absolute path>
Example:
“cd c:\program files\open office 4\program”

III. Answer the following questions (3 Marks)

Question 1.
Differentiate PYTHON and C++
Answer:
PYTHON:

  1. Python is typically an “interpreted” language
  2. Python is a dynamic-typed language
  3. Data type is not required while declaring a variable
  4. It can act both as scripting and general-purpose language

C++:

  1. C++ is typically a “compiled” language
  2. C++ is compiled statically typed language
  3. Data type is required while declaring a variable
  4. It is a general-purpose language

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

Question 2.
What are the applications of a scripting language?
Answer:

  • To automate certain tasks in a program
  • Extracting information from a data set
  • Less code-intensive as compared to traditional programming language
  • Can bring new functions to applications and glue complex systems together

Question 3.
What is MinGW? What is its use?
Answer:
MinGW refers to a set of runtime header files, used in compiling and linking the code of C, C++ and FORTRAN to be run on the Windows Operating System.

MinGw-W64 (a version of MinGW) is the best compiler for C++ on Windows. To compile and execute the C++ program, you need ‘g++’ for Windows. MinGW allows to compile and execute C++ program dynamically through Python program using g++.

Python program that contains the C++ coding can be executed only through the minGW-w64 project run terminal. The run terminal opens the command-line window through which the Python program should be executed.

Question 4.
Identify the modulo operator, definition name for the following welcome.display()
Answer:
Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python 1

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

Question 5.
What is sys.argv? What does it contain?
Answer:
sys.argv is the list of command-line arguments passed to the Python program, argv contains all the items that come along via the command-line input, it’s basically an array holding the command-line arguments of the program.
To use sys.argv, you will first have to import sys. The first argument, sys.argv[0], is always the name of the program as it was invoked, and sys.argv[l] is the first argument you pass to the program (here it is the C++ file).
For example:
main(sys.args[1]) Accepts the program file (Python program) and the input file (C++ file) as a list(array). argv[0] contains the Python program which is need not to be passed because by default _main_ contains source code reference and argv[l] contains the name of the C++ file which is to be processed.

IV. Answer the following questions (5 Marks)

Question 1.
Write any five features of Python.
Answer:

  • Python uses Automatic Garbage Collection
  • Python is a dynamically typed language.
  • Python runs through an interpreter.
  • Python code tends to be 5 to 10 times shorter than that written in C++.
  • In Python, there is no need to declare types explicitly
  • In Python, a function may accept an argument of any type, and return multiple values without any kind of declaration beforehand.

Question 2.
Explain each word of the following command.
Python < filename.py > – < i >< C++ filename without cpp extension >
Answer:
Python < filename.py > -i < C++ filename without cpp extension >

Python Keyword to execute the Python program from command-line
filename.py Name of the Python program to execute
– i input mode
C++ filename without CPP extension Name of C++ file to be compiled and executed

Example: Python pycpp.py -i pali

Question 3.
What is the purpose of sys, os, getopt module in Python. Explain
Answer:
Python’s sys module:
This module provides access to some variables used by the interpreter and to functions that interact strongly with the interpreter.

Python’s OS module:

  • The OS module in Python provides a way of using operating system dependent functionality.
  • The functions that the OS module allows you to interface with the Windows operating system where Python is running on.

Python getopt module:

  • The getopt module of Python helps us to parse (split) command-line options and arguments.
  • This module provides two functions to enable command-line argument parsing.

Question 4.
Write the syntax for getopt( ) and explain its arguments and return values
Answer:
Syntax of getopt():
, =getopt.
getopt(argv,options, [long options])
where

i) argv:
This is the argument list of values to be parsed (splited). In our program the complete command will be passed as a list.

ii) options:
This is string of option letters that the Python program recognize as, for input or for output, with options (like or ‘o’) that followed by a colon (r), Here colon is used to denote the mode.

iii) long_options :
This parameter is passed with a list of strings. Argument of Long options should be followed by an equal sign C=’). In our program the C++ file name will be passed as string and ‘I’ also will be passed along with to indicate it as the input file.

  • getopt( ) method returns value consisting of two elements.
  • Each of these values are stored separately in two different list (arrays) opts and args.
  • opts contains list of splitted strings like mode, path.
  • args contains any string if at all not splitted because of wrong path or mode.
  • args will be an empty array if there is no error in splitting strings by getopt().

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

Example:
opts, args = getopt. getopt
(argv, “i:”, [‘ifile=’])

where opts contains [(‘-i’, /c:\\pyprg\\p4′)]
-i:- option nothing but mode should be followed by:
c:\\pyprg\ \p4′ value – the absolute path of C++ file.        ,

In our examples since the entire command line commands are parsed and no leftover argument, the second argument argswill be empty [ ].
If args is displayed using print () command it displays the output as [].

Question 5.
Write a Python program to execute the following C++ coding?
#include <iostream>
using namespace std;
int main( )
{ cout«“WELCOME”;
return (0);
}
The above C++ program is saved in a file welcome.cpp
Python program
Type in notepad and save as welcome.cpp
Answer:
#include<iostream>
using namespace std;
int main( )
{
cout<<“WELCOME”;
retum(0);
}
Open Notepad and type python program and save as welcome.py
import sys,os,getopt
def main(argv):
cppfile =”
exefile = ”
opts, args = getopt.getopt(argv, “i:”, [ifile = ‘])
for o,a in opts:
if o in (“_i”,” ifile “):
cpp_file = a+ ‘.cpp’
exe_file = a+ ‘.exe’
run(cpp_file, exe_file)
def run(cpp_file, exe_file):
print(“compiling” +cpp_file)
os.system(‘g++’ +cpp_file + ‘_o’ + exe_file)
print(“Running” + exe_file)
print(“………………………….”)
print
os.system(exe_file)
print
if — name — == ‘–main –‘;
main(sys.argv[1:])
Output:
——————-
WELCOME
——————-

12th Computer Science Guide Importing C++ Programs in Python Additional Questions and Answers

I. Choose the best answer (1 Marks)

Question 1.
Which one of the following language act as both scripting and general-purpose language?
(a) python
(b) c
(c) C++
(d) html
Answer:
(a) python

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

Question 2.
_______ can act both as scripting and general-purpose language.
a) Python
b) C
c) C++
d) Html
Answer:
a) Python

Question 3.
Identify the script language from the following.
(a) Html
(b) Java
(c) Ruby
(d) C++
Answer:
(c) Ruby

Question 4.
language use automatic garbage collection?
a) C++
b) Java
c) C
d) Python
Answer:
d) Python

Question 5.
is required for the scripting language.
a) Compiler
b) Interpreter
c) Python
d) Modules
Answer:
b) Interpreter

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

Question 6.
How many values can be returned by a function in python?
(a) 1
(b) 2
(c) 3
(d) many
Answer:
(d) many

Question 7.
is an expansion of MinGW
a) Minimalist Graphics for windows
b) Minimum GNU for windows
c) Minimalist GNU for Windows
d) Motion Graphics for windows
Answer:
c) Minimalist GNU for Windows

Question 8.
………….. is not a python module.
a) Sys
b) OS
c) Getopt
d) argv
Answer:
d) argv

Question 9.
Which of the following language codes are linked by MinGW on windows OS?
(a) C
(b) C++
(c) FORTRAN
(d) all of these
Answer:
(d) all of these

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

Question 10.
is both a python-like language for writing C extensions.
a) Boost
b) Cython
c) SWIG
d) Ctypes
Answer:
b) Cython

Question 11.
refers to a set of runtime header files used in compiling and linking the C++ code to be run or window OS.
a) SWIG
b) MinGW
c) Cython
d) Boost
Answer:
b) MinGW

Question 12.
Which is a software design technique to split the code into separate parts?
a) Procedural programming
b) Structural programming
c) Object-Oriented Programming
d) Modular Programming
Answer:
d) Modular Programming

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

Question 13.
Which refers to a file containing python statements and definitions?
a) Procedures
b) Modules
c) Structures
d) Objects
Answer:
b) Modules

Question 14.
Which symbol inos. system ( ) indicates that all strings are concatenated and sends that as a list.
a) +
b) .
c) ()
d) –
Answer:
a) +

Question 15.
The input mode in the python command is given by ………………………….
(a) -i
(b) o
(c) -p
Answer:
(a) -i

Question 16
the command is used to clear the screen in the command window.
a) els
b) Clear
c) Clr
d) Clrscr
Answer:
a) els

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

Question 17.
The keyword used to import the module is
a) Include
b) Input
c) Import
d) None of these
Answer:
c) Import

Question 18.
Which in os.system( ) indicates that all strings are concatenated?
(a) +
(b) –
(c) #
(d) *
Answer:
(a) +

II. Answer the following questions (2 and 3 Marks)

Question 1.
Define wrapping.
Answer:
Importing a C++ program in a Python program is called wrapping up of C++ in Python.

Question 2.
Explain how to import C++ Files in Python.
Answer:
Importing C++ Files in Python:

  • Importing a C++ program in a Python program is called wrapping up of C++ in Python.
  • Wrapping or creating Python interfaces for C++ programs is done in many ways.

The commonly used interfaces are

  • Python-C-API (API-Application Programming Interface for interfacing with C programs)
  • Ctypes (for interfacing with c programs)
  • SWIG (Simplified Wrapper Interface Generator- Both C and C++)
  • Cython (Cython is both a Python-like language for writing C-extensions)
  • Boost. Python (a framework for interfacing Python and C++)
  • MinGW (Minimalist GNU for Windows)

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

Question 3.
Define: g++
Answer:
g++ is a program that calls GCC (GNU C Compiler) and automatically links the required C++ library files to the object code.

Question 4.
Write a note on scripting language?
Answer:
A scripting language is a programming language designed for integrating and communicating with other programming languages. Some of the most widely used scripting languages are JavaScript, VBScript, PHP, Perl, Python, Ruby, ASP, and Tel.

Question 5.
What is garbage collection in python?
Answer:

  • Python deletes unwanted objects (built-in types or class instances) automatically to free the memory space.
  • The process by which Python periodically frees and reclaims blocks of memory that no longer are in use is called Garbage Collection.

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

Question 6.
Define: GNU C compiler.
Answer:
g++ is a program that calls GCC (GNU C Compiler) and automatically links the required C++library files to the object code

Question 7.
Explain how to execute a C++ program through python using the MinGW interface? Give example
Answer:
Executing C++Program through Python: C:\Program Files\OpenOffiice 4\
Python.
cd<absolute path>

Question 8.
Write a note on

  1. cd command
  2. els command

Answer:

  1. cd command: The cd command refers to changes directory and absolute path refers to the complete path where Python is installed.
  2. els command: To clear the screen in the command window.

Question 9.
Define: Modular programming
Answer:

  • Modular programming is a software design technique to split your code into separate parts.
  • These parts are called modules. The focus for this separation should have modules with no or just a few dependencies upon other modules.

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

Question 10.
Define

  1. sys module
  2. OS module
  3. getopt module

Answer:

  1. sys module provides access to some variables used by the interpreter and to functions that interact with the interpreter.
  2. OS module in Python provides a way of using operating system-dependent functionality.
  3. The getopt module of Python helps you to parse (split) command-line options and arguments.

Question 11.
Explain how to import modules and access the function inside the module in python?
Answer:

  • We can import the definitions inside a module to another module.
  • We use the import keyword to do this.
  • Using the module name we can access the functions defined inside the module.
  • The dot (.) operator is used to access the functions.
  • The syntax for accessing the functions from the module is < module name >

Example:
>>> factorial.fact(5)
120
Where
Factorial – Module name. – Dot Operator fact (5) – Function call

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

Question 12.
Explain Module with suitable example
Answer:

  • Modules refer to a file containing Python statements and definitions.
  • A file containing Python code, for e.g. factorial.py, is called a module and its module name would be factorial.
  • We use modules to break down large programs into small manageable and organized files.
  • Modules provide reusability of code.
  • We can define our most used functions in a module and import it, instead of copying their definitions into different programs.

Example:
def fact(n): f=l
if n == 0:
return 0
elif n == 1:
return 1
else:
for i in range(l, n+1):
f= f*i
print (f)
Output:
>>>fact (5)
120

Question 13.
Write an algorithm for executing C++ program pali_cpp.cpp using python program.pall.py.
Answer:
Step 1:
Type the C++ program to check whether the input number is palindrome or not in notepad and save it as “pali_cpp.cpp”.
Step 2:
Type the Python program and save it as pali.py
Step 3:
Click the Run Terminal and open the command window
Step 4:
Go to the folder of Python using cd command
Step 5:
Type the command Python pali.py -i pali_ CPP

Question 14.
Explain _name_ is one such special variable which by default stores the name of the file.
Answer:

  • _name_ is a built-in variable which evaluates the name of the current module.
  • Example: if _name_ == ‘_main _’: main

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

Question 15.
How Python is handling the errors in C++.
Answer:
Python not only execute the successful C++ program, but it also helps to display even errors if any in C++ statement during compilation.
Example:
// C++ program to print the message Hello
/ / Now select File—^New in Notepad and type the C++ program #include using namespace std; int main()
{
std::cout«/,hello// return 0;
}
/ / Save this file as hello.cpp
#Now select File → New in Notepad and type the Python program as main.py
#Program that compiles and executes a .cpp file
The output of the above program :

Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python

III. Answer the following questions (5 Marks)

Question 1.
a) Write a Python code to display all the records of the following table using fetchmany().

Reg No. Name Marks
3001 Chithirai 353
3002 Vaigasi 411
3003 Aani 374
3004 Aadi 289
3005 Aavani 407
3006 Purattasi 521

Python Program:
import sqlite3
connection = sqlite3.connect(” Academy.db”) cursor = connection.cursorQ student_data = [(‘3001″,”Chithirai”,”353″),
(‘3002″ ,”Vaigasii”,”411″),
(‘3003″,” Aani”,”374″),
(‘3004″,” Aadi”,”289″),
(‘3005″,”Aavanii”,”507″),
(‘3006″,”Purattasi”,”521″),]
for p in student_data:
format_str = “””INSERT INTO Student (Regno, Name, Marks) VALUES (“{regno}”,”{name}”,”{marks}”);”””
sql_command = format_str.format(regno=p[0], name=p[l], marks = p[4])
cursor.execute(sql_command)
cursor.execute(“SELECT * FROM student”)
result = cursor. fetchmany(6)
print(*result,sep=:”\n”) connection.commit() connection.close()

b) Write a Python Script to display the following Pie chart.
Samacheer Kalvi 12th Computer Science Guide Chapter 14 Importing C++ Programs in Python 2
Answer:
(Pie chart not given in question paper, Let us consider the following pie chart)
import matplotlib.pyplot as pit sizes = [89, 80, 90,100, 75]
labels = [“Tamil”, “English”, “Maths”, “Science”, “Social”]
plt.pie (sizes, labels = labels, autopet = “%.2f “)
plt.axes().set_aspect (“equal”)
plt.show()

Question 2.
Write a C++ program to check whether the given number is palindrome or not. Write a program to execute it?
Answer:
Example:- Write a C++ program to enter any number and check whether the number is palindrome or not using while loop.
/*. To check whether the number is palindrome or not using while loop.*/
//Now select File-> New in Notepad and type the C++ program
#include <iostream>
using namespace std; intmain( )
{
int n, num, digit, rev = 0;
cout << “Enter a positive number:”;
cin>>num;
n = num;
while(num)
{ digit=num% 10;
rev= (rev* 10) +digit;
num = num/10;
cout << “The reverse of the number is:”<<rev <<end1;
if (n ==rev)
cout<< “The number is a palindrome”;
else
cout<< “The number is a palindrome”;
return 0;
}
//save this file as pali_cpp.cpp
#Now select File → New in Notepad and type the Python program
#Save the File as pali.py.Program that complies and executes a .cpp file
import sys, os, getopt
def main(argv);
cpp_file=”
exe_file=”
opts.args = getopt.getopt(argv, “i:”,[‘ifile-])
for o, a in opts:
if o in (“-i”, “—file”):
cpp_file =a+ ‘.cpp’
exe_file =a+ ‘.exe’
run(cpp_file, exe_file)
def run(cpp_file, exe_file):
print(“Compiling” + eppfile)
os.system(‘g++’ + cpp_file +’ -o’ + exe file)
print(“Running” + exe_file)
print(“——————“)
print
os.system(exefile)
print
if_name_==’_main_’: #program starts executing from here
main(sys.argv[1:])
The output of the above program:
C:\Program Files\OpenOffice 4\program>Python c:\pyprg\pali.py -i c:\pyprg\pali_cpp
Compiling c:\pyprg\pali_cpp.cpp
Running c:\pyprg\pali_cpp.exe
———————————–
Enter a positive number: 56765
The reverse of the number is: 56765
The number is a palindrome
C:\Program Files\OpenOffice 4\program>Python c:\pyprg\pali.py -i c:\pyprg\pali_cpp Compiling c:\pyprg\pali_cpp.cpp Running c:\pyprg\pali_cpp.exe
Enter a positive number: 56756
The reverse of the number is: 65765
The number is not a palindrome
————————