Namma Kalvi 12th Computer Science Chapter 1 To 4 Notes em
Namma Kalvi 12th Computer Science Chapter 1 To 4 Notes em
SCHOOL - 601206
Namma Kalvi
UNIT
CHAPTER 1 FUNCTION
I
www.nammakalvi.in
1.What is a subroutine? 6. Which of the following is a normal function
Subroutines are small sections of code. definition and which is recursive function definition
Used to perform a particular task that can be used i) let rec sum x y:
repeatedly. return x + y
In Programming languages subroutines are called as ii) let disp :
Functions print ‘welcome’
2. Define function with respect to programming iii) let rec sum num:
language. if (num!=0) then return num + sum (num-1)
In Programming languages subroutines are called as else
Functions return num
A function contains a set of code that works on i. Recursive function
many kinds of inputs, like variants, expressions and ii. Normal function
produces a concrete output. iii. Recursive function
3. Write the inference you get from X:=(78). 7.Mention the characteristics of Interface
X:= (78) has an expression in it but (78) is not The class template specifies the interfaces to enable
itself an expression. an object to be created and operated properly.
(78) is a function definition. An object's attributes and behaviour is controlled by
Definitions bind values to names, sending functions to the object.
Here ,the value 78 being bound to the name ‘X’.
Definitions are not expressions, Definitions can 8. Why strlen is called pure function?
have expressions nested inside them. Strlen() is a pure function
4. Differentiate between interface and because the function takes one variable as a
implementation. parameter, and accesses it to find its length.
Explain with an example interface and implementation This function reads external memory but does
Interface not change it,
An interface is a set of action that an object can do 9. What is the side effect of impure function. Give
In object oriented programs classes are the example.
interface
Implementation The variables used inside the function may cause
Implementation carries out the instructions defined side effects though the functions which are not
in the interface passed with any arguments.
How the object is processed and executed is the When a function depends on variables or
implementation. functions outside of its definition block,
Ex. For example
let min 3 x y z := let Random number
if x < y then let a := random()
if x < z then x else z if a > 10 then
else if y < z then y else z return: a
In the above example , else
Implementation of a function that finds the return: 10
minimum of its three arguments: Here the function Random is impure as it is not sure
5.What is recursive function? what will be the result when we call the function.
A function definition which call itself is called
recursive function
www.nammakalvi.in
10. Differentiate pure and impure function. (ii) Parameter with Type
Here , data types are mentioned with parameter in
Pure Function Impure Function function definition.
Pure functions are Impure functions are Ex . let rec pow (a: int) (b: int) : int :=
functions which will functions which will not if b=0 then 1
give exact result when give exact result when else a * pow b (a-1)
the same arguments the same arguments
are called. Ex. sin(0) are called Ex. date()
Explicitly annotating the types can help with debugging
The return value The return value does such an error message.
depends on its not depend on its 13.Explain with example Pure and impure functions
arguments passed. arguments passed.
Pure Function
same return values. different return values Pure functions are functions which will give exact
They do not have They have side effects. result when the same arguments are called. Ex.
any side effects. sin(0)
They do not modify They may modify The return value depends on its arguments passed.
the arguments which the arguments which
same return values.
are passed to them are passed to them
They do not have any side effects.
They do not modify the arguments which are
11. What happens if you modify a variable outside the passed to them
function? Give an example. Example
One of the most popular groups of side effects is Strlen() is a pure function
modifying the variable outside of function.
because the function takes one variable as a
For example
parameter, and accesses it to find its length.
let y: = 0
(int) inc (int) x This function reads external memory but does
y: = y + x; not change it,
return (y) Impure Function
In the above example the value of y get changed Impure functions are functions which will not give
inside the function definition. exact result when the same arguments are called
Due to which the result will change each time. Ex. date()
The side effect of the inc () function is to changing The return value does not depend on its
the data of the external visible variable ‘y’. arguments passed.
Different return values
12.What are called Parameters and write a note on They have side effects.
(i) Parameter without Type (ii) Parameter with Type They may modify the arguments which are passed
Define Parameters (and) arguments. to them
Parameters are the variables in a function 14. Identify in the following program
definition. let rec gcd a b :=
Arguments are the values which are passed to a if b <> 0 then gcd b (a mod b)
function definition. else return a
(i) Parameter without Type i) Name of the function Ans: gcd
Here , data types are not mentioned with parameter in ii) Identify the statement which tells it is a recursive
function definition. function Ans : let rec gcd
Ex. let rec pow a b:= iii) Name of the argument variable Ans: a , b
if b=0 then 1 iv) Statement which invoke the function recursively
else a * pow a (b-1) Ans: gcd b (a mod b)
Some language compiler solves this type (data type) v) Statement which terminates the recursion
problem algorithmically, but some require the type to Ans: return a
be mentioned.
www.nammakalvi.in
Namma Kalvi
UNIT
CHAPTER 2 DATA ABSTRACTION
I
www.nammakalvi.in
1.Define abstraction. 6. What is a List? Give an example.
The process of providing only the essentials and List is constructed by placing expressions within
hiding the details is known as abstraction. square brackets separated by commas.
2. What is abstract data type? In a list elements can be changed.
The process of providing only the essentials and List can store multiple values.
hiding the details is known as abstraction. Ex. lis[10,12]
Abstract Data type (ADT) is a type for objects
whose behavior is defined by a set of value and a 7.Differentiate Concrete data type and abstract
set of operations. datatype.
The definition of ADT only mentions what Concrete data type Abstract data type.
operations are to be performed but not how these
The data representation The data representation is
operations will be implemented.
is known unknown
3. Differentiate constructors and selectors.
Concrete data types or Abstract Data Types
Constructors
structures (CDT's) are (ADT's) offer a high level
Constructors are functions that build the abstract
direct implementations view (and use) of a
data type.
of a relatively simple concept independent of
Constructors create an object, bundling together concept. its implementation
different pieces of information,
Ex. city = makecity (name, lat, lon) ---- Constructors
Selectors 8. Which strategy is used for program designing?
Selectors are functions that retrieve information Define that Strategy.
from the data type. A powerful strategy for designing programs: 'wishful
selectors extract individual pieces of information thinking'.
from the object. Wishful Thinking is the formation of beliefs and
getname(city) making decisions according to what might be
getlat(city) -------- Selectors
pleasing to imagine instead of by appealing to
getlon(city) reality.
9. Identify Which of the following are constructors and
4. What is a Pair? Give an example. selectors? (a) N1=number() (b) accetnum(n1)
Any way of bundling two values together into one (c) displaynum(n1) (d) eval(a/b)
can be considered as a pair. (e) x,y= makeslope (m), makeslope(n) (f) display()
Pair is a compound structure which is made up of a) Constructors
list or Tuple b) Selector
Therefore List can be called as Pairs. c) Selector
Ex. d) Constructors
lst[(0, 10), (1, 20)] e) Constructors
here ,(1, 20) (0, 10) - are pairs f) Selector
10. What are the different ways to access the elements 12.Write an ADT for rational numbers
of a list. Give example. - constructor
List is constructed by placing expressions within - - constructs a rational number with numerator n,
square brackets separated by commas. denominator d
In a list elements can be changed. rational(n, d)
List can store multiple values. - - selector
The elements of a list can be accessed in two ways. numer(x) → returns the numerator of rational number x
1. Multiple assignment denom(y) → returns the denominator of rational
lst := [10, 20] number y.
x, y := lst ; here x will become 10 and y is 20 13.How to Representation of Tuple as a Pair
2. Element selection operator Example 1
Accessing the elements in a list is by the element lst[(0, 10), (1, 20)] - where
selection operator, also expressed using square
brackets
Ex.
lst[0]
10 Example 2
nums := (1, 2)
lst[1]
nums[0]
20 1
Method III nums[1]
Represent list similar to a set. 2
1.) lst[(0, 10), (1, 20)] - where 14.How will you facilitate data abstraction. Explain it
with suitable example
The process of providing only the essentials and
hiding the details is known as abstraction.
To facilitate data abstraction, you will need to create
Any way of bundling two values together into two types of functions: constructors and selectors.
one can be considered as a pair. Constructors
Lists are a common method to do so. Constructors are functions that build the abstract
Therefore List can be called as Pairs. data type.
Constructors create an object, bundling together
11. Identify Which of the following are List, Tuple and different pieces of information,
class ? For example,
(a) arr [1, 2, 34] (b) arr (1, 2, 34) you have an abstract data type called city.
(c) student [rno, name, mark] This city object will hold the city’s name, and its
(d) day= (‘sun’, ‘mon’, ‘tue’, ‘wed’) latitude and longitude.
(e) x= [2, 5, 6.5, [5, 6], 8.2] To create a city object, you’d use a function like
(f) employee [eno, ename, esal, eaddress] city = makecity (name, lat, lon) ----- -- Constructors
Selectors
Selectors are functions that retrieve information
a) List b) Tuple c) Class d) Tuple e) List f) Class
from the data type.
selectors extract individual pieces of information
from the object.
To extract the information of a city object, you would
use functions like
getname(city)
getlat(city) ------- Selectors
getlon(city)
Namma Kalvi
UNIT CHAPTER 3 SCOPING
I www.nammakalvi.in
Local(L) scope
1.What is a scope? Local scope refers to variables defined in current
Scope refers to the visibility of variables, function.
parameters and functions in one part of a Always, a function will first look up for a variable
program to another part of the same program. name in its local scope.
2.Why scope should be used for variable.State the Only if it does not find it there, the outer scopes are
reason. checked.
Normally, every variable defined in a program For example
has global scope.
Once defined, every part of your program can
access that variable.
3.What is Mapping?
The process of binding a variable name with an
In output, the variable a displays the value 7, because it
object is called mapping.
= (equal to sign) is used in programming languages is defined and available in the local scope
to map the variable and object. Global(G) Scope
4.What is meant by namespaces? A variable which is declared outside of all the
Namespaces are containers for mapping names functions in a program is known as global variable.
of variables to objects. This means, global variable can be accessed inside
Programming languages keeps track of all or outside of all the functions in a program.
mappings with namespaces. For example
5.What is the life time of variable?
The duration for which a variable is alive is called its
‘life time’.
6.Define LEGB rule
The LEGB rule is used to decide the order in which In output, the variable a displays the value 7 for the
the scopes are to be searched for scope resolution. function call Disp() and then it displays 10, because a is
The scopes are listed below in terms of hierarchy defined in global scope.
(highest to lowest). Enclosed(E) scope
A variable which is declared inside a function,
Local(L) Defined inside function/class which contains another function definition with in
it, the inner function can also access the variable of
the outer function.
Enclosed(E) Defined inside enclosing functions
(Nested function concept) This scope is called enclosed scope.
When a compiler or interpreter search for a variable in
a program,
Global(G) Defined at the uppermost level
It first search Local, and then search Enclosing
scopes. Consider the following example
Built-in (B) Reserved names in built-in
functions (modules)
The variable ‘a’ defined in Disp() can be even used The code is stored across multiple files.
by Disp1() because it is also a member of Disp(). Code is short, simple and easy to understand.
Built-in(B) Scope Errors can easily be identified, as they are localized
The built-in scope has all the names that are pre- to a subroutine or function.
loaded into the program scope when we start the The same code can be used in many applications.
compiler or interpreter. The scoping of variables can easily be controlled.
Any variable or module which is defined in the
library functions of a programming language has 12.What is Access control? Why access control is
Built-in or module scope. required?
They are loaded as soon as the library files are Access control is a security technique that regulates
imported to the program. who or what can view or use resources in a
computing environment.
It is a fundamental concept in security that
minimizes risk to the object.
Namma Kalvi
UNIT CHAPTER 4 ALGORITHMIC STRATEGIES
I ALGORITHMIC STRATEGIES
www.nammakalvi.in
18.Explain the Binary search techniques in algorithm mid = low + (high - low) / 2
for searching. =5+(9 – 5)/2 = 7 new mid is 7
The element stored at 7 is not match with search
2.Binary Search element
Binary search also called half-interval search Current mid value is 7
algorithm. Now search element is less than the middle element.
It finds the position of a search element within a So take left element
sorted array. Now mid value is 6
The binary search algorithm can be done as
divide-and-conquer search algorithm and
executes in logarithmic time.
Pseudo code for Binary search
Calculate mid again
Start with the middle element:
high = mid – 1 = 6 – 1 = 5 current low value = 5
If the search element = middle element of the
mid = low + (high - low)/2 = 5 +(5 – 5) /2 = 5
array , then return the index of the middle
new mid is 5
element.
Now we compare the value stored at location 5 with
If not, then compare
our search element. We found that it is a match.
if search element > middle element , if TRUE
19.Explain the Bubble sort algorithm with example.
select the elements to the right side of the
The algorithm starts at the beginning of the list of
middle index, and go to Step-1.
values stored in an array.
Otherwise
It compares each pair of adjacent elements and
select the elements to the left side of the
swaps them if they are in the unsorted order.
middle index, and start with Step-1.
This comparison and passed to be continued until
When a match is found, display FOUND message.
no swaps are needed, which indicates that the list
If no match is found for all comparisons, then of values in an array is sorted.
display NOT FOUND message. Pseudo code
Binary Search Working principles Start with the first element i.e., index = 0,
compare the current element with the next
List of elements in an array must be sorted first .
element of the array.
For example,
If the current element is greater than the next
Consider the following array of elements;
element of the array, swap them.
If the current element is less than the next
element, move to the next element.
Search element is 60 Go to Step 1 and repeat until end of the index is
First, find index of middle element by using formula: reached.
mid = low + (high - low) / 2 For example, Let's consider an array with values
mid =0+(9 – 0) / 2 = 4 {15, 11, 16, 12, 14, 13}
At the end of all the iterations we will get the sorted 21.Explain the Insertion sort algorithm with example.
values in an array as given below: It works by taking elements from the list one by
one and inserting then in their correct position in
to a new sorted list.
Pseudo code
20.Explain the Selection sort algorithm with example. If it is the first element, it is already sorted.
Pick next element
This algorithm will first find the smallest elements Compare with all elements in the sorted sub-list
in array and swap it with the element in the first Shift all the elements in the sorted sub-list that is
position of an array, greater than the value to be sorted
then it will find the second smallest element and Insert the value
swap that element with the element in the second Repeat until list is sorted
position,
and it will continue until the entire array is sorted
in respective order.
Pseudo code
24. ____ is measured by the maximum memory space 38. ____ search is a sequential method for finding a
required by the algorithm. particular value in a list.
a. Time b. Space c. Both A and B d. None of these a. Linear b. Sequential c. Binary d. Either A or B
25. Identify the correct statement from the following: 39. In ____ searching algorithm, list need not be
a. The efficiency of an algorithm is defined as the ordered.
number of computational resources used by the a. Linear b. Sequential c. Binary d. Either A or B
algorithm. 40. ____ search also called half-interval search
b. A variable part is defined as the total space required algorithm.
by variables, which sizes depends on the problem and a. Linear b. Sequential c. Binary d. Either A or B
its iteration. 41. The ____ search algorithm can be done as divide-
c. A fixed part is defined as the total space and-conquer search algorithm.
required to store certain data and variables for an a. Linear b. Sequential c. Binary d. None of these
algorithm. d. All the above 42. The ____ search algorithm executes in logarithmic
26. The execution time of an algorithm depends on time.
factor. a. Speed of the machine b. Compiler and other a. Binary b. Sequential c. Linear d. All of these
system Software tools c. Operating System d. All the 43. List of elements in an array must be sorted first for
above ____ search.
27. The execution time of an algorithm depends on a. Linear b. Sequential c. Binary d. Unary
factor. a. Programming language used b. Volume of 44. ____ sort is a simple sorting algorithm.
data required c. Speed of the machine d. All a. Bubble b. Merge c. Insertion d. Selection
28. ____ trade off refers to a situation where we can 45. ____ sort algorithm is too slow and less efficient
reduce the use of memory at the cost of slower when compared other sorting methods.
program execution a. Bubble b. Merge c. Insertion d. Selection
a. Time b. Space c. Cost d. None of these 46. ____ sort algorithm works by taking elements
29. ____ trade off refers to a situation where we can from the list one by one and inserting then in their
reduce the running time at the cost of increased correct position in to a new sorted list.
memory usage. a. Bubble b. Merge c. Insertion d. Selection
a. Time b. Space c. Cost d. None of these 47. ____ algorithm uses n-1 number of passes to get
30. The best algorithm to solve a given problem is one the final sorted list.
that ____ a. requires less space in memory b. takes a. Bubble b. Merge c. Insertion d. Selection
less time to execute its instructions 48. Dynamic programming approach is similar to ____
c. Both A and B d. None of these a. Divide and Conquer b. Integration
31. How many asymptotic notations are mostly used to c. Merging d. None of these
represent time complexity of algorithms? 49. ____ programming is used whenever problems
a. Five b. Four c. Three d. Two can be divided into similar sub-problems.
32. ____ is an asymptotic notation used to represent a. Static b. Dynamic c. Concrete d. None of these
time complexity of algorithm. 50. Dynamic algorithms uses ____ technique.
a. Big O b. Big Ω c. Big Θ d. All the above a. Memorization b. Memorize c. Memory d. None
33. ____ is often used to describe the worst-case of an 51. ____ sort algorithm compares each pair of adjacent
algorithm. elements and swaps them if they are in the unsorted
a. Big O b. Big Ω c. Big Θ d. All the above order. a. Bubble b. Merge c. Insertion d. Selection
34. ____ is used to describe the best-case of an
algorithm.
a. Big O b. Big Ω c. Big Θ d. All the above
35. ____ is used to describe lower bound of an
asymptotic function. www.nammakalvi.in
a. Big O b. Big Ω c. Big Θ d. All the above
36. ____ is used to describe upper bound of an
asymptotic function.
a. Big O b. Big Ω c. Big Θ d. All the above
37. Linear search also called ____ search.
a. Binary b. Sequential c. Random d. quick