VIVA QUESTIONS-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

VIVA QUESTIONS

9- What are the two modes to work with python.


Ans: (i) Interactive mode (ii)Script mode
10- What is an expression?
Ans: An expression is legal combination of symbols that represents a value.
11- What is string?
Ans: String is a sequence of characters.
12- What is the use of ‘+’ operator in string?
Ans: ‘+’ operator joins or concatenates the strings on both sides of the operator.
13-What is the use of ‘*’ operator in string?
Ans: ‘*’ operator creates a new string concatenating multiple copies of the same string.
14- How many types of strings are supported in Python?
Ans: Python allows two string types:
i) Single line String- String that terminated in a single line.
ii) Multiline String-String storing multiple lines of text
15- What is a tuple?
Ans: A tuple is an immutable sequence of values which can be of any type and are indexed by an integer.
16- Differentiate between list and tuples.
Ans: Tuples cannot be changed unlike lists, tuples use parentheses, whereas list use square brackets
17- What is an argument? Give an example.
Ans: An argument is data passed to a function through function call statement. For example, in the statement
print (math.sqrt(25)) the integer 25 is an argument.
18- Python has certain functions that you can readily use without having to write any special code. What types of
functions are these?
Ans: The pre-defined functions that are always available for use are known as Python’s built-in functions.
Examples of some built-in functions are: input(), type(), int(), eval(), etc.
19- What is Python module? What is its significance?
Ans: A “module” is a chunk of Python code that exists in its own (.py) file and is intended to be used by Python
code outside itself.
Modules allow one to bundle together code in a form in which it can easily be used later. The Modules can be
“imported” in other programs so the functions and other definitions in imported modules become available to
code that imports them.
20- What is the utility of the built-in function help()?
Ans: Python’s built-in function help() is very useful. When it is provided with a program name or a module-name
or a function-name as an argument, it displays the documentation of the argument as help. It also displays the
string within its passed-argument’s definition. For example,
>>>help (math)
21- What is the difference between local variable and global variable?
Local Variable
1- It is a variable which is declared within Function or within a block.
2- It is accessible only within the function /block program.
Global Variable
1- It is a variable which is declared outside all the functions.
2- It is accessible throughout the program in which it is declared.
22- Is return statement optional? Compare and comment on the following two return statements:
Return
return val
The return statement is optional only when the function is void or we can say that when the function does not
return a value. A function that returns a value must have at least one return statement. From the given two return
statements, the statement:
Return
is not returning any value. Rather, it returns the control to caller along with empty value None. And the
statement:
return val
is returning the control to caller along with the value contained in variable val
24- What is the difference between a keyword and a variable?
Ans. Keyword is a special word that has a special meaning and purpose. For example, int, float, for, if, elif, else,
etc., are keywords. Keywords are reserved and are few.
Variable is the user-defined name given to a value. Variables are not fixed/reserved. These are defined by the user
but they can have letters, digits and a symbol underscore. They must begin with either a letter or underscore. For
example, _age, name, result_1, etc., are the variable names in Python.
25- What is a function? How is it useful?
Ans. A function in Python is a named block of statements within a program.
30- What is the difference between break and continue statements?
Ans. Break and continue are the jump statements, and can be used to alter the flow of loop. Break statement
terminates the loop and resumes execution in the statement. Continue statement skips the rest of the code inside
a loop for the current iteration.
36. Is indentation required in python?
Ans: Indentation is necessary for Python. It specifies a block of code. All code within loops, classes, functions, etc
is specified within an indented block. It is usually done using four space characters. If your code is not indented
necessarily, it will not execute accurately and will throw errors as well.
37. What is the difference between Python Arrays and lists?
Ans: Arrays and lists, in Python, have the same way of storing data. But, arrays can hold only a single data type
elements whereas lists can hold any data type elements.
38. What are functions in Python?
Ans: A function is a block of code which is executed only when it is called. To define a Python function, the def
keyword is used.
39. How does break, continue and pass work?
Break: Allows loop termination when some condition is met and the control is transferred to the next statement.
Continue: Allows skipping some part of a loop when some specific condition is met and the control is transferred
to the beginning of the loop
Pass: Used when you need some block of code syntactically, but you want to skip its execution. This is basically a
null operation. Nothing happens when this is executed.
40. How do you write comments in python?
Ans: Comments in Python start with a # character. However, alternatively at times, commenting is done using
docstrings(strings enclosed within triple quotes).
41. What is pickling and unpickling?
Ans: Pickle module accepts any Python object and converts it into a string representation and dumps it into a file
by using dump function, this process is called pickling. While the process of retrieving original Python objects from
the stored string representation is called unpickling.
42. How will you capitalize the first letter of string?
Ans: In Python, the capitalize() method capitalizes the first letter of a string. If the string already consists of a
capital letter at the beginning, then, it returns the original string.
43. How will you convert a string to all lowercase?
Ans: To convert a string to lowercase, lower() function can be used.
44. What is the purpose of ‘is’, ‘not’ and ‘in’ operators?
Ans: Operators are special functions. They take one or more values and produce a corresponding result.
is: returns true when 2 operands are true (Example: “a” is ‘a’)
not: returns the inverse of the boolean value
in: checks if some element is present in some sequence
45. What is a dictionary in Python?
Ans: The built-in datatypes in Python is called dictionary. It defines one-to-one relationship between keys and
values. Dictionaries contain pair of keys and their corresponding values. Dictionaries are indexed by keys.
46. What are Python packages?
Ans: Python packages are namespaces containing multiple modules.
47. How can files be deleted in Python?
Ans: To delete a file in Python, you need to import the OS Module. After that, you need to use the os.remove()
function.
48. What are the built-in types of python?
Ans: Built-in types in Python are as follows –
Integers
Floating-point
Complex numbers
Strings
Boolean

49. How to add values to a python array?


Ans: Elements can be added to an array using the append(), extend() and the insert (i,x) functions.
50. How to remove values to a python array?
Ans: Array elements can be removed using pop() or remove() method. The difference between these two
functions is that the former returns the deleted value whereas the latter does not.
51. What are Python libraries? Name a few of them.
Ans:Python libraries are a collection of Python packages. Some of the majorly used python libraries are – Numpy,
Pandas, Matplotlib, Scikit-learn and many more.
52. What is split used for?
Ans:The split() method is used to separate a given string in Python.
53. What is the maximum possible length of an identifier?
Ans: Identifiers can be of any length.
54. What are the common built-in data types in Python?
Ans: The common built in data types in python are-
Numbers– They include integers, floating point numbers, and complex numbers. eg. 1, 7.9,3+4i
List– An ordered sequence of items is called a list. The elements of a list may belong to different data types. Eg.
*5,’market’,2.4+
Tuple– It is also an ordered sequence of elements. Unlike lists , tuples are immutable, which means they can’t be
changed. Eg. (3,’tool’,1)
String– A sequence of characters is called a string. They are declared within single or double quotes. Eg. “Sana”,
‘She is going to the market’, etc.
Set– Sets are a collection of unique items that are not in order. Eg. {7,6,8}
Dictionary– A dictionary stores values in key and value pairs where each value can be accessed through its key.
The order of items is not important. Eg. ,1:’apple’,2:’mango-
Boolean– There are 2 boolean values- True and False.
55. What is type conversion in Python?
Ans: Type conversion refers to the conversion of one data type iinto another.
int() – converts any data type into integer type
float() – converts any data type into float type
ord() – converts characters into integer
hex() – converts integers to hexadecimal
oct() – converts integer to octal
tuple() – This function is used to convert to a tuple.
set() – This function returns the type after converting to set.
list() – This function is used to convert any data type to a list type.
dict() – This function is used to convert a tuple of order (key,value) into a dictionary.
str() – Used to convert integer into a string.
complex(real,imag) – This function converts real numbers to complex(real,imag) number.
56. What is slicing in Python?
Ans: Slicing is used to access parts of sequences like lists, tuples, and strings. The syntax of slicing is-
[start:end:step]. The step can be omitted as well. When we write [start:end] this returns all the elements of the
sequence from the start (inclusive) till the end-1 element. If the start or end element is negative i, it means the ith
element from the end. The step indicates the jump or how many elements have to be skipped. Eg. if there is a list-
[1,2,3,4,5,6,7,8]. Then [-1:2:2] will return elements starting from the last element till the third element by printing
every second element.i.e. [8,6,4].
57. What are Literals in Python and explain about different Literals
Ans: A literal in python source code represents a fixed value for primitive data types. There are 5 types of literals
in python-
i) String literals– A string literal is created by assigning some text enclosed in single or double quotes to a variable.
To create multiline literals, assign the multiline text enclosed in triple quotes. Eg.name=”Tanya”
ii) A character literal– It is created by assigning a single character enclosed in double quotes. Eg. a=’t’
iii) Numeric literals– They include numeric values that can be either integer, floating point value, or a complex
number. Eg. a=50
iv) Boolean literals– These can be 2 values- either True or False.

58. What are negative indexes and why are they used?
Ans: The sequences in Python are indexed and it consists of the positive as well as negative numbers. The
numbers that are positive uses ‘0’ that is uses as first index and ‘1’ as the second index and the process goes on
like that.
The index for the negative number starts from ‘-1’ that represents the last index in the sequence and ‘-2’ as the
penultimate index and the sequence carries forward like the positive number.
The negative index is used to remove any new-line spaces from the string and allow the string to except the last
character that is given as S[:-1]. The negative index is also used to show the index to represent the string in correct
order.
35. Is python case sensitive?
Ans: Yes. Python is a case sensitive language.

What are the different modes of opening a file?


The different modes of opening a file are as follows:
r,w,a,r+,w+,a+,rb,wb,ab,rb+,wb+,ab+

What is the difference between “w” and “a” mode?


“a” mode adds the content to the existing file whereas “w” mode overwrites the contents into the file.
What is the difference between readline() and readlines() function?
readline() function reads the content of the text file and returns the content into the string whereas readlines()
function reads the content of the text file and returns the content into the list.

Are CSV files and Text Files same?


CSV files and Text Files are same in storage but csv file stores the data separated by a delimiter.
What are the different valid delimiters?
, is default delimiter
other delimiters are tab – \t, colon – :, or semi colon – ;

What is pickling?
Pickling refers to the process of converting python object hierarchy into a byte stream to write into a binary file.
What is unpickling?
It is the process of converting the byte stream back into an object hierarchy.
Which module is required to handle binary files?
pickle
Name the functions used to read and write data into binary files.
pickle.dump(list_object, file_handle)
pickle.load(file_object)

What are the significance of tell() and seek() functions?


tell() function returns the current file position in a file
seek() function change the current file position

What are the operations can be performed on the stack?


Push
Pop
Peep or Peek

Which principle is followed by stack?


LIFO (Last in First Out)

You might also like