Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

301.

Explain Comments in Python.

Answer»

Comments in Python : 

A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up to the physical line end are part of the comment, and the Python interpreter ignores them. 

# !/usr/bin/python 

# First comment 

print 

“Hello, Python!” ; 

# second comment 

This will produce following result: 

Hello, Python! 

A comment may be on the same line after a 

statement or expression: 

name = “Madisetti” 

# This is again comment 

You can comment multiple lines as follows : 

# This is a comment. 

# This is a comment, too. 

# This is a comment, too.

# I said that already.

302.

Give an example of “random()”

Answer»

The following example shows the usage of random() method.

# !/usr/bin/python 

import random 

# First random number 

print “random(): ” , 

random.random() # Second random number print “random(): “, 

random.random() 

This will produce the following results 

random(): 0.281954791393 

random(): 0.309090465205

303.

Write a program to illustrate function call.

Answer»

#!/usr/bin/python # Function definition is here def printme(str): “This prints a passed string into this function” print tr; 

return; # Now you can call printme function printme(“Mohd saif naqvi”); 

printme(“Chairman of Waltons Technology”) 

It produces following result:

Mohd saif naqvi 

Chairman of Waltons Technology

304.

What do you mean by function?

Answer»

A function is a block of organized, reusable code that is used to perform a single, related action.

305.

What are the rules to define a function in Python?

Answer»

You can define functions to provide the required functionality. Here are simple rules to define a function in Python:

(1) Function blocks begin with the keyword def followed by the function name and parentheses (( )) 

(2) Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses. 

(3) The first statement of a function can be an optional statement-the documentation string of the function or docstring. 

(4) The code block within every function starts with a colon (:) and is indented. 

(5) The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.

306.

Explain Global vs. Local variables.

Answer»

Variables that are defined inside a function body have a local scope, and those defined outside have a global scope. This means that local variables can be accessed only inside the function in which they are declared whereas global variables can be accessed throughout the program body by all functions. When you call a function, the variables declared inside it are brought into scope.

307.

Write a program to illustrate “Required arguments”.

Answer»

#!/usr/bin/python 

# Function definition is here 

def printme (str): “This prints a passed string into this function” 

print str; 

return; 

# Now you can call printme function printme(); 

It produces following result: 

Traceback (most recent call last): 

File “test.py”, line 11, in <module> 

printme() 

TypeError: printme() takes exactly 1 argument (0 given)

308.

Write a program to illustrate “variablelength arguments”.

Answer»

#!/usr/bin/python 

# Function definition is here 

def printinfo(argl,*vartuple):

“This prints a variable passed arguments” 

print “Output is:” 

print argl for var in vartuple : 

print var return; 

# Now you can call printinfo function 

printinfo(10); printinfo(70,60,50);

When the above code is executed, it produces following result:

Output is: 

10 

Output is: 

70 

60 

50

309.

Write a program to illustrate “Keyword arguments”.

Answer»

#!/usr/bin/python 

# Function definition is here 

def printme(str):”Thisprints a passed string into this function” 

print str; return; 

# Now you can call printme function

 printme(str=”My string”); 

When the above code is executed, it produces following result: 

My string

310.

What is a Python module ? What is its significance ? .

Answer»

A ‘module’ is a chunk of python code that exists in its own (.pu) file and is intended to be used by python code outside iteself. Modules allow one to fundle together code in a form in which it can easily be used later. 

The modules can be ‘imported’ in other programs so the function and other definitions in imported modules becomes availabe to code that imports them.

311.

What value will be return by log(x[, base])?

Answer»

With one argument, return the natural logarithm of x (to base e). With two arguments, return the logarithm of x to the given base, calculated as log(x) log(base)

312.

Define pow(x, y) function in Python ?

Answer»

Return x raised to the power y.. In particular, pow(1.0, x) and pow(x, 0.0) always return 1.0, even when x is a zero or a NaN. If both x and y are finite, x is negative, and y is not an integer then pow(x, y) is undefined, and raises ValueError.

313.

What is the utility of Python standard library’s math module and random module ?

Answer»

Math module is used for math related functions that work with all number types except for complex numbers, radom module is used for different random number generator functions.

314.

Explain the term ‘Module’.

Answer»

Module : A module is a file containing Python definitions and statements. Standard library of Python is extended as modules to a programmer. Definitions from the module can be used within the code of a program. To use these modules in the program, a programmer needs to import the module. There are many ways to import a module in program : 

(i) Import 

(ii) from 

(i) Import: It is simplest and most common way to use modules in our code. Its syntax is : 

> > > import module name 1 [, module name 2,………. ] 

Example, 

> > > import math > > > value = math.sqrt (25) 

(ii) From : It is used to get a specific function in the code instead of the complete module file.

If we know before hand which functions, we will be needing, then we may use ‘from’. For modulus having large number of functions, it is recommended to use ‘from’ instead of import. Its syntax is 

> > > from module name import function name 1 [, function name 2, ……….] Example, 

> > > from math import sqrt 

> > > value = math . sqrt (36)

315.

What is raw_input?

Answer»

It is a function which takes a string (e.g., a question) as argument, writes the string to the terminal window, halts the program and lets the user write in text on the keyboard, and then the text is returned to the calling code as a string object.

316.

Determine what is printed by the following program listinput.py, list1 = eval(raw_input(‘Give a list:’)) list2 = eval(raw_input(‘Give another list:’)) print list1 + list2 When the program is running as follows: Unix/DOS &gt; python listinput.py Give a list: ‘[1,2]’ Give another list: “[3,4]”

Answer» The program  correct answer is = [1,2] [3,4]
317.

Explain return keyword.

Answer»

The statement return [expression] exits a function, optionally passing back an expression to the caller.

318.

Define scope of a variable.

Answer»

The scope of a variable determines the portion of the program where you can access a particular identify.

319.

What are two basic scopes of variables in Python?

Answer»

Global variables and Local variables.

320.

What are Required arguments?

Answer»

Required arguments are the arguments passed to a function in correct positional order.

321.

Write the syntax of lambda functions?

Answer»

The syntax of lambda functions contains only a single statement, which is as follows: 

lambda[arg1 [,arg2, argn]] :expression

322.

Write a function that:(i)  Asks the user to input a diameter of circle in inches. (ii)  Sets a variable called radius to one half of that number. (iii)  Calculate the area of circle. (iv)  Print the area of circle with appropriate unit. (v)  Return this same amount as the output of the function.

Answer»

def area_circle (): 

import math 

diameter=float (raw_input (“Enter the diameter in Inches)) 

radius = diameter 12 

area_circle_inches = (math.pi) * (radius * * 2) 

print “The area of circle, area_circle_ inches 

return (area_circle_inches)

323.

How does a user can share global variables across modules ?

Answer»

The canonical way to share information across modules within a single program is to create a special module (called config or cfg). Just import the config module in all modules of application. The module then becomes available as a global name. Because there is only one instance of each module, any change made to the module object get reflected everywhere. For example,

config. py: 

x=0 

mod.py: 

import config 

config.x=1 

main.py : 

import config 

import mod 

print config.x

324.

In analog to the example, write a script that asks users for the temperature in F and prints the temperature in C.

Answer»

# Program to convert temperature from F into C. 

> > > fahrenheit = input (“Entry the temperature in F : “) 

> > > centigrade = (fahrenheit – 32) 8 5/9.0 

> > > print “The equivalent temperature in centigrade is”, centigrade 

> > > print

325.

Predict the output of the following C++ statements: 1. cout&lt;&lt;toupper('a');2. cout&lt;&lt;(char) toupper('a');

Answer»

1. It prints 65 

2. It prints A

326.

What is user – defined functions?

Answer»

C++ also provides the facility to create new functions for specific task as per user requirement. The name of the task and data required (arguments) are decided by the user and hence they are known as User-defined functions.

327.

Write a note on Local Scope.

Answer»

1. A local variable is defined within a block. A block of code begins and ends with curly braces { }.

2. The scope of a local variable is the block in which it is defined.

3. A local variable cannot be accessed from outside the block of its declaration.

4. A local variable is created upon entry into its block and destroyed upon exit.

328.

What is the difference between isupper() and toupper() functions?

Answer»

isupper():

  • This function is used to check the given character is uppercase.
  • This function will return 1 if true otherwise 0.

toupper():

  • This function is used to convert the given character into its uppercase.
  • This function will return the upper case equivalent of the given character. If the given character itself is in upper case, the output will be the same.
329.

What is Built – in functions?

Answer»

C++ provides a rich collection of functions ready to be used for various tasks. The tasks to be performed by each of these are already written, debugged and compiled, their definitions alone are grouped and stored in files called header files. Such ready – to – use sub programs are called pre – defined functions or built – in functions.

330.

Consider the following C++ statements and predict the output. int p=isalpha(‘5’); cout&lt;&lt;p;

Answer»

0. 

isalpha() – To check whether a character is an alphabet or not. If the character is an alphabet it returns a value 1 otherwise it returns 0.

331.

C++ has a built-in function with which we get the result of 42 .1. Identify the name of the function.2. Identify the header le for the above function.

Answer»

1. pow(4, 2); 

2. The header file used is cmath.

332.

In C++ the arguments can be passed to a function in ……. ways.(a) 2(b) 1(c) 3(d) 7

Answer»

In C++ the arguments can be passed to a function in 2 ways.

333.

Define library functions.

Answer»

C++ provides a rich collection of functions ready to be used for various tasks. The tasks to be performed by each of these are already written, debugged and compiled, their definitions alone are grouped and stored in files called header files. Such ready – to – use sub programs are called pre – defined functions or built – in functions or Library Functions.

334.

Inline functions execute faster but requires more …….(a) variables(b) pointers(c) memory(d) functions

Answer»

Inline functions execute faster but requires more memory

335.

Give an example for defining a function.

Answer»

def functionname(parameters): 

“function_docstring” function_suite 

return[expression]

336.

…………. is the name of the function.(a) fact(b) task(c) arguments(d) none of these

Answer»

(d) none of these

337.

The pow() function takes the two arguments of ……….(a) target and source(b) upper and lower(c) base and exponent(d) source and exponent

Answer»

(c) base and exponent

338.

………. is used to check whether the given character is an alphabet or not.(a) isalnum()(b) isalpha()(c) isalph()(d) isal()

Answer»

(b) isalpha()

339.

What are the information the prototype provides to the compiler?

Answer»

The prototype above provides the following information to the compiler:

1. The return value of the function is of type long. 

2. Fact is the name of the function.

3. The function is called with two arguments:

  • The first argument is of int data type. 
  • The second argument is of double data type, int display(int, int) // function prototype//.

The above function prototype provides details about the return data type, name of the function and a list of formal parameters or arguments.

340.

What is default arguments? Give example.

Answer»

In C++, one can assign default values to the formal parameters of a function prototype. 

The Default arguments allows to omit some arguments when calling the function.

1. For any missing arguments, complier uses the values in default arguments for the called function.

2. The default value is given in the form of variable initialization.

Example : void defaultvalue(int n1 = 10, n2 = 100);

3. The default arguments facilitate the function call statement with partial or no arguments.

Example :

1. defaultvalue (x, y);

2. defaultvalue (200, 150);

3. defaultvalue (150);

4. defaultvalue (x, 150);

4. The default values can be included in the function prototype from right to left, i.e., we cannot have a default value for an argument in between the argument list.

Example:

1. void defaultvalue (int n1=10, n2);//invalid prototype.

2. void defaultvalue (int n1, n2 = 10);//valid prototype.

341.

The strcpy() function takes two arguments of …………(a) target and source(b) upper and lower(c) base and exponent(d) none of these

Answer»

(a) target and source

342.

What is isdigit()? Give example.

Answer»

This function is used to check whether a given character is a digit or not. This function will return 1 if the given character is a digit, and 0 otherwise.

Example:

using namespace std;

#include

#include int main( )

{

char ch;

cout <<“\n Enter a Character:”; cin>> ch;

cout << “\n The Return Value of isdigit(ch) is << isdigit(ch);

}

Output – 1

Enter a Character: 3

The Return Value of isdigit(ch) is : 1

Output – 2

Enter a Character: A

The Return Value of isdigit(ch) is :0

343.

What is return statement with example?

Answer»

The return statement stops execution and returns to the calling function. When a return statement is executed, the function is terminated immediately at that point. The return statement is used to return from a function. It is categorized as a jump statement because it terminates the execution of the function and transfer the control to the called statement.

Example:

return(a + b); return(a);

return; // to terminate the function

344.

Explain reload)) Function.

Answer»

When the module is imported into a script, the code in the top-level portion of a module is executed only once.Therefore, if you want to re-execute the top -level code in a module, you can use the reload() function. 

The reload() function imports a previously imported module again. The syntax of the reload)) function is this : 

reload (module_name)

345.

Write a program using pow() and sin() function.

Answer»

The pow() function returns base raised to the power of exponent. The sin() function takes a single argument in radians

#include

#include using namespace std;

int main ()

{

double base, exponent, result;

base = 5;

exponent = 4;

result = pow(base, exponent);

cout << “pow(“ << base << “A” << exponent <<”) = ” << result;

double x = 25;

result = sin(x);

cout << “\nsin(“<< x <<”)= ”<< result;

return 0;

}

Output:

pow (5^4) = 625

sin (25) = – 0.132352

346.

What is divide and conquer?

Answer»

Divide and Conquer

1. Complicated programs can be divided into manageable sub programs called functions.

2. A programmer can focus on developing, debugging and testing individual functions.

3. Many programmers can work on different functions simultaneously.

347.

Find the error def minus (total, decrement) output = total – decrement return output

Answer»

The function’s header has colon missing at the end. So add colon (:) at end of header line. Thus, the correct code is

def minus (total, decrement) 

output = total – decrement 

return output

348.

Which is return data type of the function prototype of add(int, int);?(a) int(b) float(c) char(d) double

Answer»

int is return data type of the function prototype of add(int, int).

349.

Write about strlen() function.

Answer»

The strlen() takes a null terminated byte string source as its argument and returns its length. The length does not include the null(\0) character.

350.

Explain about address method.

Answer»

This method copies the address of the actual argument into the formal parameter. Since the address of the argument is passed, any change made in the formal parameter will be reflected back in the actual parameter.

#include

using namespace std;

void display(int & x) //passing address of a//

{

x = x*x;

cout << “\n\n The Value inside display

function (n1 x n1) :”<< x ;

}

int main()

{

intn 1;

cout << “\n Enter the Value for N1 cin >> n1;

cout << “\n The Value of N1 is inside main function Before passing:” << n1;

display(n1);

cout << “\n The Value of N1 is inside main function After passing (n1 x n1):”<< n1; retum(O);

}

Output:

Enter the Value for N1 : 45

The Value of N1 is inside main function Before passing : 45

The Value inside display function (n1 x n1) : 2025 The Value of Nl is inside main function After passing (n1 x n1): 2025