Python and C++ Together: book back answers

22 solved questions from the end of this chapter, Class 12 Computer Science, Tamil Nadu State Board. Each answer is checked against the textbook, and you can open the page that proves it.

← Notes and quiz for Python and C++ Together

Choose the best answer (1 Mark)

  1. Which of the following is not a scripting language?

    • (A) JavaScript
    • (B) PHP
    • (C) Perl
    • (D) HTML
    Try it, then show the answer

    (D) HTML

    The chapter lists JavaScript, PHP and Perl as scripting languages, but does not list HTML as one.

    From the book: “Some of the most widely used scripting languages are JavaScript, VBScript, PHP, Perl, Python, Ruby, ASP and Tcl.”

  2. Importing C++ program in a Python program is called

    • (A) wrapping
    • (B) Downloading
    • (C) Interconnecting
    • (D) Parsing
    Try it, then show the answer

    (A) wrapping

    The chapter names importing a C++ program in Python as wrapping up C++ in Python.

    From the book: “Importing C++ program in a Python program is called wrapping up of C++ in Python.”

  3. The expansion of API is

    • (A) Application Programming Interpreter
    • (B) Application Programming Interface
    • (C) Application Performing Interface
    • (D) Application Programming Interlink
    Try it, then show the answer

    (B) Application Programming Interface

    The chapter explicitly gives this expansion for API.

    From the book: “Python-C-API (API-Application Programming Interface for interfacing with C programs)”

  4. A framework for interfacing Python and C++ is

    • (A) Ctypes
    • (B) SWIG
    • (C) Cython
    • (D) Boost
    Try it, then show the answer

    (D) Boost

    The chapter identifies Boost.Python as a framework for interfacing Python and C++.

    From the book: “Boost. Python (a framework for interfacing Python and C++)”

  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
    Try it, then show the answer

    (B) Modular programming

    Modular programming is defined as a technique for splitting code into separate parts.

    From the book: “Modular programming is a software design technique to split your code into separate parts.”

  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
    Try it, then show the answer

    (A) OS module

    The OS module provides a way to use operating-system-dependent functionality, including interfacing with Windows.

    From the book: “The OS module in Python provides a way of using operating system dependent functionality.”

  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
    Try it, then show the answer

    (c)args variable

    The chapter says args is an empty list when there is no error.

    From the book: “args will be an empty list if there is no error.”

  8. Which of the following can be used for processing text, numbers, images, and scientific data?

    • (A) HTML
    • (B) C
    • (C) C++
    • (D) PYTHON
    Try it, then show the answer

    (D) PYTHON

    The chapter states that Python can be used to process text, numbers, images and scientific data.

    From the book: “It can be used for processing text, numbers, images, scientific data”

  9. What does __name__ contains ?

    • (A) c++ filename
    • (B) main() name
    • (C) python filename
    • (D) os module name
    Try it, then show the answer

    (C) python filename

    By default, __name__ stores the name of the Python file.

    From the book: “__name__ is one such special variable which by default stores the name of the Python file.”

Part - II — Answer the following questions (2 Marks)

  1. What is the theoretical difference between Scripting language and other programming language?

    Try it, then show the answer

    Scripting languages do not require the compilation step and are interpreted. A scripting language requires an interpreter, while a programming language requires a compiler.

    The chapter describes this as the theoretical distinction, while noting that a language's label can depend on the environment in which it is used.

    From the book: “scripting languages do not require the compilation step and are rather interpreted.”

  2. Differentiate compiler and interpreter.

    Try it, then show the answer

    A compiler is required for a programming language and compiles the program before it runs. An interpreter is required for a scripting language and interprets it without a compilation step.

    The chapter contrasts the compilation step used for C++ with interpreted execution for Python.

    From the book: “A scripting language requires an interpreter while a programming language requires a compiler.”

  3. Write the expansion of (i) SWIG (ii) MinGW

    Try it, then show the answer

    (i) SWIG — Simplified Wrapper Interface Generator (ii) MinGW — Minimalist GNU for Windows

    Both expansions are given in the chapter's list of commonly used interfaces.

    From the book: “SWIG (Simplified Wrapper Interface Generator- Both C and C++)”

  4. What is the use of modules?

    Try it, then show the answer

    Modules break large programs into small, manageable and organized parts. They also enable code reuse by letting programmers define commonly used functions once and import them into other programs.

    The chapter describes modules as files containing Python statements and definitions that help organize and reuse code.

    From the book: “We use modules to break down large programs into small manageable and organized program.”

  5. What is the use of cd command. Give an example.

    Try it, then show the answer

    The cd command is used to change a directory. For example, type cd pyprg to go to the pyprg directory.

    The chapter gives this command as an example of changing to the pyprg directory.

    From the book: “To change a directory 'cd' command is used.”

Part - III — Answer the following questions (3 Marks)

  1. Differentiate PYTHON and C++

    Try it, then show the answer

    Python is typically interpreted, dynamically typed, and does not require data types to be declared for variables. C++ is typically compiled, statically typed, and requires data types to be declared. Python code tends to be 5 to 10 times shorter than C++ code.

    These are contrasts stated in the chapter's introduction and features list.

    From the book: “Python code tends to be 5 to 10 times shorter than that written in C++.”

  2. What is MinGW? What is its use?

    Try it, then show the answer

    MinGW is a set of runtime header files used to compile and link C, C++ and FORTRAN code for Windows. It allows C++ programs to be compiled and executed dynamically through Python using g++.

    The chapter describes both what MinGW contains and how it is used from Python.

    From the book: “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 Windows Operating System.”

  3. Identify the module ,operator, definition name for the following welcome.display()

    Try it, then show the answer

    Module: welcome Operator: dot (.) Definition name: display

    In module.function notation, the name before the dot is the module and the name after it is the function accessed.

    From the book: “The syntax for accessing the functions from the module is <module name> . <function name>”

  4. What is sys.argv? What does it contain?

    Try it, then show the answer

    sys.argv is the list of command-line arguments passed to the Python program. It contains the arguments supplied through command-line input; sys.argv[0] is the Python program name, and later entries contain the arguments passed to it.

    The chapter uses sys.argv[1:] to pass the arguments after the script name to main().

    From the book: “sys.argv is the list of command-line arguments passed to the Python program.”

Part - IV — Answer the following questions (5 Marks)

  1. 1 Write any 5 features of Python.

    Try it, then show the answer

    Python is an interpreted, high-level, general-purpose programming language. The chapter describes these features of Python in comparison with C++.

    1. Interpreted language: Python runs through an interpreter, whereas C++ is pre-compiled.
    2. Dynamic typing: Python is dynamically typed, so a variable’s data type need not be declared explicitly.
    3. No explicit type declaration: Python does not require the programmer to declare data types while declaring variables.
    4. Automatic garbage collection: Python automatically removes unwanted objects and reclaims memory that is no longer in use.
    5. Shorter code: Python programs tend to be much shorter than equivalent programs written in C++.

    Example: Python automatically frees memory by removing unwanted objects.

    These features make Python flexible and concise for programming.

    From the book: “Python uses Automatic Garbage Collection whereas C++ does not.”

  2. What is the purpose of sys,os,getopt module in Python.Explain

    Try it, then show the answer

    The sys, os and getopt modules are standard Python modules used in the program that compiles and executes C++ code. Each module provides a different function for handling command-line input and interacting with the operating system.

    1. sys module: The sys module gives access to built-in variables used by the interpreter. In this program, sys.argv supplies the command-line arguments to the main function.
    2. sys.argv: sys.argv is a list of the command-line arguments. The first item is the Python program name; the following item provides the C++ file argument.
    3. os module: The os module provides a way to use operating-system-dependent functionality. It allows the Python program to interface with the Windows operating system.
    4. os.system(): The system() function executes a command in the shell. It is used to run the g++ compilation command and then execute the generated program.
    5. getopt module: The getopt module parses, or splits, command-line options and arguments. Its getopt() method enables command-line argument parsing.
    6. getopt() results: The getopt() method returns two values, stored in opts and args. opts holds the parsed option and its value; args is empty when there is no error or leftover argument.

    Example: In the program, opts, args = getopt.getopt(argv, "i:") parses the input option; os.system('g++ ' + inp_file + ' -o ' + exe_file) compiles the C++ file.

    Thus, sys provides command-line arguments, getopt parses them, and os runs operating-system commands.

    From the book: “The getopt module of Python helps you to parse (split) command-line options and arguments.”

  3. Write the syntax for getopt() and explain its arguments and return values

    Try it, then show the answer

    The getopt() function in Python parses command-line options and arguments.

    1. Syntax: The function returns two values, which are assigned to opts and args.
    2. argv: This is the list of values to be parsed. In the program, the complete command is passed as a list.
    3. options: This string contains the option letters recognised by the program, such as input or output options. A colon after an option letter denotes the mode.
    4. long_options: This is a list of strings for long options. A long-option argument must be followed by an equal sign.
    5. Return value: opts: The first returned value contains the parsed options and their values, such as the mode and the C++ file path.
    6. Return value: args: The second returned value contains an error string if the command has a wrong path or mode. It is an empty list when there is no error.

    Example: opts, args = getopt.getopt(argv, "i:",['ifile='])

    Thus, getopt() separates command-line input into parsed options and the remaining arguments.

    From the book: “getopt() method returns value consisting of two elements.”

  4. 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.

    Try it, then show the answer

    Importing and executing a C++ program through a Python program is called wrapping. The Python program uses the `g++` compiler to compile the C++ file and then executes the resulting file.

    1. Save the C++ file: Save the given C++ program as `welcome.cpp`. The Python program receives the C++ file name without its `.cpp` extension.
    2. Import required modules: Import `sys`, `os` and `getopt` to read the command-line arguments, parse the input option and interact with the operating system.
    3. Parse the input option: Use `getopt.getopt()` to read the `-i` option and the C++ file name supplied when running the Python program.
    4. Compile and execute: Use `os.system()` to run the `g++` compilation command, then use it again to execute the generated file. The C++ program displays `WELCOME`.

    Example: Run the program with: `python welcome.py -i welcome`

    Thus, the Python program compiles and executes `welcome.cpp` and displays its output.

    From the book: “os.system('g++ ' + inp_file + ' -o ' + exe_file)”

Next chapterDatabases and SQLite