Wiley Acing The GATE Computer Science and Information Technology PDF
Wiley Acing The GATE Computer Science and Information Technology PDF
Gaurav Sharma
Assistant Professor
School of Computing Science and Engineering
Galgotias University, Greater Noida
Kuldeep Singh
Department of Computer Science and Engineering
Thapar University, Patiala
WILEY ACING THE GATE
COMPUTER SCIENCE AND INFORMATION TECHNOLOGY
Copyright © 2015 by Wiley India Pvt. Ltd., 4435-36/7, Ansari Road, Daryaganj, New Delhi-110002.
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in
any form or by any means, electronic, mechanical, photocopying, recording or scanning without the written
permission of the publisher.
Limits of Liability: While the publisher and the author have used their best efforts in preparing this book,
Wiley and the author make no representation or warranties with respect to the accuracy or completeness of
the contents of this book, and specifically disclaim any implied warranties of merchantability or fitness for
any particular purpose. There are no warranties which extend beyond the descriptions contained in this
paragraph. No warranty may be created or extended by sales representatives or written sales materials.
Disclaimer: The contents of this book have been checked for accuracy. Since deviations cannot be
precluded entirely, Wiley or its author cannot guarantee full agreement. As the book is intended for
educational purpose, Wiley or its author shall not be responsible for any errors, omissions or damages
arising out of the use of the information contained in the book. This publication is designed to provide
accurate and authoritative information with regard to the subject matter covered. It is sold on the
understanding that the Publisher is not engaged in rendering professional services.
The objective of writing this book is to help the GATE aspirants in understanding the various subjects of Computer
Science and Information Technology. Being a national level exam, it measures the candidate’s ability on different
parameters, namely, basic fundamentals, concepts, interpretation and analytical ability. This examination assumes
more importance as the GATE score has been used by public sector undertakings (PSUs) for recruiting graduate-level
engineers at the entry level, JRF at CSIR Labs, etc. Even certain foreign universities (like Singapore, Germany) have
also started recognizing the GATE score, for using it as a benchmark for providing scholarships for students interested
in pursuing PG and PhD programs.
The major strength of this book lies in the demystifying complex concepts by unpeeling levels of abstraction down
to the basic level. The book follows a balanced approach on systematic representation of the text. Each chapter begins
with the basics and then in-depth concepts are described with the help of various example(s)/numerical(s) and sche-
matic diagrams. Different ways of interpretation are also highlighted while providing the solutions to previous year
GATE question papers and then the chapter is culminated with a summary of important terms/formulas used in that
chapter. Therefore, the book is self-contained.
The organization of this book reflects our belief that significant amount of practice is required for achieving the
c omplete mastery. Therefore, each chapter contains a set of multiple questions of different levels (approximately
100 questions in each chapter) for self-assessment/mock test preparation, designed by keeping in mind the latest
pattern.
While writing this book, we have drawn upon the long experience of teaching the subjects as well as our own under-
standing of the subjects. The contents have been carefully compiled and have been presented in a lucid manner to
the reader. Utmost care has also been taken to relieve the candidate from the unnecessary burden of recollecting the
concepts during the preparation, so that the student qualifies with a high GATE score.
We sincerely hope that students looking for an up-to-date, excellent self-study, compact and comprehensive book
will be more than happy to have this book.
It is innate that some error(s) might have crept in, we shall be thankful to the reader for highlighting the same as
well as the suggestions in the improvement of the text presented.
ACKNOWLEDGEMENTS
Writing a book, is like a major project and is rarely the work of a single individual.
We thank our teachers and seniors for providing the wealth of knowledge, sharing the depth and breadth of their
experience and answering a barrage of our questions. Our numerous students, whose valuable discussions are a key
constituent of this book.
During the compilation and writing of this work, we have been fortunate enough to receive useful suggestions, timely
assistance and humble support from our colleagues and friends.
Special thanks to our family members who were extremely generous with their time being consumed for writing this
book and tolerated us when we were trying tooth-and-nail to meet the deadlines.
We are grateful to the Wiley India, particularly, Siddhartha Deb, Publishing Editor, for initiating all of us into this
venture and Sruthi Guru, Developmental Editor, for the continuous cooperation and assistance for the timely feedback.
Without their conscientious effort and personal involvement, this piece of work would not have been possible. We
would also like to thank the production team, Rakesh Poddar and Samir Das, for their efforts.
We wish to acknowledge the efforts of our reviewer(s) for providing countless suggestions for improving the content
and presentation.
Thank you!
Anil Kumar Verma ([email protected])
Gaurav Sharma ([email protected])
Kuldeep Singh ([email protected])
Anil Kumar Verma is currently working as an Associate Professor in the Department of CSE,
Thapar University, Patiala. He is a member of MISCI (Turkey), LMCSI (Mumbai), GMAIMA
(New Delhi) and certified software quality auditor by MoCIT, Govt. of India. He received his
B.S., M.S. and PhD, majoring in Computer Science and Engineering. He has a rich teaching expe-
rience of over 23 years, which include the post of Lecturer at M.M.M. Engineering College (now,
MMM University of Technology), Gorakhpur from 1991 onwards, apart from being a visiting
faculty to many institutions. He has visited USA, South Korea and Japan for academic purposes,
and published over 150 papers in refereed journals and conferences (India and abroad). He is
member of various program committees for different international/national conferences and is on the review board of
various international journals. His research interests include wireless networks, routing algorithms and securing Ad hoc
networks. He can be reached at [email protected].
Kuldeep Singh is currently a PhD research scholar and freelance consultant at Thapar
University, Patiala, working in the area of Ad hoc Network Security. He has done his M.E. (IS)
from Thapar University. Prior to that he had done his MCA and qualified GATE and CSIR-
UGC NET. He has commendable and growing list of publications, and is a member of reputed
computer societies like ACM. He can be reached at [email protected].
ABOUT GATE
Purpose of Examination
The Graduate Aptitude Test in Engineering (GATE) essentially examines the aptitude of an engineering graduate.
It differs from other examinations as it calls for an aptitude based learning and approach on engineering topics. Being
a national level exam, GATE measures the candidate’s ability on different parameters, namely, basic fundamentals,
concepts, interpretation and analytical ability. This examination assumes more importance as the GATE score has
been used by public sector undertakings (PSUs) for recruiting graduate-level engineers at the entry level, JRF at CSIR
Labs, etc. Even certain foreign universities (like Singapore, Germany) have also started recognizing the GATE score,
for using it as a benchmark for providing scholarships for students interested in pursuing PG and PhD programs.
1. Financial assistance: A GATE qualified student pursuing PG is liable to receive a stipend of H12,000/- pm
for 24 months from MHRD, India and other Government agencies.
2. Jobs in Public Sector Undertakings: Major PSUs such as – Indian Oil Corporation Ltd. (IOC), Power Grid
Corporation of India Ltd.(PGCIL), Oil and Natural Gas Corporation(ONGC), Punjab State Power Corporation
Ltd. (PSPCL), Bharat Broadband Network Limited (BBNL), etc. are recruting candidates based on their GATE
score.
3. Foreign Universities: National University, Singapore,; Nanyang University, Singapore; and some universities
in Germany do consider the GATE score.
1. Eligibility: The candidate fulfilling the following eligibility criteria can apply for the examination:
Bachelor’s degree in Engineering/Technology/Architecture/ Pharmacy (Post-Diploma/Post B.Sc./4 years after 10+2)
OR
Master’s degree in any branch of Science/ Mathematics/ Statistics/ Computer Applications or equivalent
Note: Students currently in the final year of the above courses can also apply.
2. Application Procedure: GATE Online Application Processing System (GOAPS) Website opens for enrol-
ment, application filling, application submission starts in month of September. For details, refer to the website of
the current organizing institute.
3. Examination Schedule: GATE is conducted in online (Computer Based Test (CBT), mode only and is
spread over two weeks. It starts in the last week of January to first week of February. The GATE (CS/IT) paper
is held in three sessions.
4. Announcement of Results: The results are announced on the GATE Online Application Processing System
(GOAPS) Website managed by IITs/IISc in the month of March.
Structure of Examination
The question paper comprises 65 questions carrying 100 marks to be attempted in 3 hours. The paper is distributed
over General Ability, Engineering Mathematics and Technical Subject.
1. General Ability accounts for 15% of the question paper containing 10 questions carrying 15 marks (5 questions
of 1 mark each and 5 questions of 2 marks each).
2. Engineering Mathematics accounts for 15% of the question paper.
3. Computer Science and Information Technology (CS & IT) accounts for 70% of the question paper.
The emphasis is on the following type of questions:
1. Recall: Based on facts, principles or laws involved. So, the candidate has to answer them from his/her memory
or simple computation.
2. Comprehension: Based on candidate’s understanding of basic concepts that help him to draw conclusions.
3. Application: Based on the ability of the candidate to apply his/her knowledge through computation or logical
reasoning.
4. Analysis and Synthesis: Based upon diagrams/data/tables/images that help in analysis. It also involves sepa-
rating useful information from the irrelevant information.
The types of questions asked are Multiple Choice Questions in which one correct option out of the four given
options needs to be chosen; and Numerical Answer Questions in which the correct answer needs to be keyed in
using an online keyboard.
The marking scheme is as follows:
1. Multiple Choice Questions: These questions carry 1 or 2 marks. There is negative marking for every
incorrect answer. For every wrong answer, 1/3 is deducted for a 1-mark question and 2/3 marks are deducted for
a 2-mark question.
2. Numerical Answer Questions: These questions carry 1 or 2 marks. No choices are given for these questions.
There is no negative marking for these questions.
From 2014 onwards, GATE is conducted mandatorily in online mode for all subject papers. Prior to this, it was
conducted online for 4 papers in 2011 and 6 papers in 2012 and 2013.
In the Online Computer Based Test (CBT) mode, the candidate is required to select the correct option for MCQ
type and enter the answer (numerical value) for numerical answer type question using a mouse on a virtual keyboard
(keyboard of the computer will be disabled). Blank paper pads for rough work are provided for rough work, and these
have to be returned back after the examination with roll numbers mentioned in them.
At the end of the 3-hour window, the computer will automatically close the screen from further actions. Another
screen with submit button will appear which needs to be clicked to end the examination.
The exhaustive syllabi could be overwhelming as it covers the entire course studied over a period of 6-7 semesters.
Thus, the emphasis must be on “knowing the material”, and not just being familiar with the material.
1. A systematic organization of the topics can be understood by going through the examination syllabus of the
subject and aligning it with the course studied.
2. Practicing questions from topics having more weightage to the ones having lower weightage helps understand the
fundamentals of concepts and their application.
3. Familiarize with the use of proper syntactical expressions, assumptions, laws, theorems, standard symbols and
notations for solving various problems based on programming codes and logic.
A well-chartered study plan taking into account the focus areas and learning objectives based on analysis of previous
year papers is important to build a connect between knowing the subject and its quantitative applications. The most
relevant topics for each unit and unit-wise weightage is provided in the part opener preceding each chapter opener.
These can be helpful in deriving focus areas.
1. The study plan may include a schedule of chapter or sub-topic level study with equal focus on revising fundamen-
tals and practicing sufficient questions based on each topic.
2. The focus areas can be evaluated based on individual assessment into strong and weak areas. The time devoted
to revise fundamentals in weak areas should be compensated with the time spent in solving questions in strong
focus areas.
3. The next step would be to spend sufficient time in practicing questions in weak areas as well as assess the
progress made.
4. After having enough confidence on both strong and weak areas, mock tests covering all the topics can be
attempted. Alternately, one-fourth of MCQs from each topic can be attempted.
The examination tests the ability to analyze information provided, make judgments, apply the understanding of topic
to arrive at the best-suited answer.
1. A quick revision of all the important formulas and unit summary would be useful before attempting practice tests
or mock tests.
2. Through the solved examples, the most effective way to solve a problem and the possible mistakes that led to the
wrong options may be identified.
3. Attempt as many previous years’ GATE questions based on the topic and follow the above approach for
self-evaluation.
4. Practice more and more questions based on the problem areas and rectify mistakes committed in the earlier
practice rounds.
Towards the end of study plan, it is important to focus on exam preparedness and time management skills.
1. It is important to enhance problem-solving skills by continuously practicing questions. While doing so, it is
important to give equal importance to both 1-mark and 2-mark type questions. Although the time required for
solving 1-mark questions might be a little less than the 2-mark questions. It is important to practice both types
to enhance the speed and proficiency to attempt all questions types.
2. The time taken to solved a complete paper replicating the examination paper, that is, a mock test or practice test
can be evaluated, and the number of incorrectly answered questions can be identified.
3. The above exercise is helpful in determining the questions which took longer time to solve, and those for which
preparation was lacking in some form. More and more of these questions can be practiced upon to increase
efficiency and accuracy for solving.
As the examination is conducted in online (computer based test) mode, it is helpful to familiarize oneself with the
examination procedure and instructions by going through the screen layout, button functions, etc., by accessing the
mock test available on GAOPS website.
1. It is important to take proper rest and sleep well in the night before the examination, to avoid having a cluttered
memory on the day of the examination.
2. Read the instructions carefully within the allotted time, and raise any equipment/software concerns immediately
to the invigilator.
3. Once the examination begins, be calm and adopt a positive attitude. Read the question carefully and then the
options. Carefully note the negatives in the question, such as “Which of the statements in incorrect?”, etc., and
work accordingly.
4. Set different time limits for the different sections, giving the maximum time for the technical section.
5. If any question appears unfamiliar or it is difficult to recollect the topic on which the question is based upon, then
mark it for review and move to the next question. Reserve some time to revisit such questions and try to narrow
down the possible responses and make an educated guess.
6. Constantly monitor the progress against the time remaining, which is displayed on the top right corner of the
screen.
BEST OF LUCK!!!
Preface v
About GATE ix
Attributes for Success in Examination xi
1 Digital Electronics 3
1.1 Introduction 3
1.2 Number System 3
1.2.1 Conversions of Number System 4
1.2.2 Complement of a Number 4
1.2.3 Representation of Negative Numbers 5
1.2.4 The IEEE Standard for Floating Point Numbers 5
1.3 Boolean Logic 6
1.3.1 Boolean Algebra Laws (Huntington’s Postulates) 7
1.3.2 Duality Theorem 7
1.3.3 Consensus Theorem 7
1.3.4 Positive Logic and Negative Logic 8
1.4 Digital Circuits 11
1.4.1 Combinational Circuits 11
1.4.2 Sequential Circuits 16
1.5 Digital Logic Families 26
Important Formulas 28
Solved Examples 28
Gate Previous Years’ Questions 33
Practice Exercises 46
Answers to Practice Exercises 51
4 Algorithms 177
4.1 Introduction 177
4.2 Algorithm 177
4.2.1 Properties of Algorithm 177
4.2.2 Steps to Solve a Problem 177
4.2.3 Algorithm Analysis 178
4.2.4 Asymptotic Notation 178
4.2.5 Recurrence Relations 180
4.3 Hashing 182
4.3.1 Hash Table 182
4.3.2 Hashing Functions 182
4.3.3 Collisions 182
4.4 Binary Heap 184
4.4.1 Insertion in Min-Heap Tree 185
4.4.2 Deletion in Min-Heap Tree 185
4.4.3 Time Complexity 186
8 Databases 403
8.1 Introduction 403
8.1.1 Traditional File Processing Approach 403
8.1.2 Database Management System 404
8.2 Components of Database Systems 404
8.3 DBMS Architecture 405
8.3.1 3-Tier Architecture 405
8.3.2 Data Independence 405
8.4 Data Models 405
8.4.1 Relational Model 406
8.4.2 ER Model 407
8.5 Database Design 410
8.5.1 Integrity Constraints 410
8.5.2 Normal Forms 411
8.5.3 Attribute Closure 411
8.5.4 Key 411
8.5.5 Decomposition 412
8.6 Query Languages (SQL) 414
8.6.1 SQL Commands 414
8.7 File Structures 416
8.7.1 Sequential Files 416
8.7.2 Indexing 416
8.7.3 B Tree 416
8.7.4 B+ Trees 416
8.8 Transactions and Concurrency Control 417
8.8.1 Transactions 417
8.8.2 Schedule 417
8.8.3 Classification of Schedule Based on Recoverability 419
8.8.4 Classification of Schedule Based on Serializability 420
8.8.5 Concurrency Control Protocol 423
Important Formulas 424
Solved Examples 425
GATE Previous Years’ Questions 427
Practice Exercises 444
Answers to Practice Exercises 449
5
Number of questions
4
Marks 1
3 Marks 2
Total number of questions
2
0
2015 2014 2013 2012 2011 2010
Year Concepts
2015 K-maps, Half and full adder, JK flip-flop, Boolean operators, Prime implicates, Base
values, Johnsen counter, Binary operator, D flip-flop
2014 Minterm expressions, Combinational functional blocks, Boolean expressions, flip-flops
2013 2’s complement, Truth tables, Logic gates, RAM chips
2012 K-maps, Truth tables, IEEE single precision
2011 Flip-flops, Boolean expressions, Logic gates
2010 K-maps, 2’s complement, Multiplexer, Boolean expression, Sequential circuits
DIGITAL ELECTRONICS
Syllabus: Digital electronics: logic functions, minimization, design and synthesis of combinational and sequential
circuits, number representation and computer arithmetic (fixed and floating point).
Digital electronics represents discrete signals instead of Radix/Base representation Codes representation
signals in a continuous range. It uses two binary levels
0’s (corresponding to false) and 1’s (corresponding to
true). The main reason for advancement in digital elec-
Numeric codes Alphanumeric codes
tronics is integrated circuits (ICs).
EBCDIC
Weighted Non-weighted ASCII
1.2 NUMBER SYSTEM
Binary Gray code
(111001)2 (0.0100)2 −B
----------
Therefore, (57.3)10 = (111001.0100)2 ----------
2. Any number system to decimal number the steps involved are as follows:
system: Consider the example of (111001.0100)2.
Step 1. Find R-1’s complement of B.
To find the decimal equivalent we have
Step 2. Add A and B.
(111001.0100)2 = 1 × 25 + 1 × 24 + 1 × 23 + 0 × 22
Step 3. If there is carry, then add this carry to the
+ 0 × 21 + 1 × 20 + 0 × 2-1 answer.
+ 1 × 2-2 + 0 × 2-3 + 0 × 2-4
= 32 + 16 + 8 + 0 + 0 + 1 + 0
+ 0.25 + 0 + 0 = (57.25)10
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
s exponent significand/mantissa
1-bit 8-bits 23-bits
Figure 1.2 | Single Precision (32 bits).
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
s exponent significand/mantissa
1-bit 11-bits 20-bits
significand (continued)
32-bits
Figure 1.3 | Double Precision (64 bits).
Inputs Operations
X Y NAND NOR Implication
X Implication Y Y Implication X
0 0 1 1 1 1
0 1 1 0 1 0
1 0 1 0 0 1
1 1 0 0 1 1
R: x * y * z = 1 7. X + (Y ⋅ Z ) = (X + Y ) ⋅ (X + Z )
8. (X ⋅ Y )′ = X ′ + Y ′
Solution:
9. X ⋅ (X + Y ) = X
x Y z P Q R
0 0 1 0 0 1 1.3.3 Consensus Theorem
0 1 0 0 1 1 It is applicable only if a Boolean function has
1 0 0 1 0 1 1. three variables
1 1 1 1 1 1 2. each variable is used two times
3. only one variable in complemented or non-
complemented form
1.3.1 Boolean Algebra Laws (Huntington’s
Then, the term related to the complemented and non-
Postulates)
complemented variable is the answer.
The laws of Boolean Algebra are given by Huntington’s
postulates: Example 1.2
1. X + 0 = X AB + A ′C + BC = AB + A ′C
2. X + (∼X ) = 1
Usually, the two (binary) levels are represented in two A Boolean expression which cannot be further reduced
different ways: is said to be in minimal form. Each term in minimal
1 (signifying high) and 0 (signifying low) − Known as Boolean expression is called prime implicant. The follow-
positive logic ing are the two methods for minimization:
or 1. Boolean Theorems (postulates): Using the
1 (signifying low) and 0 (signifying high) − Known as Boolean postulates, we can simplify an expression
negative logic using algebraic method. This method is relatively
difficult as we need to remember all the postulates.
1.3.4.1 Canonical and Standard Forms Problem 1.7: Simplify the Boolean function X = (A + B)(A + C )
X = (A + B)(A + C ).
The minterms and the maxterms are given in Table 1.3.
Solution:
Product-of-sums (POS) form: X = AA + AC + AB + BC
F = (x ′y ′z ′) + (xy ′z ′) + (xyz)
= A + AC + AB + BC
= A(1 + C ) + AB + BC
= m0 + m4 + m7 = A(1) + AB + BC
= ∑ m(0, 4, 7) = A + AB + BC
= A(1 + B) + BC
Sum-of-products (SOP) form: = A(1) + BC
F = (X + Y + Z ) ⋅ (X ′ + Y + Z ) ⋅ (X + Y + Z ) = A + BC
= M 0 ⋅ M 4 ⋅ M7
Problem 1.8: Simplify the Boolean function X = A + A¢B
= ∏ M (0, 4, 7) X = A + A¢B.
2. Karnaugh-Map Method (K-Map method): Grouping of two adjacent cells removes one vari-
Karnaugh-map (K-map) involves graphical repre- able and output will contain common variables in
sentation for simplifying Boolean expressions. It two terms.
consists of group of adjacent cells in which each 2. Quads: We can group four adjacent cells in K-map
cell corresponds to one of the combinations of max- forming a quad. Grouping a quad eliminates the
terms or minterms of n variables and is represented two variables that appear in both normal and com-
by group of literals. For n-variable K-map, there plemented form. Resultant terms contain (n − 2)
are 2n cells required (Figs. 1.5−1.7). From one variables, where n is the number of variables.
cell to the neighbouring cell there must be only 3. Octet: A group of eight adjacent cells is called
one literal change. While solving the K-map from octet. Grouping of 1’s eliminates three variables.
higher-order group to lower-order group only, the Resultant terms contain (n − 3) variables, where
simplification must be done. In K-map, leftmost n is the number of variables.
column and rightmost column of the same row are
considered to be adjacent. 1.3.4.4 SOP Simplification Using K-Map
If K = 1, it represents that all the cells are 1 so
there is no need of circuit. A higher priority will be given to more possible covering,
that is, octet then to quad and then to pairs. The follow-
1.3.4.3 Grouping of Cells for Simplification ing procedure is followed:
1. Check the SOP expression and convert to standard
The grouping of cells for simplification is done in the and canonical form if required.
following ways: 2. Draw the K-map based on the number of variables.
1. Pairs: If two adjacent cells either in row or in 3. On the basis of SOP canonical form, 1’s are entered
column contain 1’s, they are said to be in pairs. to corresponding cells. Group the adjacent cells
and take the common variable from each grouping.
4. Combine each group variable to obtain simplified
B 0 1 B 0 1 expression.
A A
0 00 01 0 0 1
1 10 11 1 2 3 1.3.4.5 POS Simplification Using K-Map
Two-variable K-map Cell designation
1. Check the POS expression and convert it to stan-
dard and canonical form if required.
B B B´ 2. Draw the K-map based on the number of variables.
A
A A + A+ 3. Enter 0’s in corresponding cells and group the
B B´ B adjacent cells.
A B B´
A´ A´B´ A´B A´ A´ + A´ + 4. Take the common variable from each grouping and
A AB´ AB B B´ write terms in OR form. The uncomplemented vari-
able is assigned 0 and complemented is assigned 1.
Cells with minterm Cells with maxterm
5. Combine each OR term from the different groups
Figure 1.5 | Two-variable K-map. to obtain simplified expression.
BC 00 01 11 10 BC 00 01 11 10
A A
0 000 001 011 010 0 0 1 3 2
1 100 101 111 110 1 4 5 7 6
Three-variable K-map Cell designation
BC B´C BC BC´ BC B + C B + C´ B´ + C´ B´ + C
B´C´
A A
A´ A´B´C´ A´B´C A´B C A´B C´ A A + B + C A + B + C´ A + B´ + C´ A + B´ + C
A AB´C´ AB´C AB C AB C´ A A´ + B + C A´ + B + C´ A´ + B´ + C´ A´ + B´ + C
Cells with minterm Cells with maxterm
Figure 1.6 | Three-variable K-map.
CD 00 01 11 10 CD 00 01 11 10
AB AB
00 0000 0001 0011 0010 00 0 1 3 2
01 0100 0101 0111 0110 01 4 5 7 6
11 1100 1101 1111 1110 11 12 13 15 14
10 1000 1001 1011 1010 10 8 9 11 10
Four-variable K-map Cell designation
CD
AB C+D C + D´ C´ + D´ C´ + D
A+B A+B+C+D A + B + C + D´ A + B + C´ + D´ A + B + C´ + D
A + B´ A + B´+ C + D A + B´+ C + D´ A + B´+ C´ + D´ A + B´ + C´+ D
A´+ B´ A´+ B´+ C + D A´+ B´+ C + D´ A´+ B´+ C´+ D´ A´+ B´+ C´+ D
A´+ B A´+ B + C + D A´+ B + C + D A´+ B + C´+ D´ A´+ B + C´+ D
Cells with maxterm
Figure 1.7 | Four-variable K-map.
01 O O O O
4 5 7 6
1.4.1.1 Half Adder
Combinational circuits Sequential circuits
A half adder is a combinational circuit for the addi-
tion of two 1-bit numbers. X and Y are inputs and
outputs are sum (S) and carry (C) (Fig. 1.9 and
Synchronous Asynchronous Table 1.4).
X′
Y′
X
Sum (S)
Y Cin
X´
Carry (C) Y
C′in
Sum (S)
Figure 1.9 | Logic diagram of a half adder. X
Y′
C′in
Table 1.4 | Truth table X
Inputs Outputs Y
Cin
X Y Carry (C ) Sum (S )
0 0 0 0 X
0 1 0 1 Y
1 0 0 1 Y
Cin Carry (C)
1 1 1 0
X
The sum and carry are as follows: Cin
S = X ′Y ′Cin + X ′YCin
′ + XYCin D = XY ′ + X ′Y
Cout = ACin + XY + YCin B = XY
X´
Y´
Bin X´
X´ Y
Y
B´in Y
Difference (D)
Bin Borrow
X
Y´ (Bout)
B´in X´
Bin
X
Y
Bin
1.4.1.4 Full Subtractor adders and 1 OR gate, so n-bit parallel adder can be made
from (2n − 1) half adders and (n − 1) OR gates.
A full subtractor is a combinational circuit that per-
forms subtraction involving 3 bits (minuend bit, sub- Y3 X3 Y2 X2 Y1 X 1 Y0 X0
trahend bit and borrow from previous stage) (Table 1.7
and Fig. 1.12).
Table 1.7 | Truth table Full adder Full adder Full adder Half adder
Inputs Outputs
X Y Bin Difference (D) Bout
Cout S3 S2 S1 S0
0 0 0 0 0
0 0 1 1 1 Figure 1.13 | Block diagram of a parallel adder.
0 1 0 1 1
1.4.1.6 Look-Ahead Carry Header
0 1 1 0 1
1 0 0 1 0 In a parallel adder, carry propagation delay is present.
1 0 1 0 0 To overcome this difficulty, look-ahead carry adder is
1 1 0 0 0 used. It uses logic gates to look at lower-order bits of
augend and addend to check if higher-order carry is gen-
1 1 1 1 1 erated or not. It uses two functions, carry generate and
carry propagate, for the same (Fig. 1.14).
The difference and borrow are as follows:
D = X ′Y ′Bin + X ′YBin
′ + XY ′Bin
′ + XYBin Problem 1.13: How many AND and OR gates are
required to design 7-bit look-ahead carry adder.
B = X ′Y + X ′YBin + YBin
Solution: To design a 7-bit look-ahead carry adder,
we need
1.4.1.5 Parallel Adder (Ripple Carry Adder)
n(n + 1) 7(7 + 1)
A parallel adder consists of full adders connected in AND gate = = = 28
2 2
cascade, that is, carry output of each adder is connected to
OR gate = n = 7
the carry input of the next higher-order adder (Fig. 1.13).
An n-bit parallel adder is constructed using (n − 1) full
where n is the number of bits.
adders and 1 half adder. As full adder consists of 2 half
.
1.4.1.7 Decoder . .
2n input . 2n : n . n output
. .
lines . Encoder . lines
A decoder is a combinational circuit that converts
binary information from n input lines to a maximum of
2n unique output lines (Fig. 1.15). The size of a decoder
is n to m where m ≤ 2n. It is used to route input data to Figure 1.17 | Block diagram of an encoder.
a specified output line for memory addressing (Fig. 1.16
and Table 1.8). 1.4.1.9 Multiplexer
..
2n inputs . MUX Output lines
Enable inputs .
Figure 1.15 | Block diagram of a decoder.
n select lines
A B Enable Figure 1.18 | Block diagram of a multiplexer.
Problem 1.14: Implement the function F(A, B, C, D) = Σm(1, 2, 5, 7, 9, 14) using MUX.
Solution: Select variable A for input as B, C and D for selection lines.
As there are four variables so size of MUX is 2N − 1 = 23 = 8 to 1 MUX.
I0 I1 I2 I3 I4 I5 I6 I7
A′ 0 1 2 3 4 5 6 7
A 8 9 10 11 12 13 14 25
0 1 A′ 0 0 A′ A A′
0 0
1 1
A´
2
0 3 8 to 1 Y
0 4 MUX
A´
5
A 6
A´ 7
B C D
Logic diagram
1.4.1.11 Demultiplexer 1.4.2.1 Flip−Flops
input
DEMUX 2n output lines 1. RS flip−flop
2. D flip−flop
3. JK flip−flop
....
4. T flip−flop
n select lines
Figure 1.20 | Block diagram of a demultiplexer. RS Flip−Flop (Direct Coupled)
This sequential logic circuit has two stable states
Implementation of higher order decoders and multiplex- (Q = 1 set state and Q = 0 reset state) achieved by
ers by using lower order. Whenever odd number imple- giving proper inputs to R and S inputs (Figs. 1.21 and
mentation is there, one NOT gate is required. 1.22 and Table 1.10). If the circuit is in one particular
state it continues to remain in that state and can store
1-bit data.
Example 1.3
Design 4 ×16 decoders using 2 × 4 decoders
S Q
16/4 = 4 RS
flip—flop
4/4 = 1 R Q´
Total = 5
Figure 1.21 | Block diagram of an RS flip−flop.
Clocked RS Flip−Flop
S
G3 G1 S
(Set) This circuit sets or resets the memory cell in synchro-
nization with the clock pulse (Figs. 1.23 and 1.24 and
Table 1.12). If clock pulse is not present, the gates G3
and G4 are inhibited; their outputs are 1.
R G4 G2 Q´
(Reset) S Q
SR
Figure 1.22 | Logic diagram of an RS flip−flop.
CLK
flip−flop
R Q´
Table 1.10 | Truth table of RS flip−flop
Figure 1.23 | Block diagram of a clocked SR flip−flop.
Inputs Outputs
Sn Rn Qn+1 Table 1.12 | Truth table of SR flip−flop
0 0 Qn (No change)
Clock Flip−Flop Inputs Outputs
0 1 0 (Reset)
CLK Sn Rn Qn+1
1 0 1 (Set)
1 0 0 Qn
1 1 Prohibited
1 0 1 0
1 1 0 1
The characteristic equation which gives the algebraic 1 1 1 Prohibited
description of the next state of a flip−flop obtained by 0 X X Qn
K-map simplification is as follows (see Table 1.11):
Qn +1 = S + R ′Qn
S G3
Table 1.11 | Truth table
G1 Q
S Qn
J or S
R 00 01 11 10 D Q
D
1 1 1 CLK
flip−flop
0 1 3 2
Q´
X X K or R
4 5 7 6 JK or SR Flip−Flop converted to D
Figure 1.25 | Block diagram of a D flip−flop.
Qn+1 = S + R¢Qn
Table 1.13 | Truth table of D flip−flop Table 1.14 | Truth table of JK flip−flop
Flip−F lop Present Next Flip−Flop Present Next
Inputs State State Inputs State State
Dn Qn Qn+1 Jn Kn Qn Qn+1
0 0 0 0 0 0 0
0 1 0 0 0 1 1
1 0 1 0 1 0 0
1 1 1 0 1 1 0
1 0 0 1
D S 1 0 1 1
Q
1 1 0 1
CLK 1 1 1 0
J G3 G1 Q
Q´
R Q´
Qm SS
J G3m G1m G3s G1s Q
CLK
T Flip−flop
It is also known as toggle flip−flop, which is obtained Qn 0 1
by connecting both inputs of JK flip−flop (Fig. 1.32 and T
Table 1.15). 1
0 0 1
J
T Q
1
JK (T) 1 2 3
CLK
flip−flop
Q´ Qn+1 = T´Qn + T Q´n
K
Figure 1.32 | Block diagram of a T flip−flop. Figure 1.33 | K-Map
Shift
registers
Based Based on
on direction input−output
of data
movement mode
Shift left Shift right Bidirectional Serial-in Serial-in Parallel-in Parallel-in Universal
register register shift serial-out parallel-out serial-out parallel-out register
Table 1.17 | Flip−flop conversion equations computation. There are different types of registers as
shown in Fig. 1.34.
S = JQ′
1 SR to JK
R = K ⋅Q 1. Serial-In/Serial-Out Shift Register: It accepts
data serially on single input line and produces output
S =D in serial form. Data can be shifted right or left.
2 SR to D •• Shift right register: In shift right register,
R = D′ serial data is entered from the left side of a regis-
S = TQ′ ter and leaves from the right side. For example,
3 SR to T a 4-bit number 1101 (QDQCQBQA) is entered
R = TQ serially into first flip−flop D from the left side
(Fig. 1.35).
J =S
4 JK to SR
K =R
J =D FF1 FF2 FF3 FF4
5 JK to D Din D QD D QC D QB D QA Dout
K = D′
D C B A
J =T
6 JK to T
K=T
7 D to JK D = TQ′ + K ′Q CLK
Figure 1.35 | 4-Bit shift right register.
8 D to SR D = S + R ′Q
•• Shift left register: In shift left register, serial Note: A universal register can perform all the
data is entered from the right side of the register operations of shift register.
and leaves from the left side. For example, a
2. Serial-In to Parallel-Out (SIPO): The regis-
4-bit number 1101 (QDQCQBQA) is entered seri-
ter is loaded with data serially, one bit at a time,
ally into input of shift left register (Fig. 1.36).
with the stored data being available at the output
FF4 FF3 FF2 FF1 in parallel form (Fig. 1.37).
Dout Din
QD D QC D QB D The content of register after each clock pulse is
given in Table 1.20.
D C B A
Table 1.20| Content of register after each clock pulse
CLK Clock Pulse QA QB QC QD
Figure 1.36 | 4-Bit shift left register. 0 0 0 0 0
The content of register after each clock pulse is 1 1 0 0 0
given in Table 1.19.
2 0 1 0 0
Table 1.19 | Content of register after each clock pulse
3 0 0 1 0
Clock Pulse QD QC QB QA
4 0 0 0 1
0 0 0 0 0
5 0 0 0 0
1 0 0 0 1
2 0 0 1 1 3. Parallel-In to Serial-Out (PISO): The parallel
3 0 1 1 0 data is loaded simultaneously into the register and
is shifted out of the register serially, one bit at a
4 1 1 0 1
time (Fig. 1.38).
QA QB QC QD
D Q D Q D Q D Q
Serial FFA FFB FFC FFD
Data In
CLK CLK CLK CLK
CLR CLR CLR CLR
Clear (CLR)
Clock (CLK)
1 Q Q
mux D Q mux D Q mux D Q mux D
FFA FFB FFC FFD Serial
Data Out
CLK CLK CLK CLK
Clock
PD PC PB PA
4-bit Parallel Data Input
Figure 1.38 | 4-Bit PISO register.
QD QC QB QA
D Q D Q D Q D Q
FFA FFB FFC FFD
Clock
PD PC PB PA
4. Parallel-In to Parallel-Out (PIPO): The par- The counter with n flip−flops has a maximum modulus
allel data is loaded simultaneously into the register, 2n so it can count from 0 to 2n−1. It can also produce
and transferred together to their respective outputs MOD numbers less than 2n by skipping the states.
by the same clock pulse (Fig. 1.39).
1. 4-Bit Asynchronous Up Counter: Figures
1.40−1.42 show a 4-bit asynchronous counter con-
Counters
structed using JK flip−flops. The clock input of first
A counter is a sequential logic circuit that is capable
flip−flop (the one with the Q0 output) is connected
of counting the number of clock pulses arriving at its
to the external clock. The clock of second flip−flop
input in a cyclic sequence. It is just a register that shifts
drives from output Q0 of the first flip−flop and so
through a predetermined sequence of states upon the
on. Each flip-flop input (J = K = 1) is connected
application of input clock pulses. There are two differ-
to HIGH voltage so it toggles when negative edge is
ent types of counters − synchronous counter (parallel
arrived at clock input and counts from 0000 to 1111.
counter) and asynchronous counter (serial or ripple
counter) (Table 1.21). 0000
1111 0001
Table 1.21 | Differences between asynchronous and
0 1
1110 15
synchronous counter. 2 0010
14
Asynchronous Synchronous 0011
1101 13 3
Counter Counter
The output of first There is no connection
flip−flop drives the clock between output of first 1100 12 4 0100
for next flip−flop. So all flip−flop and clock input
flip−flops are not clocked of next flip−flop. All the 5 0101
simultaneously. flip−flops are clocked 1011 11
simultaneously.
10 6
Speed is low. Speed is high. 0110
1010
9 7
Simple logic circuit as Complex design as number 8
number of states increases. of states increases. 1001 0111
1000
Figure 1.40 | State diagram of a 4-bit asynchronous
Example: Ring counter, Example: Binary ripple
Johnson counter. counter, Up-down counter.
counter.
Vcc = 5V
J0 Q0 J1 Q1 J2 Q2 J3 Q3
CLK
Count
QA 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
QB 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0
QC 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0
QD 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0
2. MOD-10 Asynchronous Counter: A MOD-10 otherwise it maintains the previous state. The flip−
asynchronous counter has 10 states and number of flop 2 toggles when both Q1 and Q0 are 1. The flip−
flip−flops required are 24 ≥ 10 (Figs. 1.43 and 1.44). flop 3 toggles when Q2, Q1 and Q0 are 1 (Table 1.22).
It counts the sequence from 0000 to 1001 and after
that it returns to the initial state. Table 1.22 | Truth table for 4-bit synchronous counter
0000 Count Q3 Q2 Q1 Q0
0001
15 0 1 0 0 0 0 0
2 0010
14 1 0 0 0 1
3 0011 2 0 0 1 0
13
3 0 0 1 1
12 4 0100 4 0 1 0 0
5 0101 5 0 1 0 1
11
6 0 1 1 0
10 6
0110 7 0 1 1 1
1010
9 7 8 1 0 0 0
8
1001 0111
1000 9 1 0 0 1
Figure 1.43 | State diagram of a 4-bit MOD-10 counter. 10 1 0 1 0
3. 4-Bit Synchronous Counter: In a 4-bit synchro- 11 1 0 1 1
nous counter, the least significant bit of flip−flop is 12 1 1 0 0
connected at high level and other inputs of JK flip−
13 1 1 0 1
flop are driven by some combination of flip−flop out-
puts. Figure 1.45 shows the logic diagram of a 4-bit 14 1 1 0 0
synchronous counter. The output of flip−flop0 always 15 1 1 0 1
toggle, and that of flip−flop1 toggles when Q0 is 1,
Vcc
0 1 0 1
J0 Q0 J1 Q1 J2 Q2 J3 Q3
CLK
1
Figure 1.44 | Logic diagram for a MOD-10 asynchronous counter.
Q0 Q1 Q2 Q3
Vdd
J Q J Q J Q J Q
FF 0 FF 1 FF 2 FF 3
C C C C
K Q´ K Q´ K Q´ K Q´
4. MOD-5 Synchronous Counter: Table 1.23 | Excitation table for JK flip−flop
Step 1: Number of states is five, so counting sequence
0 to 4. Flip−Flop States JK Flip−Flop Inputs
Flip−flops required are 2n ≥ 5, so n = 3. Present Next J K
Let us label these flip−flops as A, B and C. State State
Step 2: Obtain the excitation table for JK flip−flop (Qn) (Qn+1)
(Table 1.23). 0 0 0 X
Step 3: Develop state table using the excitation table
(Table 1.24). 0 1 1 X
Step 4: K-map simplification for finding expressions 1 0 X 1
corresponding to each input (Fig. 1.46). 1 1 X 0
QC 00 01 11 10 QC 00 01 11 10
0 1 0 X X X X
0 1 3 2 0 1 3 2
1 X X X X 1 1 X X X
4 5 7 6 4 5 7 6
J C = Q BQ A KC = 1
QC 00 01 11 10 QC 00 01 11 10
0 1 X X 0 X X 1
0 1 3 2 0 1 3 2
1 X X X 1 X X X X
4 5 7 6 4 5 7 6
J B = QB KB = QA
QC 00 01 11 10 QC 00 01 11 10
0 1 X X 1 0 X 1 1 X
0 1 3 2 0 1 3 2
1 X X X 1 X X X X
4 5 7 6 4 5 7 6
JA = Q´C KA = 1
Figure 1.46 | K-map simplification.
Step 5: Logic diagram of MOD-5 counter (Fig. 1.47). Table 1.25 | Continued
Logic families
Bipolar Unipolar
TTL (transistor
transistor logic)
IMPORTANT FORMULAS
SOLVED EXAMPLES
1. A correct output is achieved from a master-slave Solution: The clock is high, only the flip-flops
JK flip-flop only if its inputs are stable while the are stable when the inputs are stable.
Ans. (d)
(a) Clock is LOW
(b) lave is ready to receive the input from master 2. An inverter circuit can be realized with how many
(c) Master gives a reset signal to slave NAND gates?
(d) Clock is HIGH
Two-input NAND gate equivalent 6. The NAND or NOR gates are referred to as
“universal” gates because
Ans. (a)
(a) they are easier to implement.
3. A modulus-12 ring counter requires a minimum of
________.
(b) they can be used to build all the other types
of gates.
(a) 10 flip-flops (b) 12 flip-flops (c) they are available in different IC families.
(c) 8 flip-flops (d) 6 flip-flops (d) they are available everywhere in the world.
Solution: In the ring counter, n flip-flops are used Solution: NAND and NOR gates are univer-
for the mod-n. 12 flip-flops because ring counter sal because all Boolean functions that can be
uses states n, so here modulus is 12. implemented by AND, OR and NOT gates can be
Ans. (b) implemented by NAND and NOR gates.
4. How is a strobe signal used when serially loading a Ans. (b)
shift register? 7. Match the following:
(a) To turn the register set and reset
(b) To control the number of clocks IC Family GATE
(c) To determine which output has a HIGH value TTL NOR
(d) To cascade it with other shift register CMOS NOT
Solution: In digital circuits, a shift register is a ECL NAND
cascade of flip-flops, sharing the same clock each
time. This is called clocking or strobing to the (a) TTL:NAND; ECL:NOR; CMOS:NOT
register to set the clocks. (b) TTL:NAND; ECL:NOT; CMOS:NOR
Ans. (b) (c) TTL:NOT; ECL:NAND; CMOS:NOR
5. An active-HIGH SR latch has a 0 on the S input (d) TTL:NOR; ECL:NOT; CMOS:NAND
and a 1 on the R input. Now when the R input goes Solution: The NAND function is actually the
to 0, the latch will be ________. simplest, most natural mode of operation for this
(a) SET (b) RESET TTL design. The basic gates are OR/NOR gates
(c) CLR (d) STROBE for ECL Construction. Inverters can be con-
structed using two complementary transistors in
Solution: a CMOS configuration.
Ans. (a)
S′
Q 8. Which of the following satisfies the commutative
law but not the associative law?
(a) NOR (b) OR
(c) XOR (d) NAND
Solution
Let ↑ signify the NAND operation. Then there
Q′ exist propositions p, q, r such that
R′ p↑ (q↑r) (p↑q) ↑r
This implies NAND is not associative. 12. A sequential circuit gives an output depending upon
p↑q = q↑p (a) its present state inputs only.
This implies NAND is commutative. (b) its present-1 state input only.
Ans. (d) (c) both present- and past-state input.
9. Match the following: (d) the size of the memory.
Solution: f1 f
f2
x y z x ⊕ y (x ⊕ y)⊕ z y ⊕ z x ⊕(y ⊕ z)
f3 = ?
0 0 0 1 0 1 0
f1(w, x, y, z) = ∑ (8, 9, 10)
0 0 1 1 1 0 1 f2(w, x, y, z) = ∑ (7, 8, 12, 13, 14, 15)
0 1 0 0 1 0 1 f(w, x, y, z) = ∑ (8, 9)
0 1 1 0 0 1 0 The function f3 is
1 0 0 0 1 1 1 (a) ∑ 9, 10 (b) ∑ 9
1 0 1 0 0 0 0 (c) ∑ 1, 8, 9 (d) ∑ 8, 10, 18
1 1 0 1 0 0 0 Solution: By the logic circuit, function f is given as
1 1 1 1 1 1 1 f = f1f2 + f3
f1 = ∑ (8, 9, 10)
f2 = ∑ (7, 8, 12, 13, 14, 15)
Ans. (b)
17. Let * be defined as x * y = x + y . Let z = x*y, As according to logic, common minterm in f1 and
then value of z*x is f2 is 8, so
(a) x + y (b) x (c) 0 (d) 1 f1f2 = ∑ (8)
Solution: Substituting value of z = x*y in the to get output as f = ∑ (8, 9), f3 can be either
expression: ∑ (8, 9) or f3 = ∑ 9
Ans. (b)
z ∗ x = (x ∗ y) ∗ x = (x + y) * x = (x + y) + x
20. Consider the circuit shown in the following figure;
= x⋅y +x f implements
Now using absorption law: a + ab = a
0
x⋅y +x = x
C
4 to 1
C 1
So, result is: z*x = x Multiplexer f
Ans. (b) C 2
21. Which of the following functions implements the 23. Consider the circuit given below with initial state
Karnaugh map shown below? Q0 = 1, Q1 = Q2 = 0. The state of the circuit is
given by the value 4Q2 + 2Q1 + Q0.
CD
AB 00 01 11 10
00 0 0 1 0 D 0 Q0 D 1 Q1 D 2 Q2
01 X X 1 X
11 0 1 1 0 LSB MSB
CLK
10 0 1 1 0 Which one of the following is the correct state
sequence of the circuit?
(a) AB + CD (b) D(C + A)
(a) 1, 3, 4, 6, 7, 5, 2 (b) 1, 2, 5, 3, 7, 6, 4
(c) AD + AB (d) (C + D)(C + D)(A + B) (c) 1, 2, 7, 3, 5, 6, 4 (d) 1, 6, 5, 7, 2, 3, 4
Solution: Karnaugh map of the function is given Solution:
as follows:
D2(Q1) D1(Q0) D0 Q2 Q1 Q0 4Q2 +
CD 00 01 11 10 (Q1⊕Q2) 2Q1 + Q0
AB
00 0 0 1 0 Initially 0 0 1 1
0 1 0 0 1 0 2
01 X X 1 X
1 0 1 1 0 1 5
11 0 1 1 0 0 1 1 0 1 1 3
1 1 1 1 1 1 7
10 0 1 1 0
1 1 0 1 1 0 6
SOP form for the given function is f(A, B, C, D) = 1 0 0 1 0 0 4
CD + AD = D(C + A) 0 0 1 0 0 1 1
Ans. (b)
22. Refer to the following arrangement of master-slave Therefore, the correct state sequence of the circuit
flip-flops. is 1, 2, 5, 3, 7, 6, 4. Ans. (b)
P Q 24. A 32-bit floating-point number is represented by a
7-bit signed exponent, and a 24-bit fractional man-
J K D
tissa. The base of the scale factor is 16.
`1’
(a) The range of the exponent is .
CLK (b) The range of the exponent is if the scale
It has the initial state of P, Q as 0, 1 (respectively). factor is represented in excess −64 format.
After the clock cycles, the output state P, Q is Solution: The given floating point number format is
(respectively)
1 7 24
(a) 1, 0 (b) 1, 1 (c) 0, 0 (d) 0, 1
S EXP Fraction (Mantissa)
Solution: 31 30 24 23 0
J K D Bias + 64
1 1 (P) P Q The above floating-point architecture is IBM float-
Initially 0 1 ing-point architecture.
1 1 0 1 0 The number is represented as the following formula
1 1 1 0 1 (−1)S× 0. Mantissa × 10EXP - 64
(a) The range of the exponent is the range of the
For J = 1 and K = 1, the output is complement to 7-bit signed numbers, which is −64 to +63.
previous state. (b) The range of the exponent if the scale factor is
Ans. (a) represented in excess −64 format is 0 to 127.
25. Booth’s coding in 8-bits for the decimal number −57 is 26. The 2’ complement representation of the decimal
value −15 is
(a) 0 - 100 + 1000 (b) 0 − 100 + 100 − 1
(c) 0 − 1 + 100 - 10 + 1 (d) 00 − 10 + 100 − 1 (a) 1111 (b) 11111
(c) 111111 (d) 10001
Solution: −57 can be represented in 2’s comple-
ment form: 1 1 0 0 01 1 1 Solution: + 15 = 1 1 1 1
Booth’s coding for decimal number −57 is −15 in 2’s complement = 10001
1 1 0 0 0 1 1 1 0 Ans. (d)
⇓
0 −1 0 0 +1 0 0 −1 −1
Ans. (b)
5. The literal count of a Boolean expression is the input A, how many transition(s) (change of logic
sum of the number of times each literal appears levels) occur(s) at B during the interval from 0 to
in the expression. For example, the literal count 10 ns?
of (xy + xz′) is 4. What are the minimum possible
Logic 1
literal counts of the product-of-sum and sum-of- A Logic 0
product representations, respectively, of the func- Time 0 1 2 3 4 5 6 7 8 9 10 11
tion given by the following Karnaugh map? Here, (ns)
X denotes “don’t care”.
ZW (a) 1 (b) 2
00 01 11 10 (c) 3 (d) 4
XY
(GATE 2003: 2 Marks)
00 X 1 0 1
01 0 1 X 0
Solution: Total number of transitions is 3.
1: 0 to 0
11 1 X X 0 2: 0 to 1
3: 1 to 1
10 X 0 0 X Ans. (c)
7. A Boolean function x′y′ + xy + x′y is equivalent to
(a) (11, 9) (b) (9, 13) (a) x′ + y′ (b) x + y
(c) (9, 10) (d) (11, 11) (c) x + y′ (d) x′ + y′
(GATE 2003: 2 Marks) (GATE 2004: 1 Mark)
Solution:
Solution:
ZW x′y′ + xy + x′y
00 01 11 10
XY = x′(y′ + y) + xy (as y′ + y = 1)
00 X 1 0 1 = x′ + xy (as x + x′y = x + y)
= x′ + y
01 0 1 X 0 Ans. (d)
The non-inverting buffers have delays d1= 2 ns and Solution: By doing hit and trial with the options,
d2 = 4 ns as shown in the figure. Both XOR gates we arrive at
and all wires have zero delay. Assume that all gate If x = 8, (73)8 = 7 × 81 + 3 × 80 = 59
inputs, outputs and wires are stable at logic level 0 If y = 11, (54)11 = 5 × 111 + 4 × 110 = 59
at time 0. If the following waveform is applied at Ans. (d)
1 1 1
12. A circuit outputs a digit in the form of 4 bits,
where 0 is represented by 0000, 1 by 0001, …, 9
by 1001. A combinational circuit is to be designed here ca′ and c′a are essential prime implicants, we
which takes these 4 bits as input and output 1 if cannot remove them.
the digit ≥5, and 0 otherwise. If only AND, OR and Ans. (a)
NOT gates are used, what is the minimum number
14. Consider the partial implementation of a 2-bit
of gates required?
counter using T flip-flops following the sequence
(a) 2 (b) 3 0-2-3-1-0, as shown in the following figure.
(c) 4 (d) 5
(GATE 2004: 2 Marks)
Solution: X
T2 Q2 T1 Q1
a B c d Output
0 0 0 0 0 MSB LSB
0 0 0 1 0
CLK
0 0 1 0 0
0 0 0 1 0 To complete the circuit, the input X should be
0 1 0 0 0 (a) Q2′ (b) Q2 + Q1
0 1 1 1 1 (c) (Q1 ⊕ Q2 )′ (d) Q1 ⊕ Q2
(Continued) (GATE 2004: 2 Marks)
Y f 00 1
01 1 1
Z
(a) f is independent of X. 10 1 1
(b) f is independent of Y.
25. You are given a free running clock with a duty Solution:
cycle of 50% and a digital waveform of which f1 = zx + zy
changes only at the negative edge of the clock.
Which one of the following circuits (using clocked f = y (zx + zy ) + yx
D flip-flops) will delay the phase off by 180°? = yzx + yzy + yx = yx + yzx + zy
= yx + y (z + zx) = yx + y (z + x)
(a) f D D Q = yx + yz + yx
Q
Now use K-map, we have
CLK YZ
X 00 01 11 10
(b) f D Q 0 1
D Q
CLK
1 1 1 1 1
Y
Data
0 MUX q2 Q D q1 Q D q0 Q D
Z f
1 clk clk clk
Clock
X
The following data 100110000 is supplied to the
Y “data” terminal in nine clock cycles. After that the
values of q2q1q0 are
(a) xz + xy + yz (b) xz + xy + yz (a) 000 (b) 001
(c) xz + xy + yz (d) xz + xy + yz (c) 010 (d) 101
(GATE 2006: 1 Mark) (GATE 2006, 1 Mark)
∑ i=1 8i = 8 + 64 = 9
From the K-map, we have w′xz, wxy′, xy′z, xyz, wyz. log 64 64 64 64
Ans. (a)
Ans. (c)
30. We consider the addition of two 2’s complement 34. Consider the following Boolean function of four
numbers bn-1bn-2 … b0 and an-1an-2 … a0. A variables:
binary adder for adding unsigned binary numbers is
used to add the two numbers. The sum is denoted f(w,x,y,z) = ∑ (1,3, 4, 6, 9, 11,12,14)
by cn-1cn-2 … c0 and the carry-out by cout. The function is
Which one of the following options correctly identi- (a) independent of one variable
fies the overflow condition? (b) independent of two variables
(a) cout (an -1 ⊕ bn -1 ) (c) independent of three variables
(d) dependent on all the variables
(b) an -1bn -1 cn -1 + an -1bn -1cn -1 (GATE 2007: 1 Mark)
(P) x′y′z′+ w′xy′+ wy′z + xz Solution: Since only one inverter is allowed, so
(Q) w′y′z′+ wx′y′+ xz n−1 variable will be used for selection and one
(R) w′y′z′+ wx′y′+ xyz + xy′z as input. 2n−1 line to 1 line multiplexer will be
(S) x′y′z′+ wx′y′+ w′y required.
Ans. (c)
(a) P only (b) Q and S
(c) R and S (d) S only 38. In a look-ahead carry generator, the carry generate
(GATE 2007: 2 Marks) function Gi and the carry propagate function Pi for
inputs Ai and Bi are given by
Solution:
Pi = Ai ⊕ Bi and Gi = AiBi
wx yz 00 01 11 10
The expressions for the sum bit Si and the carry
00 1 bit Ci+1 of the look-ahead carry adder are given by
Si = Pi ⊕ Ci and Ci +1 = Gi + PiCi, where C0 is
01 1 1 1 the input carry.
11 1 1 Consider a two-level logic implementation of the
look-ahead carry generator. Assume that all Pi and
10 1 1 Gi are available for the carry generator circuit and
that the AND and OR gates can have any number
Option (d) does not contain essential prime of inputs. The number of AND gates and OR gates
implicants. needed to implement the look-ahead carry genera-
Ans. (d) tor for a 4-bit adder with S3, S2S1, S0, and C4 as its
outputs are, respectively,
36. Define the connective * for the Boolean variables
X and Y as X*Y = XY + X′Y′. Let Z = X*Y. (a) 6, 3 (b) 10, 4
Consider the following expressions P, Q and R. (c) 6, 4 (d) 10, 5
(GATE 2007: 2 Marks)
P: X = Y*Z Q: Y = X*Z R: X*Y*Z = 1
Which of the following is TRUE? Solution: Two 1-bit adders are required, where
each 1-bit adder required 5 AND and 2 OR gates.
(a) Only P and Q are valid. So, 10 AND gates and 4 OR gates are required.
(b) Only Q and R are valid.
Ans. (b)
(c) Only P and R are valid.
(d) All P, Q, R are valid. 39. The control signal functions of a 4-bit binary coun-
(GATE 2007: 2 Marks) ter are given below (where X is “don’t care”):
Solution:
Clear Clock Load Count Function
ab
1 X X X Clear to 0 00 01 11 10
cd
0 X 0 0 No change 00 1 1 1
0 1 X Load input
0 0 1 Count next 01
X
So, b ⋅ d + a ⋅ d
Ans. (a)
Count = 1
41. In the IEEE floating-point representation, the
Clear 4-bit counter Load = 0 hexadecimal value 0x00000000 corresponds to
Clock (a) The normalized value 2−127
(b) The normalized value 2−126
(c) The normalized value +0
Inputs
(d) The special value +0
(GATE 2008: 1 Mark)
⇒ 1 + 2r 2 + r 3 = (1 + r)2 = 1 + r 2 + 2r
function represented by the Karnaugh map?
ab
00 01 11 10
r 3 + r 2 - 2r = 0 ⇒r > 2
cd
00 1 1 1 Ans. (d)
43. Given f1, f3 and f in canonical sum of products form
01 X (in decimal) for the circuit
11 X f1
f2 f
10 1 1 X
f3
(a) b ⋅ d + a ⋅ d (b) a ⋅ b + b ⋅ d + ab ⋅ d f1 = ∑m(4, 5, 6, 7, 8)
f3 = ∑m(1, 6, 15)
(c) b ⋅ d + a ⋅ b ⋅ d (d) a ⋅ b + b ⋅ d + a ⋅ d
f = ∑m(1, 6, 8, 15)
(GATE 2008: 1 Mark) then f2 is
= (P ⋅ Q + P ⋅ Q + P ⋅ Q ⋅ R + PR)(P ⋅ R + Q) 256 × 8
Number of chips required = = 64
32 × 1
= P ⋅Q+P ⋅Q+P ⋅Q⋅R +P ⋅Q⋅R
Ans. (c)
= P ⋅Q (as 1 + R = 1)
Ans. (a)
49. The minterm expansion of f (P , Q, R) = PQ + QR + PR
45. (1217)8 is equivalent to f (P , Q, R) = PQ + QR + PR is
(a) (1217)16 (b) (028F)16 (a) m2+ m4+ m6+ m7 (b) m0+ m1+ m3+ m5
(c) (2297)10 (d) (0B17)16 (c) m0+ m1+ m6+ m7 (d) m2+ m3+ m4+ m5
(GATE 2009: 1 Mark) (GATE 2010: 1 Mark)
Solution:
Solution:
(1217)8 = (001 010 001 111)8
(0010 1000 1111) = (28F)16 = (028F)16 ff((PP,,Q
Q,,R
R)) =
= PQ
PQ+ QR′′ +
+QR +PRPR′′
Ans. (b) =
= PQ
PQ((R
R++RR′′)) +
+((P
P+ +PP ′′))QR
QR′′ +
+PP((QQ+
+QQ′′))R
R′′
46. What is the minimum number of gates required to == PQR
PQR + PQR′′ +
+ PQR +PP′QR
′QR′′ + PQ′′R
+ PQ R′′
implement the Boolean function (AB + C ) if we =
=mm77 +
+mm66 +
+mm22 +
+mm44
have to use only 2-input NOR gates?
(a) 2 (b) 3 Ans. (a)
(c) 4 (d) 5
(GATE 2009: 1 Mark) 50. P is a 16-bit signed integer. The 2’s complement
representation of P is (F87B)16. The 2’s comple-
Solution: ment representation of 8×P is
(AB + C ) = (A + C)(B + C)
(a) (C3D8)16 (b) (187B)16
= ((A + C )′ + (B + C)′)′
(c) (F878)16 (d) (987B)16
Thus, it requires three NOR gates.
Ans. (b) (GATE 2010: 1 Mark)
47. The binary operation is defined as follows
Solution:
P Q P Q (F87B)16 = (1925)2
1925 × 8 = (15400)2 = 2’s complement of (15400)2
T T T = (C3D8)16
T F T Ans. (a)
F T F
51. The Boolean expression for the output f of the mul-
F F T tiplexer shown below is
Solution:
R P + Q + Q + R + P +P
R++Q
Q++Q
R+ R + P + R + Q + R
R = (P + Q + Q + R) ⋅ (P=+(PR +
+QQ+
+QR)+ R) ⋅ (P + R + Q +
f
R = )(P Q + QR)(P R + QR)
= (P Q + QR)(P R + QR
R = P QP
= P QP R + P QQR + QRP R+R QRQR
+ P QQR + QRP R + Q
=+
= P QR + P QR + P QR P QR + P QR + P QR + QR
= P QR + QR = P QR + QR
= QR(P + 1) = QR = =
Q QR
+ R(P + 1) = QR = Q + R
P Q
Ans. (a)
(a) P ⊕ Q ⊕ R (b) P ⊕ Q ⊕ R 53. In the sequential circuit shown below, if the initial
(c) P ⊕ Q ⊕ R (d) P + Q + R value of the output Q1Q0 is 00, what are the next
four values of Q1Q0?
(GATE 2010: 1 Mark)
Solution:
1 T Q T Q
P Q Input Output Clock
0 0 R P′Q′R
0 1 R′ P′QR′
Q0 Q1
1 0 R′ PQ′R′ (a) 11,10,01,00 (b) 10,11,01,00
1 1 R PQR (c) 10,00,01,11 (d) 11,10,00,01
(GATE 2010: 2 Marks)
P′Q′R + P′QR′ + PQ′R′ + PQR = P ⊕ Q ⊕ R Solution:
Ans. (b)
Clock Q1 Q0
52. What is the Boolean expression for the output f of 1 0 0
the combinational logic circuit of NOR gates given 2 1 1
below?
3 1 0
P 4 0 1
Q 5 0 0
Ans. (a)
Q 54. Which one of the following circuits is NOT equiva-
R lent to a 2-input XNOR (exclusive NOR) gate?
f (a) (b)
P
R
(c) (d)
Q
R
(a) Q + R (b) P + Q
(GATE 2011: 1 Mark)
(c) P + R (d) P + Q + R
Solution: Option (d) does not show XNOR gate
(GATE 2010: 2 Marks) functionality.
Ans. (d)
55. The simplified SOP (Sum of Product) form of the 57. If at some instance prior to the occurrence of the
Boolean expression clock edge, P, Q and R have a value 0, 1 and 0,
(P + Q + R) ⋅ (P + Q + R) ⋅ (P + Q + R) is
respectively, what shall be the value of PQR after
the clock edge?
(a) (P ⋅ Q + R) (b) (P + Q ⋅ R) (a) 000 (b) 001
(c) (P ⋅ Q + R) (d) (P ⋅ Q + R) (c) 010 (d) 011
(GATE 2011: 2 Marks)
(GATE 2011: 1 Mark)
Solution: Solution:
0 0 1 1 1 1 0 1 0 0 1 0
2 0 1 1 0 1 1
1 0 0 0 0
3 1 0 0 1 0 0
(c) Fraction bits of 100…000 and exponent value Solution: Smallest integer for 2’s complement is
of 0 −2n− 1
(d) No exact representation So here n = 8, −28 − 1 = −128
Ans. (b)
(GATE 2012: 1 Mark)
63. In the following truth table, V = 1 if and only if
Solution: Binary representation of 0.5 is 0.1000,
which can be written as 1.000 × 2−1
the input is valid.
Here, exponent = −1 and fraction bits are 000…000.
Ans. (b) Inputs Outputs
61. What is the minimal form of the Karnaugh map D0 D1 D2 D3 X0 X1 V
shown below? Assume that X denotes a don’t care 0 0 0 0 x x 0
term.
1 0 0 0 0 0 1
ab x 1 0 0 0 1 1
cd 00 01 11 10 x x 1 0 1 0 1
x x x 1 1 1 1
00 1 X X 1
PRACTICE EXERCISES
Set 1
1. Find the value of x in (257) base 10 = (41) base x? (a) I-A, II-C, III-B (b) I-B, II-A, III-B
(c) I-A, II-B, III-A (d) I-A, II-C, III-B
(a) 61 (b) 64
(c) 127 (d) 256 10. Consider the following circuit:
2. What is the result of 01A2 + 001A = ? f1
f2
(a) 01BC (b) 01CB f5
f3
(c) 01CC (d) 01DC
3. The simplified function of Boolean function Given: f1(x, y, z) = ∑m(1, 2, 6, 7), f3(x, y, z) =
F(W, X, Y, Z) = Σ(0, 1, 2, 5, 8, 9, 10) is ∑m(0, 3), f5(x, y, z) = ∑m(0, 1, 3, 6)
(a) (W ′ + X′) (Y ′ + Z′) (X′ + Z). How many different minterms combinations are
(b) (W + X) (Y ′ + Z′) (X′ + Z′). possible for f2?
(c) (W ′ + X′) (Y + Z) (X + Z). (a) 15 (b) 12
(d) (W ′ + X) (Y + Z′) (X′ + Z′). (c) 8 (d) 6
4. What is the hexadecimal representation of (1356)
base 8? 11. The two numbers represented in signed 2’s comple-
ment form is A = 11101101 and B = 11100110.
(a) 2FE (b) 2DE
If B is subtracted from A, the value obtained in
(c) 2EE (d) EEE
signed 2’s complement form is
5. How many 3 × 8 decoders are needed to construct (a) 10000111 (b) 00000111
4 × 16 decoder? (c) 10001001 (d) 11001001
(a) 2 (b) 4
12. Simplify the Boolean function
(c) 3 (d) 24/64
F(w, x, y, z) = ∑(1,3,10) + ∑d(0,2,8,12)
6. If 73 (in base x number system) is equal to 54 (in
base y number system), the possible values of x and (a) F = w′x′+ x′z (b) F = wx′ + x′z′
y are (c) F = w′x + x′z′ (d) F = w′x′ + x′z′
(a) 11,8 (b) 8,13 13. Which of the following is the complement of func-
(c) 11,11 (d) 8,11 tion F = x(y′z′ + yz)
7. Total number of 8 × 1 MUX required for imple- (a) x + yz′ + y′z′ (b) x′ + yz′ + y′z
mentation of 256 × 1 MUX is (c) x′ + y′z′ + y′z (d) x′ + y′z′ + y′z′
C. ∑m(1,2,5,6)
III. xy + yz + zx (a) 0 (b) 2
(c) 1 (d) 01
16. What is the maximum value that we can represent 24. Which FF has its output clearly defined on all the
using N bits? combinations inputs?
(a) 2n (b) 2n− 1 (a) SR FF (b) D FF
(c) 2n − 2 (d) 2n − 1 (c) JK FF (d) T FF
17. The number of bits required to represent 128 dif- 25. The main difference between JK and RS flip-flop
ferent numbers is is that
(a) 5. (b) 7. (a) JK flip-flop do not need a clock pulse whereas
(c) 6. (d) 8. RS flip-flop requires the same.
(b) The output Q is connected to J and K is
18. Consider the following truth table, where A, B, C
grounded whereas it is opposite in R and S.
represents the inputs and F the output function
(c) JK flip-flop accepts both inputs as 1.
(d) JK flip-flop is acronym of Johnson-Keith’s
A B C F multivibrator and RS of reset-set multivibrator.
0 0 0 0
26. AND gate provides a LOW output .
0 0 1 0
0 1 0 0 (a) when any one input is LOW
0 1 1 1 (b) always
1 0 0 0 (c) when both the inputs are HIGH
1 0 1 0 (d) when both the inputs are LOW
1 1 0 1 27. How many pins does the 4049 IC have?
1 1 1 0
(a) 22 (b) 16
(a) B′(A + C ′)(A′ + C ′) (b) B(A + C ′)(A′ + C ) (c) 20 (d) 40
(c) B′(A′ + C ′)(A′ + C ) (d) B(A + C )(A′ + C ′)
28. Which of the following expressions is in the sum-of-
19. Which of the following expressions is equivalent to products (SOP) form?
A ⊕ B ⊕ C? (a) A′B + C ′D ′ (b) AB ′(CD)
(a) (A + B + C)(A′ + B′ + C ′) (c) (A + B ′)(C + D) (d) (A)B ′(C + D ′)
(b) (A + B + C)(A′ + B′ + C) 29. The significance of MSD in a decimal number
(c) ABC + A′(B ⊕C) + B′(A ⊕ C) system is
(d) (A′ + B + C)(A′ + B′ + C ′)
(a) the middle digit of a stream of numbers.
20. A half adder is also known as (b) the digit to the right of the decimal point.
(a) NOR circuit. (b) XNOR circuit. (c) the last digit on the right.
(c) NAND circuit. (d) XOR circuit. (d) the digit with the most weight.
21. Which of the following is minimum error code? 30. The Generic Array Logic (GAL) device is .
32. An XNOR gate gives a HIGH output with the fol- 43. What combination of the inputs a JK flip-flop
lowing input combination toggles?
(a) A = LOW, B = HIGH (a) J = 0, K = 0 (b) J = 0, K = 1
(b) A = HIGH, B = LOW (c) J = 1, K = 0 (d) J = 1, K = 1
(c) A = LOW, B = LOW
(d) It is not possible to get a HIGH output in this gate 44. In a 6-bit Johnson counter sequence, the total
number of states or bit patterns is _________.
33. A shift register that has a parallel input, or a bidi-
rectional serial load and having internal shift fea-
tures, can be termed as Set 2
(a) Tristate device.
(b) Parallel-in shift-out (PISO) register. 1. The simultaneous equations on the Boolean vari-
(c) Universal shift register. ables x, y, z and w, are
(d) Bidirectional shift register. x+y+z=1
xy = 0
34. Which of the following logic families has the least
xz + w = 1
propagation delay?
xy + zw = 0
(a) 54LYY (b) BiCMOS
(c) ECL (d) 74SXX have the following solution for x, y, z and w,
respectively.
35. Convert binary 111111110010 into hexadecimal.
(a) 0100 (b) 1101
(a) (EF2)16 (b) (FF2)16
(c) 1011 (d) 1000
(c) (FE2)16 (d) (FD2)16
36. Convert the binary number (1001.0010)2 to decimal. 2. Given the following Karnaugh map, which one
of the following represents the minimal sum-of-
(a) 90.125 (b) 9.125
product of the map?
(c) 91.25 (d) 12.125
37. The alphanumeric code widely used for input and wx
yz 00 01 11 10
output to/from a computer?
(a) Biquinary Code (b) ASCII 00 0 X 0 X
(c) ANSI Code (d) EBCDIC 01 X 1 X 1
38. (8B3F)16 = (?)2
11 0 X 1 0
(a) 111101101111 (b) 01110101111
(c) 1011001111101111 (d) 1000101100111111 10 0 1 X 0
39. Solving -11 + (-2) will yield which 2’s comple-
(a) xy + y′z
ment answer?
(b) wx′y′ + xy + xz
(a) 10011001 (b) 1000 1001 (c) w ′x + y′z + xy
(c) 1111 0011 (d) 1110 1001 (d) xz + y
40. Dividing (1100010)2 by (0101)2 yields (X)10. The
value of X is _________. 3. Minimum SOP for f (w, x, y, z) shown in Karnaugh-
map below is
41. The solution to (0110)16 + (10010)16 = (?)16
wx
(a) 1012016 (b) 1112016 yz 00 01 11 10
(c) 1002016 (d) 00B016
00 0 1 1 0
42. Which of the following is correct for full adders?
01 X 0 0 1
(a) Full adders can be used for multiplication also.
(b) Full adders are used to make half adders and 11 X 0 0 1
quad adders.
(c) Full adders have three inputs including a carry 10 0 1 1 X
input.
(d) In a parallel full adder, the first stage may be (a) xz + y′z (b) xz ′ + zx ′
a half adder. (c) x′y + zx ′ (d) None of the above
4. Consider the following logic circuit whose inputs 8. Which of the following sets of component(s) is/
are functions f1, f2, f3 and output f. are sufficient to implement any arbitrary Boolean
function?
f1(x, y, z)
f2(x, y, z) f(x, y, z) (a) XOR gates, NOT gates
(b) 2 to 1 Multiplexors
(c) AND gates, XOR gates
(d) Three-input gates that output (A⋅B) + C for
f3(x, y, z) = ? the inputs A, B and C
Given that f1(x, y, z) = ∑(0, 1, 3, 5); f2(x, y, z) = 9. Which function does NOT implement the Karnaugh
∑(6, 7) and f3(x, y, z) = ∑(1, 4, 5) then f is map given in the following figure?
10 1 1 x I0
I1
y I2
(a) BD + BD 4 to 1 f(x,y,z) ?
I3 MUX
(b) (B + C + D)(B + C + D) A1
(c) (B + D)(B + D) z A0
EM
(d) (B + D)(B + D)
(c) arithmetic left shift. 28. A flip-flop has a 4-ns delay from the time the clock
(d) converting a signed integer from one size to edge occurs to the time the output is complemented
another. in a 10-binary ripple counter. The ripple counter
operates reliably at which maximum frequency?
25. The state of a 12-bit register is 100010010111. It is
represented in BCD, what is its content? (a) 25 MHz (b) 30 MHz
(c) 32 MHz (d) 40 MHz
(a) 2199 (b) 3423
(c) 564 (d) 897 29. How many parity check bits must be included with
26. Simplification of given Boolean expressions = the data word to achieve single-error correction
{(a + b′ + c′)(a′ + c′)′′} to a minimum number of when data word contains 16 bits?
literals is (a) 4 Bits (b) 5 Bits
(a) (c′ + a′b′) (b) (a′ + b′c′) (c) 6 Bits (d) 7 Bits
(c) (b′ + a′c′) (d) (a′ + b′ + c′)
30. How many 32K × 8 RAM chips are needed to
27. Simplification of the following Boolean function provide a memory capacity of 256K bytes?
{F(A, B, C, D) = ∑(3, 7, 11, 13, 14, 15)} using
(a) 4 (b) 5
K-maps:
(c) 6 (d) 8
(a) CD + ABC + BCD (b) CD + ABC + ABD
(c) CD + ACD + ABD (d) CD + ABC + ACD
Set 1
1. (b) 9. (b) 17. (b) 25. (c) 33. (c) 41. (a)
2. (a) 10. (a) 18. (d) 26. (a) 34. (c) 42. (d)
3. (a) 11. (b) 19. (c) 27. (b) 35. (b) 43. (d)
4. (c) 12. (d) 20. (c) 28. (a) 36. (b) 44. (12)
5. (a) 13. (b) 21. (b) 29. (d) 37. (b)
6. (d) 14. (c) 22. (c) 30. (b) 38. (d)
7. (a) 15. (b) 23. (d) 31. (a) 39. (c)
8. (d) 16. (d) 24. (c) 32. (c) 40. (3)
Set 2
f1(x,y,z) f (x,y,z) 00 0 X 0 1
f2(x,y,z)
01 0 X 1 1
11 1 1 1 1
f3(x,y,z) 10 0 X 0 0
wz
5. (c) f(f(x + y, y), z)= f((x + y)′ + y, z) = f(x′y′ + y, z) xy 00 01 11 10
= f(x′ + y, z) = (x′ + y)′ + z = xy′ + z 0 X 0 0
00
01 0 X 1 1
6. (c)
A
B 11 1 1 1 1
C
Y 10 0 X 0 0
D
E
f(x, y, w, z) = y(x + w)
Therefore, Y = ABC ⋅ DE
Option (c) is incorrect.
10. (a) 14. (d) To store the table for multiplication of two
8-bit unsigned integers, size of ROM required is
a
22N× 2N = 216× 16 = 64K×16
1
g a
2:1
MUX 2:1 15. (a)
0 b MUX f
c Booth recording of a multiplier are
(a) 1 0 1 0 1 0 …… 1 0 1 0
b c ⇓
x1 −1 +1 −1 +1 −1 +1……−1 +1 −1 0
x2 (b) 1 0 0 0 0 0 ……0 0 0 1
⇓
The output g is g = 1 x1′ + 0 x1 = x1′ −1 + 0 0 0 0 0……−0 0 +1 −1
The output f is (c) 1 1 1 1 1 1 …… 1 1 1 1
f = gx2′ + x1x2
⇓
f = x1′x2′ + x1x2
0 0 0 0 0 0…… 0 0 0 −1
11. (a) (d) 0 1 1 1 1 1 …… 1 1 1 0
= EN(A1A0 I 0 + A1A0 I1 + A1A0 I2 + A1A0 I3 ) ⇓
1 0 0 0 0 0 …… 0 0 −1 0
= z ′(xyz ′ + yzx + 0 + y ′zy ′)
= xyz ′ + xyzz ′ + 0 ⋅ z ′ + z ′ ⋅ zy ′ Therefore, option (a) gives the worst performance
because it contains maximum number of 1’s.
= xyz ′ + 0 + 0 + 0 = xyz ′
(d) When the radix is 6 Therefore, the decimal value 0.25 is equivalent to
binary 0.01.
(224)6 = (88)10 ≈ (9.38)10 and (13)6 = (9)10.
24. (d) Sign extension is the operation for increasing
Therefore, (224)6 ≈ (13)6 the number of bits of a binary number while pre-
Option (d) is not correct. serving the number’s sign (positive/negative) and
value.
19. (d) Zero has two representations in
25. (d)
Sign magnitude as (i) 0 0 0 0, (ii) 1 0 0 0
26. (a) (a + b′ + c′)(a′ + c′)
1’s Complement as (i) 0 0 0 0, (ii) 1 1 1 1
⇒ aa′ + ac′ + a′b′ + b′c′ + a′c′ + c′c′
20. (c) Positive 2’s complement numbers are repre- ⇒ c′ + b′(a′ + c′)
sented as same simple binary. So +43 in binary is ⇒ c′ + a′b′
0 0 1 0 1 0 1 1. 27. (b)
21. (a) Given that A = 2.0 × 10 , B = −2.0 × 10 ,
30 30
CD
C = 1.0 00 01 11 10
AB
m0 m1 m8 m2
(i) X = A + B = 2.0 × 1030 + (−2.0 × 1030) = 0.0 00
1
m4 m5 m7 m6
(ii) Y = A + C = 2.0 × 1030 + (1.0) 1
01
(iii) Y = X + C = 0.0 + 1.0 = 1.0 m12 m13 m15 m14
11 1 1 1 ABC
(iv) Y = Y + B = 2 .0 × 1030 + 1.0 + (−2.0 × m5 m9 m11 m10
1030) = 1.0 10 1
6
Number of questions
4 Marks 1
Marks 2
3 Total number of questions
2
0
2015 2014 2013 2012 2011 2010
Year Concepts
2015 Cache memory, Memory, Pipelining, Main memory, Pipeline, Anti-dependency,
Instruction format, Byte Addressable memory, Pipeline, Three-address code
2014 Memory access, pipelining
2013 Cache memory, hard disk, pipelining, registers, micro-operations
2012 Pipelining, ROM, cache, file system
2011 Cache memory, interrupts
2010 Memory, pipelining, registers, cache memory
Syllabus: Machine instructions and addressing modes, ALU and data path, CPU control design, memory interface,
I/O interface (interrupt and DMA mode), instruction pipelining, cache and main memory, secondary storage
Computer organization deals with the operation AR Address Holds address for
and interconnection of the various hardware components. register memory
(Continued)
Table 2.1 | Continued When the system consists of multiple frequent cases,
where i is the number of frequent cases:
Register Register Function
−1
Symbol Name
( ) Fi
Soverall = 1 − ∑ Fi + ∑
S
IR Instruction Holds an instruction
register that is to be executed
Problem 2.1: Consider a hypothetical processor used
PC Program Holds address of
in mathematical model simulation. It consists of two
counter instruction to be
functional units, floating point and integer. The float-
executed next
ing point is enhanced then it runs two times faster,
TR Temporary Holds temporary data but only 10% of the instructions are floating point.
register if required What is the speed up?
INPR Input register Holds input character
Solution: Here S = 2, F = 0.1
OUTR Output register Holds output character
−1
0.1
Soverall = (1 − 0.1) +
2
= 1.052
2.2.2 Quantitative Principles to Design
High-Performance Processor
Amdahl’s law focused on performance gain after enhanc- 2.3 MACHINE INSTRUCTIONS AND
ing the system. The performance gain is denoted by ADDRESSING MODES
Soverall and ET stands for execution time.
Performance of the system with enhancement
Soverall = Machine instruction is an individual machine code. The
Performance of the system without enhancement complete set of all machine codes recognized by a partic-
1 ETnew ular processor makes its Instruction Set. Instructions can
Soverall = (2.1) be grouped according to the function they perform. The
1 ETold
number of ways by which arguments for these machine
ETold instructions can be specified constitutes the addressing
Soverall =
ETnew modes for a processor.
After enhancement, the system consists of two portions:
unenhanced and enhanced portion. 2.3.1 Machine Instructions
2. Two-address instruction: Each address field can 1. provide programming flexibility to users through
specify either a processor register or a memory word. use of pointers to memory, counter for loop control,
OV R1, A (R1 ← M[A]);
Example: M data indexing and program relocation.
MUL R1, R2 (R1 ← R1*R2) 2. reduce the size of the addressing field of the
instruction.
3. One-address instruction: It used an implied
accumulator (AC) register for all data manipula- Let us suppose [x] means contents at location x for all the
tion. The other operand is in register or memory. addressing modes.
5. Auto-increment or Auto-decrement mode: 10. Base register addressing mode: In this mode,
This is similar to register indirect mode except the the effective address of an operand is obtained by
register containing effective address is incremented adding the content of a base register to the address
or decremented after (or before) its value is used to part of the instruction. This is somewhat similar to
access memory. the indexed addressing mode except that the base
6. Direct address mode: In this mode, the effec- register stores base or beginning address instead of
tive address of an operand is equal to the address an index register. It is used for program relocation.
part of the instruction. Example: ADD A instruc-
tion adds content of memory cell A to accumula-
tor, that is, ACC = [ACC] + M[A]. Problem 2.3: A two-word instruction LOAD is stored
Instruction with direct address mode at location 300 with its address field in the next loca-
tion. The address field has value 600 and value stored
at 600 is 500 and at 500 is 650. The words stored
Opcode Memory address
at 900, 901 and 902 are 400, 401 and 402, respec-
Memory tively. A processor register R contains the number
800 and index register has value 100. Evaluate the
effective address and operand if addressing mode of
the instruction is as follows:
Operand
1. Direct 4. Immediate
2. Indirect 5. Register indirect
7. Indirect address mode: In this mode, memory 3. Relative 6. Index
address specified by address field contains the Solution: Memory layout is as follows
address of (pointer to) the operand. Example:
ADD @ A will add the contents of the memory cell 300 LOAD
A, that is, ACC = [ACC] + M[M[A]]. 301 600
Instruction with indirect address mode
500 650
Opcode Memory address
Memory
600 500
Pointer to operand
700 900
800 700
Operand
900 400
901 401
8. Relative address mode: In this mode, the effective 902 402
address of an operand is obtained by adding the con-
tent of a program counter to the address part of the Addressing Effective Operand
instruction. The address part of the instruction can be Mode Address
either positive or negative represented in 2’s comple-
ment. The result obtained after adding the content of Direct 600 500
the program counter to the address field produces an Indirect 500 650
effective address whose position in memory is relative Relative 902 402
to the address of the next instruction. Immediate 301 600
9. Index address mode: In this mode, the effective Register indirect 800 700
address of an operand is obtained by adding the Index 700 900
content of an index register to the address part of
the instruction. The index register is a special CPU
register that stores an index value and the address
field of the instruction stores the base address of a Problem 2.4: A relative mode branch type instruc-
data array in the memory. The distance between tion is stored in memory at an address equivalent to
the base address and the address of the operand is decimal 600 and the branch is made to an address
the index value that is stored in the index register. equivalent to decimal 400. What is the value of the
The index register can be incremented to facilitate relative address field of the instruction (in decimal)?
access to consecutive operands stored in arrays
Solution: Relative address = 400 − 601 = −201
using the same instruction.
2.4 ARITHMETIC LOGIC UNIT By controlling the output Y of multiplexers with two
selection inputs S1 and S0 and Cin either 0 or 1, we can
generate the eight arithmetic micro-operations (Table 2.2).
Arithmetic logic unit (ALU) is a combinational circuit
that performs all arithmetic and logic operations so that 2.4.2 Logic Micro-Operations
the entire register transfer operation from the source reg-
isters through the ALU and into the destination register Logic micro-operations such as AND, OR, Exclusive OR,
can be performed during one clock pulse period. etc., consider each bit of register separately and specify
binary operations for strings of bits (Table 2.3).
2.4.1 Arithmetic Micro-Operations
Table 2.3 | Types of micro-operations
The basic arithmetic micro-operations such as addition,
subtraction, increment, decrement and shift are performed Micro-operation Name
F←0
on numeric data stored in registers. The basic component
Clear
of arithmetic is parallel binary adder, and by controlling
the input to adder, different micro-operations can be F←A∧B AND
realized. Figure 2.1 depicts a 2-bit arithmetic circuit which F ←A∧B
includes two full-adder circuits and two multiplexers for
choosing different arithmetic micro-operations. There are F←A Transfer A
two 2-bit input numbers A and B and 2-bit output D. The F ←A∧B
F←B
two inputs from A go directly to X inputs of full adder.
Transfer B
The output of multiplexer goes to input Y of full adder.
F←A⊕B Exclusive OR
Cin F ← A ∨B OR
A0
S1
X0 C0 F ←A∨B NOR
S1 FA D0
S0 Y0 C1 F ← A⊕B Exclusive NOR
0 4×1
S0
B0 1 MUX F ←B Complement B
2
3
A1 F ←A∨B
X1 C1
S1 FA D1 F ←A Complement A
0 4×1
S0 Y1 C2
B1 1 MUX Cout F ←A∨B
2
3
0 1 F ←A∧B NAND
Figure 2.1 | A 2-bit arithmetic circuit. F ← all 1’s Set to all 1’s
1 1 0 0 At
1 0 1 0 B
At+1 (A ¬ A Å B)
External output
0 1 1 0
Figure 2.2 | General register organization.
6. Insert operation: It is used to insert a specific bit
pattern into A register, leaving the other bit posi- select lines SELA and SELB from multiplexers A
tions unchanged. This is accomplished by two sub- and B select one of the input and feed to ALU.
operations: masking operation to clear the desired bit OPR specifies one of the possible operation codes
positions, followed by OR operation to introduce the that ALU will perform on the data inputs and the
new bits into the desired positions. Suppose you wanted output is transferred either to one of the registers
to introduce 10 into the low order two bits of A: using 2 × 4 decoder or to the external output say
1101 A (Original) and 1110 A (Desired) memory. The control word (Fig. 2.3) for the two-
operand instruction is as follows:
1 1 0 1 A (Original)
1 1 0 0 Mask 2 bits 2 bits 2 bits 4 bits
1 1 0 0 A (Intermediate)
SELA SELB SELD OPR
0 0 1 0 Added bits
1 1 1 0 A (Desired) Figure 2.3 | A control word.
3. Stack organization: Stack may consist of number repeated continuously for a complete program and is
of registers or a part of main memory in which data known as the fetch-execute cycle (Fig. 2.4). The fol-
items are stored in consecutive locations that are lowing steps are performed for executing an instruction:
accessed by LIFO (last in, first out) mechanism. As
there is limited number of registers, a part of memory Start
is implemented as stack for storage and retrieval of
intermediate data. Stack pointer (SP) keeps a track
of the top item of a stack. The process of inserting Load PC contents
a new item onto a stack is known as push accom- to MAR
plished by first incrementing stack pointer and then
inserting an item from the data register. Increment PC to
SP ← SP + 1 point to next
instruction
M[SP] ← DR
The process of removing an item from the top of a
Load the instruction
stack is known as pop performed by first transfer-
stored at MAR to IR
IR ← M[MAR]
ring data into DR and then decrementing SP.
DR ← M[SP]
SP ← SP − 1 Decode the
instruction
Problem 2.5: A system has CPU organized in the
form of general register organization consisting of 16 Load any data
registers, each storing 32-bit data. Assume the ALU required into MDR
has 35 operations.
(a) How many multiplexers are there in A bus and B
Check
bus, and what is the size of each multiplexer? Yes Set PC to value
for jump
(b) How many selection inputs are needed for MUX A from jump inset
instruction
and MUX B?
(c) How many inputs and outputs are there in a decoder?
(d) How many inputs and outputs are there in ALU No
for data, including input and output carries?
(e) Formulate a control word for the system. Execute the
instruction
Solution:
(a) 32 Multiplexers, each of size 16 × 1.
(b) 4 Inputs each, to select one of 16 registers.
(c) 4 to 16 − Line decoder Check for No
(d) 32 + 32 + 1 = 65 data input lines interrupts
(e) 32 + 1 = 33 data output lines
4 bits 4 bits 4 bits 6 bits Yes
SELA SELB SELD OPR Service the
interrupt
A CPU generally executes one instruction at a time 1. Fetching the instruction: The next instruction
sequentially and a sequence of such instructions is is fetched from the memory address that is saved in
known as a program. The CPU executes the instructions the program counter, and memory content fetched
that reside in the main memory. In order to execute is stored in instruction register (IR). The program
an instruction, the CPU has to fetch the instruction counter then points to the next instruction that
first from the main memory into one of its registers. will be read in the next cycle.
It then decodes the instruction, that is, it decides what 2. Decode the instruction: During this cycle, the
the instruction intended to do, fetch operands required instruction inside the IR gets interpreted by the
and finally executes the instruction. This process is decoder.
3. Operand fetch: In case of a direct or indirect decoder generates a separate control line for each
memory instruction, the execution begins in the step in the control sequence. The encoder gets
next clock cycle. If the instruction has an indirect its input signal from the decoder, step decoder,
address, the effective address of the operand is read external input and condition codes and generates
from the main memory, and the required data is individual control signals. It is faster and more
fetched from the memory into memory data regis- efficient but less flexible and is difficult to add
ters. If the instruction has direct address, nothing new feature or correct mistakes in original design.
is done at this clock cycle.
4. Execute the instruction: The control unit of Clock Control step Reset
the CPU passes the instruction decoded by decoder counter
as a sequence of control signals to the different
functional units of the CPU to execute the tasks
required by the instruction such as reading values Step decoder
from registers or input devices, performing mathe-
T1 T2 Tn
matical or logic micro-operations by ALU, and writ- I1
ing the result back to a register or main memory. External
I2
inputs
Instruction
2.5.2 CPU Data Path IR Encoder
decoder
In Condition
CPU contains data paths that are responsible for routing codes
data between the functional units of a computer. The
following are the different data path structures available End
for routing: Control
1. Single bus structure: In this architecture, all CPU signals
Figure 2.5 | Block diagram of hardwired control unit.
registers are connected to the same bus. Data can be
transferred either between CPU registers or between
CPU register and ALU at a given clock pulse. The 2. Micro-programmed control: Control signals
speed of operation is slow as only one operand can be are generated by using programming known as
transferred in one clock cycle and addition operation micro-programs that constitutes micro-instructions
(R1 ← R2 + R3) occurs in three clock cycles. (control word) (Fig. 2.6). Memory that is part
2. Two bus structure: All general purpose CPU
registers are connected to both buses say bus A and IR External
Sequences inputs
bus B; but special purpose registers are divided into (starting and branch
two groups, say group 1 connecting bus A to pro- address generator) Condition
gram counter and one input of ALU and group 2 codes
connecting bus B to MDR (Memory Data Register)
and other input of ALU. The two operands are Control address
transferred to ALU in 1 clock cycle and the addition Clock
register
operation (R1 ← R2 + R3) occurs in 2 clock cycles. Address
3. Three bus structure: The performance can be
further be improved by using three buses such that Control
Read
addition operation (R1 ← R2 + R3) can occur in command memory
one clock cycle.
Control word
of CPU is known as control memory and stores Table 2.4 | RISC versus CISC
micro-instructions. The micro-program sequencer
generates the address of micro-instruction accord- RISC (Reduced CISC (Complex
ing to instruction stored in instruction register. Instruction Set Instruction Set
The address of micro-instruction to be executed Computers) Computers)
is available in content addressable register. Micro- Rich register set Less number of registers
program sequencer issues read command to read
micro-instruction from control memory into micro- Supports less addressing Supports more number of
instruction register which on execution generates modes addressing modes
control signals for various parts of a processor. This Supports fixed length Supports variable length
control unit design is more flexible to accommodate instruction instruction
new features and less error prone but quite slower Successful pipeline with Unsuccessful pipeline
than the hardwired unit. one instruction per cycle
The format of the control word is Example: ARM, Example: Pentium
Motorola processors
Branch Flag Control Control memory
condition signal address
On the basis of the type of control word supported, it is 2.6 I/O INTERFACE (INTERRUPT
divided into two types: AND DMA MODE)
1. Horizontal micro-programmed control unit:
In this design, the control signals are represented in I/O interface bridges the differences between CPU and
the form of 1 bit per control signal and it supports peripheral devices and provides a method for transfer-
longer control word. ring information between internal storage and external
2. Vertical micro-programmed control unit: In I/O devices. There are the following three modes of I/O
this design, the control signal is represented by using transfer:
encoding format.
1. Programmed I/O: The I/O device does not
have direct access to memory. It requires execution
Problem 2.6: Consider a control unit which has 1024 of several instructions by the CPU and the CPU
control word memory; it supports 48 control signals has to wait for the I/O device to be ready for either
and 8 flag conditions. What is the size of the control reception or transmission of data.
word in bits and control memory in bytes? 2. Interrupt initiated I/O: In this, instead of
Solution: waiting, the control is transferred from a currently
running program to another service program as a
(a) Using horizontal programmed control unit result of an external/internal generated request.
0 bits 3 bits 48 bits 10 bits ••Hardware interrupts: These interrupts are
Branch Flag Control Control present in the hardware pins.
condition signal memory ••Software interrupts: These are the instruc-
tions used in the program whenever the required
Size of control word = 61 bits functionality is needed.
Control memory = (1024 × 61)/8 = 128 × 61 bytes ••Maskable interrupts: These interrupts may
(b) Using vertical programmed control unit be enabled or disabled explicitly.
••Non-maskable interrupts: These interrupts
0 bits 3 bits 48 bits 10 bits are always there in the enable state. We cannot
Branch Flag Control Control disable them by explicit conditions (flags).
condition signal memory ••Vectored interrupts: These interrupts are
log 48 ~ 6 bits associated with the static vector address.
••Non-vectored interrupts: These interrupts
Size of control word = 19 bits
Control memory = (1024 × 19)/8 = 128 × 19 bytes are associated with dynamic vector address.
••External interrupts: These interrupts are
generated by external devices such as I/O.
2.5.4 RISC versus CISC Processors ••Internal interrupts: These devices are gener-
ated by the internal components of the processor
The differences between reduced and complex instruc- such as temperature sensor, power failure, error
tion set computers is given in Table 2.4 instruction, etc.
••Synchronous interrupts: These interrupts block in memory is given by the address register,
are controlled by the fixed time interval. All and the length of the bytes to transfer is given by
the interval interrupts are called as synchronous the word count register. The controller decrements
interrupt. a word counter each time it moves a data byte.
••Asynchronous interrupts: These interrupts
There are several modes of operation of DMA:
are initiated based on the feedback of previ-
ous instructions. All the external interrupts are ••Burst or block transfer mode: In this mode,
called as asynchronous interrupt. the entire block of data is transferred once the
3. Direct memory access (DMA): It is one of DMA controller is granted access to the system
several methods for coordinating the data transfers bus by the CPU. The bytes of data in the block are
between an I/O device and the core processing unit transferred before releasing control of the system
or memory in a computer. It refers to transfer of buses back to the CPU. The only disadvantage of
data directly between a fast storage device and this mode is that it renders the CPU inactive for
memory bypassing CPU because of its limited some long periods of time.
speed. DMA provides a significant improvement ••Cycle stealing mode: In this mode, the DMA con-
in terms of latency and throughput as it allows troller obtains access to the system buses like burst
the I/O device to access the memory directly, mode; but after one byte of data transfer, the control
without using the processor. There are certain of the system bus is released back to the CPU via
advantages of using DMA for data transfer: BG. It is then continually requested again via BR,
••DMA saves processor’s MIPS as the core can transferring one byte of data per request, until the
operate in parallel. entire block of data has been transferred. This mode
••DMA saves power because it requires less cir- is suitable for the systems in which the CPU cannot
cuitry than the processor to transfer data. be disabled for the considerable length of time as in
••DMA has no modulo block size restrictions. burst transfer modes such as for controllers moni-
Direct memory access (DMA) controller takes over toring the data in real time. The advantage is that
the control of buses to manage the transfer directly CPU is not idled for as long as in burst mode, but
between the I/O device and memory. Bus request the data block is not transferred as quickly.
••Transparent mode: It is the slowest yet more ef-
(BR) and Bus grant (BG) signals are used by the
DMA controller to request the CPU to relinquish ficient data transfer mode in terms of overall system
control of the buses and get the control of system performance. The DMA controller transfers data only
buses (Fig. 2.7). The DMA controller consists of when the CPU is busy in performing operations that
3 different registers: an address register, a control do not use the system buses. So, the CPU never stops
register and a word counter register. To transfer a executing its programs but the biggest disadvantage
block of data between an I/O device and memory, is complex hardware circuitry that needs to deter-
the controller stores initial values in the address mine when the CPU is not using the system buses.
register. The DMA channel then transfers the A DMA read transfers data from the memory to
block of information from or to memory according the I/O device, while DMA write transfers data
to the control register. The starting address of the from an I/O device to memory. The functional
Address behaviour of a DMA transfer outlined in Fig. 2.8:
bus ••TheCPU transmits the following information to a
Data DMA controller:
bus Data bus (a) beginning address in memory which is stored in
Address bus
buffer buffer address register in DMA controller.
DMA (b) Number of words to transfer which is stored in
select DS
Register Address word count register in DMA Controller.
select RS register (c) direction (memory-to-I/O device or I/O device-
Read RD to-memory), port ID, DMA mode of transfer
Internal bus
Interrupt
BG Random Access
CPU
Memory (RAM)
BR
RD WR Address Data RD WR Address Data
Read control
Write control
Address
select Address bus
Data bus
RD WR Address Data
DS DMA acknowledge
BG
Interrupt
Figure 2.8 | DMA controller interconnection with memory, CPU and I/O devices.
••When the DMA controller accesses memory, it execution time of a set of instructions and there is no
synchronizes this memory request with an idle need to wait of the most part of the processor circuits for
period of the processor, thus disabling the pro- the other parts of the processor to complete their part
cessor, or requesting a halt of the processor, and of execution. Pipeline speed is limited by the slowest
awaits an acknowledgement. pipeline stage.
••After the completion of the block transfer, the DMA Throughput of a processor is the rate at which opera-
controller either raises an interrupt request if the tions get executed. Latency is the amount of time that a
interrupts are enabled or indicates the completion single operation takes to execute. In an unpipelined com-
in its status register and the processor recognizes puter, throughput = 1/latency, as each operation exe-
I/O completion (either by interrupt signal or by cutes by itself and for pipelined computer, throughput
reading the status register) and gets its system > 1/latency, since execution of instruction is overlapped.
buses back and normal processing starts. The Consider a k-segment pipeline with a clock cycle time
device has to initiate a new data transfer through Tp used to execute n tasks (Fig. 2.9). An equivalent non-
DMA request signal which is again acknowledged pipelined system takes Tn time to complete each task.
by CPU through DMA acknowledge signal via The speed up of a pipelined system over a non-pipelined
DMA controller. system is given by the following relation:
n × Tn
S=
2.7 INSTRUCTION PIPELINING (k + n - 1) × Tp
One clock One clock One clock One clock One clock
cycle cycle cycle cycle cycle
Pipeline Stages
(b)
step → 1 2 3 4 5 6 7 8
Segment ↓ Fetch I1 I2 I3 I4
Decode I1 I2 I3 I4
Fetch operand I1 I2 I3 I4
Execute I1 I2 I3 I4
Write back I1 I2 I3 I4
(c)
Figure 2.9 | (a) Unpipelined processor. (b) Pipelined five-stage processor.
(c) Timing diagram of a five-stage instruction pipeline.
1. Structural hazards: These result from resource instruction refers to a result which is yet not
conflicts when the hardware cannot support been calculated, that is, in this inst2 tries to
instructions that need simultaneous execution in read a source before inst1 writes to it. This
pipeling. situation arises if the read operation by instruc-
2. Data hazards: They arise when an instruction tion takes place before write done by other in-
depends on the result of a previous instruction and struction. For example,
that result is not yet calculated. inst1: R3 <-R1 + R2
There are three situations in which data hazards inst2: R4 <-R3 + R2
can occur: The first instruction calculates a value by adding
••Read
values in registers R1 and R2 and saves the result
after write (RAW), a true dependency
in register R3, and the second instruction uses
••Write after read (WAR), an anti dependency this saved value to calculate a result for regis-
••Write after write (WAW), an output dependency ter R4. However, in a pipeline, when operands
for the second operation are fetched, the results
Consider two instructions inst1 and inst2
from the first instruction will not have been
occurring, with inst1 occurring before inst2 in
saved yet, and so there arises a data depend-
the program order.
ency. It can be said that there is a data depend-
••Readafter write (RAW): A read after write ency with instruction inst2, as it is dependent
(RAW) data hazard is a situation in which an on the completion of instruction inst1.
••Write after read (WAR): A write after 3. Control hazards: They arise from the pipelining
read (WAR) data hazard refers to a situation in of branches and other instructions that change the
which there is a problem with concurrent execu- value of PC.
tion, that is, inst2 tries to write a destination Speed up from pipelining
before it is read by inst1. This situation arises
if write operation completes first by instruction Average instruction time unpipelined
=
before the read operation takes place by other Average instruction time pipelined
instruction. For example,
Speed up from pipelining
inst1: R4 <-R1 + R3
CPI unpipelined × Clock cycle pipelined
inst2: R3 <-R1 + R2 =
CPI pipelined × Clock cycle pipelined
If a situation arises in which there is a chance
that inst2 may get completed before inst1 CPI unpipelined
Ideal CPI =
(i.e., with concurrent execution) we must note Pipeline depth
that we do not store the result of register R3
before inst1 has had a chance to fetch the Speed up from pipelining
operands. Ideal CPI × Pipeline depth × Clock cycle unpipelined
=
••Write after write (WAW): A write after CPI pipelined × Clock cycle pipelined
write (WAW) data hazard refers to a situation
in which there is a concurrent execution envi- Speed up from pipelining
ronment, that is, inst2 tries to write an oper- Ideal CPI × Pipeline depth × Clock cycle unpipelined
and before it is written by inst1.This situation
(Ideal CPI + Pipeline stall) × Clock cycle pipelined
=
arises if write operation by an instruction occurs
in the reverse order of the intended sequence. Assuming ideal CPI as 1, speed up is:
For example,
Speed up from pipelining
inst1: R2 <-R1 + R3
inst2: R2 <-R4 + R5 Pipeline depth × Clock cycle unpipelined
(1 + Pipeline stall) × Clock cycle pipelined
=
The WB (write back) of inst2 must be delayed
until the execution of inst1. where CPI is cycles per instruction.
Problem 2.7: Consider a four-stage pipeline processor. The number of cycles needed by the four instructions I1, I2,
I3 and I4 in stages instruction fetch, decode, operand fetch and execute are shown below. Assume I2 is the branch
instruction. Draw the timing space diagram.
S1 S2 S3 S4
I1 2 1 1 1
I2 1 2 3 1
I3 1 1 1 2
I4 2 1 3 1
Solution:
STEP → 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Fetch I1 I1 I2 I3 - - - - - I3 I4 I4
Decode I1 I2 I2 - - - - - I3 - I4
Operand Fetch I1 I2 I2 I2 - - - I3 - I4 I4 I4
Execute I1 - - - I2 - - - I3 I3 - - I4
Problem 2.8: Assume a simple 5-stage pipeline (IF, ID, E, DF, W) each stage takes a single cycle. Assuming there
are no cache misses. How many cycles would the following code take to execute if there is no special hardware to
improve performance in the presence of hazards?
MOV edx,[ecx+100]
MOV ebx,[ecx+104]
ADD edx,ebx
MOV [ecx+108],ebx
MOV eax,[ecx+100]
ADD ebx,eax
1 2 3 4 5 6 7 8 9 10 11 12 13 14
IF ID DF E W
IF ID DF E W
IF ID DF stall E W
IF ID stall DF stall W
IF ID stall DF stall stall E W
IF ID stall DF stall stall stall E W
Problem 2.9: In the below figure, calculate the total execution time after which the result of the fourth task enter-
ing the pipe above ready?
IF ID EX MEM WB
5 ns 5 ns 10 ns 10 ns 5 ns
Solution:
5 10 15 20 25 30 35 40 45 50 55 60 65
Inst1 IF ID EX EX MEM MEM WB
Inst2 IF ID EX EX MEM MEM WB
Inst3 IF ID EX EX MEM MEM WB
Inst4 IF ID EX EX MEM MEM WB
Problem 2.10: What is the mean overhead of a pipe- Problem 2.12: Calculate the time required to perform
line with 8 stages and an execution time per stage of 1000 operations in a 6-staged pipeline with an execu-
2 ns? tion time of 3 ns per stage?
Solution:
Solution: The mean overhead = (Stages - 1) ×
Execution time per stage = (8 - 1) × 2 = 7 × 2 = 14 ns Tp = (k - 1 + n) × T = (6 - 1 + 1000) × 3 = 3.015 µs
Problem 2.11: How many stages has a pipeline that Problem 2.13: Calculate the mean overhead of a pipeline
achieves a speed of 9.9 for 100 operations? with 7 stages and an execution time per stage of 2 ns?
Problem 2.14: Consider a pipeline with 5 stages: IF, ID, EX, M and W. Assume that each stage requires one clock
cycle. Show how the following program segment for adding 2 arrays is processed and compare the clock cycles
needed in non-pipelined system with pipelined system when result of the branch instruction i.e. content of is avail-
able after WB stage.
LOAD R4 #400
L1: LOAD R1, 0 (R4);
LOAD R2, 400 (R4);
ADD R3, R1, R2;
STORE R3, 0 (R4);
SUB R4, R4, #4;
BNEZ R4, L1;
Solution: Number of cycles = [Initial instruction + (Number of instructions in the loop L1) × Number of loop
cycles] × Number of clock cycles/instruction (CPI)
= [1 + (6) × 400/4] × 5 = 3005
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
LOAD R4 #400 IF ID EX M W
LOAD R1, 0 (R4) IF ID EX M W
LOAD R2, 400 (R4) IF ID stall stall EX M W
ADD R3, R1, R2 IF ID stall stall EX stall M W
STORE R3, 0 (R4) IF ID stall DF stall stall E W
SUB R4,R4, #4 IF ID stall Ex M W
BNEZ R4, L1 IF stall ID stall stall EX M W
Problem 2.15: Consider a 5-stage pipeline with stages: For all following questions we assume that: (a) Pipeline
contains stages: IF (Instruction Fetch), IS (Issue), FO (Fetch operand), E (Execute) and W (Write). (b) Each stage
except E requires one clock cycle and system has 4 Functional Units for floating point operations, FP load/store,
FP addition/subtraction, FP multiplication and FP division, (c) Execution stage for Load/Store operations requires
1 clock cycle, for ADD or SUB operations requires 1 clock cycle, for MUL operation requires 3 clock cycles and for
DIV operation requires 4 clock cycles. All memory references hit in cache. Pipeline has forwarding circuitry for all
FUs, except FP-Load/Store where operand is ready after W-stage.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
LOAD F6, 20(R5) IF IS FO E W
LOAD F2, 28(R5) IF ISD FO E W
MUL F0, F2, F4 IF IS stall stall FO E E E W
SUB F8, F6, F3 IF IS FO E W
DIV F10, F0, F6 IF IS stall stall stall stall FO E E E E W
ADD F6, F8, F2 IF IS FO E W
STORE F8, 50(R5) IF IS FO E W
Identify the hazards in the following instructions from the following list (Structural, Data, Control, RAW, WAR,
WAW, None)
1. MULT F0, F2, F4 and STORE F8, 50(R5)
2. DIV F10, F0, F6 and ADD F6, F8, F2
3. MULT F0, F2, F4 and DIV F10, F0, F6
4. DIV F10, F0, F6 and ADD F6, F8, F2
2.8 MEMORY HIERARCHY and ROM (read only memory). Integrated RAM chips
are available in two modes:
The storage media can be categorized in hierarchy accord- 1. Static RAM: It stores the binary information in
ing to their speed and cost (Fig. 2.10). As we move down flip flops and information remains valid until power
the hierarchy, access time increases and cost per bit is supplied. It has faster access time and is used in
decreases. implementing cache memory.
2. Dynamic RAM: It stores the binary information
as a charge on the capacitor. It requires refreshing
circuitry to maintain the charge on the capacitors
CPU after few milliseconds. It contains more memory
registers cells per unit area as compared to SRAM.
Decreasing Cache Increasing
cost and memory cost and
speed Main memory speed 2.8.1.1 Memory Interfacing
Magnetic disks If the required memory for the computer is larger
Increasing Decreasing
size size than the capacity of one chip, it is necessary to
Magnetic tapes connect multiple RAM and ROM chips to a CPU
(a) (b)
Figure 2.11 | (a) RAM chip. (b) ROM chip.
Problem 2.16: A computer employs RAM chips of 256 × 8 and ROM chips of 1024 × 16. The computer system needs
2K bytes of RAM and 4K bytes of ROM and four interface units each with four registers. Draw a memory address
map for the system and give the address range in hexadecimal for RAM and ROM chips.
≈ ≈ ≈ ≈
2.8.4 Cache Mapping Techniques Block 255
The cache memory can store a reasonable number of Block 127 Tag 3 Data Block 3968
blocks, but this number is always small as compared to
blocks in the main memory to keep average cost per bit
low. The correspondence between memory blocks and
≈ ≈
cache block is specified by the following mapping tech- Block 4095
niques. Consider a cache memory consisting of 2K words
with 128 blocks of 16 words each. Number of bits required Figure 2.13 | Direct mapped cache organization.
to address a cache block is 11 bits. Main memory has 64K
The demerit of direct mapping is that hit ratio drops
words and bits required to address is 16 (Fig. 2.12).
considerably if two or more words having same index and
different tags are accessed consecutively one after the other.
two fields: word and tag. The associative memory stores contain the desired block. The high-order tag field is
both the address (tag) and data of the main memory. then compared associatively to the tags corresponding
Figure 2.14 shows the mapping of different blocks into to the matched set. If a match occurs, the corresponding
cache. High-order 12 bits of CPU address is placed in the word is read from cache else main memory is referred
argument register of the associative memory and com- and block containing that word is brought into cache for
pared to tag bits of each block of the cache to see if the future reference (Fig. 2.15).
desired block is present. Once the desired block is pres-
ent, 4-bit word is used to extract necessary word from Tag Set Word
the cache. 6 6 4 Main memory
Main memory address Main memory address Block 0
12 4 Block 1
≈
Tag 0
Main memory ≈
Tag Word Cache memory
Block 0 Block 63
Set 0 Tag 0 Data Tag 2 Data
Cache memory Block 1 6 bits Block 62
Tag 0 Date
(12 bits)
Block i
Set 63 Tag 3 Date Tag 61 Date 4033 Tag 63
4095
Block 4095 Figure 2.15 | Set-associative mapped cache organization.
Problem 2.19: Consider a 2-way set associative cache Problem 2.20: The access time of a cache memory
consisting of 256 blocks of 8 words each, and assume is 200 ns and that of main memory is 2000 ns. It is
that the main memory is addressable by 16-bit address estimated that 70% of the memory requests are for
and it consists of 4K blocks. Calculate the number read and remaining 30% for write. The hit ratio for
of bits in each of the TAG, BLOCK/SET and word read accesses only is 0.9. A write-through procedure
fields for different mapping techniques? is used.
Solution: For direct mapping, word field is of 3 (a) What is the average access time of the system
bits to identify 8 different words in a block (23 = 8). considering only memory read cycles?
As cache memory consists of 256 blocks so (28 = 256) (b) What is the average access time of the system for
8 bits are required to address a block because there is both read and write requests?
one-to-one correspondence of block k in main memory (c) What is the hit ratio taking into consideration the
to block (k mod 256) in cache memory. The remain- write cycles also?
ing 5 (16 − 8 - 3) high-order address bits are tag bits.
Thus, the main memory address for direct mapping is Solution:
divided as follows: (a) Average access time = 0.9 × 200 + 0.1 × 2200 =
180 + 220 = 400 ns
Tag Block Word (b) Average access time = 0.3 × 2000 + 0.7 × 400 =
5 bits 8 bits 3 bits 600 + 280 = 880 ns
(c) Hit ratio = 0.7 × 0.9 = 0.63
For fully associative mapping, number of word
bits are same, that is, 3 bits. Cache memory stores
both tag and data. The high-order tag bits of an
address generated by CPU are compared with tag
bits of each block so number of block bits is zero. All Problem 2.21: A 4-way set-associative cache memory
remaining bits (except word bits) are identified as tag uses blocks of four words. The cache can accommo-
bits. Thus, the main memory address for fully asso- date a total of 1024 words from the main memory.
ciative mapping is divided as follows: The main memory size is 128K × 32.
(a) Formulate all pertinent information required to
Tag Word
construct the cache memory.
13 bits 3 bits (b) What is the size of the cache memory?
(b) Since cache is 4-way set associative, 4 blocks per set are stored in cache memory.
Problem 2.22: Suppose physical memory is of 2GB (b) Since cache is 2-way set associative, 2 blocks per
and each word is of 16 bits. There is a cache contain- set are stored in cache memory. So, the number
ing 2K words of data, and each cache block contains of sets is 128/2 = 64.
16 words. For each of the direct mapped and 2-way
set associative cache configurations, specify how the 21 bits 6 bits 4 bits
address would be partitioned. Tag Set Word
Solution:
(a) For direct mapping, the word field is of 4 bits
Problem 2.23: Consider a direct mapped cache of size
identify 16 different words in a block (24 = 16).
32 KB with block size 32 bytes. The CPU generates
As the cache memory consists of 2K words which
32-bit addresses. What are number of bits needed for
is equivalent to 2K/16 = 128 blocks so (27 =
addressing block in cache and number of tag bits?
128) 7 bits are required to address a block. The
remaining 20 (31 − 7 − 4) high-order address
Solution:
bits are tag bits. Thus, the main memory address
for direct mapping is divided as follows: Tag Block Word
32 − 10 − 5 bits 10 bits 5 bits
20 bits 7 bits 4 bits
Tag Block Word The number of bits needed for addressing block in
cache and number of tag bits are 10, 17, respectively.
IMPORTANT FORMULAS
SOLVED EXAMPLES
1. The principle of locality is used in 2. Which memory unit has lowest access time?
(a) Interrupt (b) Registers (a) Cache (b) Registers
(c) DMA (d) Cache memory (c) Optical disk (d) Main memory
Solution: It is used in cache memory to help the Solution: Registers are used for processing
program access small amounts of address space at and manipulating data and for holding memory
any instant. addresses that are available to the machine-code
programmer. So, they have lowest access time.
Ans. (d)
Ans. (b)
3. During DMA transfer, the DMA controller takes (c) a processor interrupt.
over the buses to manage the transfer (d) a clock interrupt.
(a) Directly from CPU to memory Solution: Hardware interrupt is present in hard-
(b) Directly from memory to CPU ware pins.
(c) Directly between the memory and registers Ans. (b)
(d) Directly between the I/O device and memory
9. Priority is provided by for access to memory
Solution: DMA controller manages transfer by various I/O channels and processors.
between I/O device and memory.
(a) a register
Ans. (d)
(b) a counter
4. Booth’s algorithm is used for the arithmetic opera- (c) the processor scheduler
tion of (d) a controller
(a) addition. (b) subtraction. Solution: Controller sets priority for memory
(c) multiplication. (d) division. access by various I/O devices and processes.
Solution: It is a multiplication algorithm that Ans. (d)
multiplies two signed binary numbers in 2’s com- 10. By applying the principle of temporal locality,
plement notation. processes are likely to reference pages that
Ans. (c)
(a) have been referenced recently.
5. The reason for improvement in CPU performance (b) are located at address near recently referenced
during pipelining is pages in memory.
(c) have been preloaded into memory.
(a) reduced memory access time.
(d) have to be reloaded into memory.
(b) increased clock speed.
(c) introduction of parallelism. Solution: Temporal locality refers to reuse of
(d) increase in cache memory. resources referenced within a short time frame.
Solution: Instruction-level parallelism is imple- Ans. (a)
mented within a single processor to allow faster 11. Which of the following is a correct statement
CPU throughput. related to L2 cache memory?
Ans. (c)
(a) T
he level 1 cache is always faster than the level
6. Use of cache memory enhances 2 cache.
(b) The level 2 cache is used to mitigate the dynamic
(a) I/O access time
slowdown every time a level 1 cache miss occurs.
(b) memory access time.
(c) Level 2 cache comes as on board only.
(c) effective memory access time.
(d) In modern day computer, the level 2 cache is
(d) secondary storage access time.
considered an internal cache.
Solution: Cache memory compensates the speed
Solution: L2 level of cache is placed between the
mismatch between processor and main memory
L1 and RAM. The L1 cache is always the fastest.
access time.
Ans. (c) Ans. (a)
7. An instruction cycle refers to 12. What is the control unit’s function in the CPU?
(a) fetching an instruction. (a) To decode program instructions
(b) executing an instruction. (b) To transfer data to primary storage
(c) fetching, decoding and executing an instruction. (c) To perform logical operations
(d) reading and executing an instruction. (d) To store arithmetic operations
Solution: It involves fetching, decoding and exe- Solution: Control unit controls several units of
cuting the instruction. CPU and helps decode program instructions.
Ans. (c)
Ans. (a)
8. A hardware interrupt is also called
13. CPU fetches the data and instructions from
(a) an internal interrupt.
(a) ROM (b) control unit
(b) an external interrupt.
(c) RAM (d) coprocessors chip
Solution: Disk is the I/O device attached exter- 23. Advantage of synchronous sequential circuits over
nally to the processor. Therefore, disk requires a asynchronous ones is
device driver.
(a) faster operation.
Ans. (d)
(b) ease of avoiding problems due to hazards.
21. More than one word are put in one cache block to (c) lower hardware requirement.
(d) better noise immunity.
(a) exploit the temporal locality of reference in a
program. Solution: Because of less delay, synchronous
(b) exploit the spatial locality of reference in a sequential circuits have faster operation than asyn-
program. chronous ones.
(c) reduce the miss penalty. Ans. (a)
(d) none of the above.
24. The total size of address space in a virtual memory
Solution: There are two types of locality of references system is limited by
temporal and spatial locality.
(a) the length of MAR.
The concept of spatial locality, instead of fetching
(b) the available secondary storage.
just one item from the main memory to the cache,
(c) the available main memory.
is useful to fetch several items that reside at adja-
(d) all of the above.
cent address as well.
So, option (b) is correct. Solution: Virtual memory depends only on the
Ans. (b) available size of the secondary memory.
22. Which of the following statements is false? Ans. (b)
(a) Virtual memory implements the translation of a 25. Comparing the time T1 taken for a single instruc-
program’s address space into physical memory tion on a pipelined CPU with time T2 taken on a
address space. non-pipelined but identical CPU, we can say that
(b) Virtual memory allows each program to exceed (a) T1 ≤ T2
the size of the primary memory. (b) T1 ≥ T2
(c) Virtual memory increases the degree of (c) T1 < T2
multiprogramming. (d) T1 plus T2 is the time taken for one instruction
(d) Virtual memory reduces the context-switching fetch cycle
overhead.
Solution: In case of one instruction, non-
Solution: Virtual memory increases the context- pipelined CPU takes less time as compared to pipe-
switching overhead. lined CPU. This is due to buffer delays for pipelining.
Ans. (d) Ans. (b)
1. For a pipelined CPU with a single ALU, consider Solution: All the three statements cause hazards.
the following situations: Ans. (d)
I. The j + 1-st instruction uses the result of the
Common Data Questions 2 and 3: Consider the
jth instruction as an operand.
following assembly language program for a hypo-
II. The execution of a conditional jump instruction.
thetical processor. A, B and C are 8-bit registers.
III. The j-th and j + 1-st instructions require the
The meanings of various instructions are shown as
ALU at the same time.
comments:
Which of the above can cause a hazard?
MOV B, #0; B ← 0
(a) I and II only (b) II and III only MOV C, #8; C ← 8
(c) III only (d) All the three Z: CMP C, #0; Compare C with 0
(GATE 2003: 1 Mark) JZ X; Jump to X if zero flag is set
Clocks 1 2 3 4 5 6 7 8 9 10 11 12
I1 IF RD EX MA WB
I2 IF - - - RD EX MA WB
I3 IF - - - RD - - EX MA WB
Ans. (c)
18. A device with data transfer rate 10 KB/s is con- fetch cycle of the first word of the instruction, its
nected to a CPU. Data is transferred byte-wise. register transfer interpretation is
Rn ⇐ PC+1;
Let the interrupt overhead be 4 s. The byte trans-
PC ⇐ M[PC];
fer time between the device interfaces register and
CPU or memory is negligible. What is the mini-
mum performance gain of operating the device The minimum number of CPU clock cycles needed
under interrupt mode over operating it under pro- during the execution cycle of this instruction is
gram-controlled mode? (a) 2 (b) 3 (c) 4 (d) 5
(a) 15 (b) 25 (GATE 2005: 2 Marks)
(c) 35 (d) 45 Solution: The minimum number of CPU clock
(GATE 2005: 2 Marks) cycles needed during the execution cycle = 4. This
is because
Solution: Data transfer rate = 10 KB/s 1 cycle is required to transfer already incremented
Interrupt overhead = 4 × 10−2 s value of PC
10 KB is sent = 1 s 2 cycles for getting data in MDR
1 B is sent = 1/10K = 100 − 10−2 s 1 to load value of MDR in PC
Minimum performance gain = 100 × 10−2/4 × 10−2 Ans. (c)
= 25
21. Consider a disk drive with the following
Ans. (b)
specifications:
Common Data Questions 19 and 20: Consider the 16 surfaces, 512 tracks/surface, 512 sectors/track, 1
following data path of a CPU. The ALU, the bus KB/sector, rotation speed 3000 rpm. The disk is oper-
and all the registers in the data path are of identi- ated in cycle stealing mode whereby whenever one 4
cal size. All operations including incrementation of byte word is ready it is sent to memory; similarly, for
the PC and the GPRs are to be carried out in the writing, the disk interface reads a 4 byte word from
ALU. Two clock cycles are needed for memory read the memory in each DMA cycle. Memory cycle time
operation - the first one for loading address in the is 40 nsec. The maximum percentage of time that the
MAR and the next one for loading data from the CPU gets blocked during DMA operation is:
memory bus into the MDR.
(a) 10 (b) 25
MAR MDR (c) 40 (d) 50
(GATE 2005: 2 Marks)
Solution:
Data transfer in one rotation = 512 × 1024 Bytes
S T 60
1 rotation takes = s
IR PC 3000
GPRs ALU 60
512KB is transferred in = s
3000
60
1 byte will be transferred = × 512 × 1024
19. The instruction “add R0, R1” has the register 3000
transfer interpretation R0 ⇐ R0 + R1. The mini- 4 bytes will be transferred
4
= 60 × × 512 × 1024 = 152.58 ns
mum number of clock cycles needed for execution
cycle of this instruction is 3000
(a) 2 (b) 3 40
Block % = = 26%
(c) 4 (d) 5 152.28
Ans. (b)
(GATE 2005: 2 Marks)
22. A CPU has 24-bit instructions. A program starts at
Solution: There will be three cycles-(1) R1out, address 300 (in decimal). Which one of the following
Sin, (2) R2out, Tin and (3) Sout, Tout, ALUadd, Rin. is a legal program counter (all values in decimal)?
Ans. (b) (a) 400 (b) 500
20. The instruction “call Rn, sub” is a two-word instruc- (c) 600 (d) 700
tion. Assuming that PC is incremented during the (GATE 2006: 1 Mark)
Solution: Size of instruction = 24 bits; Start and the bits are numbered 0 to 31, bit in position
address = 300. Legal address will be multiple of 0 being the least significant. Consider the following
three, that is, 300. emulation of this instruction on a processor that
Ans. (c) does not have bbs implemented.
23. A CPU has a cache with block size 64 bytes. temp ← reg & mask
The main memory has k banks, each bank being Branch to label if temp is non-zero.
c bytes wide. Consecutive c byte chunks are The variable temp is a temporary register. For correct
mapped on consecutive banks with wrap around. emulation, the variable mask must be generated by
All the k banks can be accessed in parallel, but
two accesses to the same bank must be serialized. (a) mask ← 0 × 1 pos
A cache block access may involve multiple itera- (b) mask ← 0 × ffffffff pos
tions of parallel bank accesses depending on the (c) mask ← pos
amount of data obtained by accessing all the k (d) mask ←0×f
banks in parallel. Each iteration requires decoding (GATE 2006: 2 Marks)
the bank numbers to be accessed in parallel and
this takes k/2 ns. The latency of one bank access is Solution: As there is only one bit with pos, the other
80 ns. If c = 2 and k = 24, the latency of retriev- bits need to be set to 0 in temp. The mask register
ing a cache block starting at address zero from the must have 1 in pos position, for which pos number
main memory is of left shifts over 1 need to be made.
Ans. (a)
(a) 92 ns (b) 104 ns
(c) 172 ns (d) 184 ns Common Data Questions 26 and 27: Consider two
cache organizations. The first one is 32 KB, 2-way
(GATE 2006: 2 Marks) set associative with 32-byte block size. The second
Solution: one is of the same size but direct mapped. The size
Time for one parallel process = k/2 + latency of an address is 32 bits in both cases. A 2-to-1 mul-
Time for one byte = 24/2 + 80 = 92 tiplexer has a latency of 0.6 ns while a k-bit com-
Total time for c bytes = 2 × 92 = 184 ns parator has a latency of k/10 ns. The hit latency of
Ans. (d) the set-associative organization is h1 while that of
the direct mapped one is h2.
24. A CPU has a five-stage pipeline and runs at 1
GHz frequency. Instruction fetch happens in the 26. The value of h1 is
first stage of the pipeline. A conditional branch (a) 2.4 ns (b) 2.3 ns
instruction computes the target address and (c) 1.8 ns (d) 1.7 ns
evaluates the condition in the third stage of the
pipeline. The processor stops fetching new instruc- (GATE 2006: 2 Marks)
tions following a conditional branch until the Solution:
branch outcome is known. A program executes Address bits = 32
109 instructions out of which 20% are conditional Block size = 32 B = 5 bits
branches. If each instruction takes one cycle to Size of cache = 32KB/32 = 1KB = 10 bits
complete on average, the total execution time of For 2-way set-associative memory = index bits = 9,
the program is tag bits = 18
(a) 1.0 s (b) 1.2 s k/10 = 18/10 = 1.8 + latency = 1.8 + 0.6 = 2.4
(c) 1.4 s (d) 1.6 s Ans. (a)
(GATE 2006: 2 Marks) 27. The value of h2 is
Solution: (a) 2.4 ns (b) 2.3 ns
Total execution time of the program (c) 1.8 ns (d) 1.7 ns
= 109 + 0.20 × 2 × 109 = 1.4 s (GATE 2006: 2 Marks)
Ans. (c)
Solution:
25. Consider a new instruction named branch-on-bit- In direct memory access: Tag bits = 17; Index = 10
set (mnemonic bbs). The instruction “bbs reg, pos, bits; word = 5 bits
label” jumps to label if bit in position pos of reg- k/10 = 17/10 = 1.7 + latency = 1.7 + 0.6 = 2.3
ister operand reg is one. A register is 32 bits wide Ans. (b)
Common Data Questions 28 and 29: A CPU has Solution: For 64 words, log264 = 6 bits are required.
a 32-KB direct-mapped cache with 128-byte block For lines = 128/4 = 32 lines, 5 bits are required
size. Suppose A is a two-dimensional array of size
512 × 512 with elements that occupy 8 bytes each. Tag bits Line Word
Consider the following two C code segments, P1 9 5 6
and P2. Ans. (d)
P1: for (i=0; i<512; i++) { 31. Consider a pipelined processor with the following
for (j=0; j<512; j++) { four stages:
x +=A[i][j];
} IF: Instruction fetch
} ID: Instruction decode and operand fetch
P2: for (i=0; i<512; i++) { EX: Execute
for (j=0; j<512; j++) { WB: Write back
x +=A[j][i]; The IF, ID and WB stages take one clock cycle each
} to complete the operation. The number of clock cycles
}
for the EX stage depends on the instruction. The
ADD and SUB instructions need 1 clock cycle and
P1 and P2 are executed independently with the
the MUL instruction needs 3 clock cycles in the EX
same initial state, namely, the array A is not in the
stage. Operand forwarding is used in the pipelined
cache and i, j, x are in registers. Let the number
processor. What is the number of clock cycles taken
of cache misses experienced by P1 be M1 and that
to complete the following sequence of instructions?
R2 ← R1 + R0
for P2 be M2.
ADD R2, R1, R0
28. The value of M1 is MUL R4, R3, R2 R4 ← R3 × R2
(a) 0 (b) 2048 SUB R6, R5, R4 R 6 ← R 5 − R4
(c) 16384 (d) 262144 (a) 7 (b) 8 (c) 10 (d) 14
(GATE 2006: 2 Marks) (GATE 2007: 1 Mark)
Solution: Solution:
Memory = 32 KB Clock 1 2 3 4 5 6 7 8
Block size = 128 B
I1 IF ID EX WB
Number of blocks = 256
Number of elements in block = 256/8 =16 I2 IF ID EX EX EX WB
P1 cache misses = M1 : 512 × 512/16 = 16384 I3 IF ID - - EX WB
Ans. (c)
Using operand forwarding, 8 clock cycles are required.
29. The value of the ratio M1/M2 is
Ans. (b)
(a) 0 (b) 1/16
32. In a simplified computer, the instructions are
(c) 1/8 (d) 16
OP RJ, Ri - Performs RJ OP Ri and stores the
(GATE 2006: 2 Marks)
result in register Ri.
Solution: OP m, Ri - Performs val OP Ri and stores the
P2 number of cache misses = M2 = 512 × 512 result in Ri. val denotes the content of memory
Ratio of M1:M2 = 1:16 location m.
Ans. (b) MOV, mRi - Moves the content of memory loca-
tion m to register Ri.
30. Consider a 4-way set-associative cache consist- MOV Ri, m - Moves the content of register Ri to
ing of 128 lines with a line size of 64 words. The memory location m.
CPU generates a 20-bit address of a word in main
memory. The number of bits in the TAG, LINE The computer has only two registers, and OP is either
and WORD fields are, respectively: ADD or SUB. Consider the following basic block:
t1 = a+b
(a) 9, 6, 5 (b) 7, 7, 6
t2 = c+d
e − t2
(c) 7, 5, 8 (d) 9, 5, 6
t3 =
(GATE 2007: 1 Mark) t4 = t1 − t3
Assume that all operands are initially in memory. The Solution: Given that R1 = 10, so the loop will
final value of the computation should be in memory. run 10 times
What is the minimum number of MOV instructions 10 × 2 + 1 = 21
in the code generated for this basic block? Ans. (d)
(a) 2 (b) 3
(c) 5 (d) 6 34. Assume that the memory is word addressable.
After the execution of this program, the content of
(GATE 2007: 1 Mark) memory location 2010 is
Solution: The instructions generated in the code (a) 100 (b) 101
for this basic block are as follows: (c) 102 (d) 110
MOV a, Ri (GATE 2007: 1 Mark)
ADD b, Ri
MOV c, Rj Solution: It will remain 100, because the loop will
ADD d, Rj exit as the value in R1 becomes 0 when address in
SUB e, Rj R3 becomes 2010.
SUB Ri, Rj Ans. (a)
MOV m, Ri 35. Assume that the memory is byte addressable and
Ans. (b) the word size is 32 bits. If an interrupt occurs
Common Data Questions 33-35: Consider the during the execution of the instruction “INC R3”,
following program segment. Here R1, R2 and R3 what return address will be pushed on to the stack?
are general purpose registers. (a) 1005 (b) 1020
(c) 1024 (d) 1040
Instruction Operation Instruction
Size (No. (GATE 2007: 1 Mark)
of Words) Solution: Memory is byte addressable, take 4 bytes
per word. So at INC R3, stack will contain 1024.
MOV R1, R1 ← m[3000] 2
(3000) Ans. (c)
LOOP: MOV R2 ← M[R3] 1 36. Consider a disk pack with 16 surfaces, 128 tracks
R2, (R3) per surface and 256 sectors per track. 512 bytes of
R2 ← R1 + R2
ADD R2, R1 1 data are stored in a bit serial manner in a sector.
The capacity of the disk pack and the number of
MOV (R3), R2 M[R3] ← R2 1 bits required to specify a particular sector in the
disk are respectively:
INC R3 R3 ← R3 + 1 1
(a) 256 Mbyte, 19 bits (b) 256 Mbyte, 28 bits
DEC R1 R1 ← R1 - 1 1 (c) 512 Mbyte, 20 bits (d) 64 Gbyte, 28 bits
BNZ LOOP Branch on not 2 (GATE 2007: 1 Mark)
zero
Solution:
Disk capacity = 16 surfaces × 128 tracks ×
HALT Stop 1
256 sectors × 512 bytes = 256 MB
Assume that the content of memory location 3000 Total number of sectors = 16 × 128 × 256 = 219
is 10 and the content of the register R3 is 2000. The Ans. (a)
content of each of the memory locations from 2000 to
2010 is 100. The program is loaded from the memory Linked Answer Questions 37 and 38: Consider a
location 1000. All the numbers are in decimal. machine with a byte addressable main memory of
162 bytes. Assume that a direct mapped data cache
33. Assume that the memory is word addressable. The consisting of 32 lines of 64 bytes each is used in the
number of memory references for accessing the system. A 50 × 50 two-dimensional array of bytes is
data in executing the program completely is stored in the main memory starting from memory
location 1100H. Assume that the data cache is ini-
(a) 10 (b) 11
tially empty. The complete array is accessed twice.
(c) 20 (d) 21
Assume that the contents of the data cache do not
(GATE 2007: 1 Mark) change in between the two accesses.
37. How many data cache misses will occur in total? exception occurs, so an exception is not allowed to
execute. Option (d) is the correct option.
(a) 48 (b) 50 (c) 56 (d) 59
Ans. (d)
(GATE 2007: 2 Marks)
41. For a magnetic disk with concentric circular tracks,
Solution: the seek latency is not linearly proportional to the
Main memory = 216 B seek distance due to
Block size = 64 B (a) non-uniform distribution of requests
Number of blocks = 32 (b) arm starting and stopping inertia
Number of elements = 50 × 50 = 2500 (c) higher capacity of tracks on the periphery of
Starting from location 1100 means from 68th block the platter
Number of blocks = 2500/64 = 40 blocks (d) use of unfair arm scheduling policies
Initially cache is empty for 32 misses, then 8 are
remaining from total 40 for one access (GATE 2008: 2 Marks)
Array is traversed twice, so data cache misses = Solution: Tracks on magnetic disks are concentric
40 + 8 + 8 = 56 and seek latency from one sector to other which
Ans. (c) may or may not be in different tracks. This seek
38. Which of the following lines of the data cache will distance is not proportional to latency since the
be replaced by new blocks in accessing the array tracks at periphery have higher diameter, and
for the second time? hence higher capacity to store data.
Ans. (b)
(a) line 4 to line 11 (b) line 4 to line 12
(c) line 0 to line 7 (d) line 0 to line 8 42. Which of the following are NOT true in a pipelined
processor?
Solution:
Applying k mod c to find the location: 68 mod I. Bypassing can handle all RAW hazards.
32 = 4 to 11 II. Register renaming can eliminate all register
Ans. (a) carried WAR hazards.
III. C ontrol hazard penalties can be eliminated by
39. Which of the following is/are true for the auto- dynamic branch prediction.
increment addressing mode?
(a) I and II only (b) I and III only
I. It is useful in creating self-relocating code. (c) II and III only (d) I, II and III
II. If it is included in an Instruction Set Architec
ture, then an additional ALU is required for (GATE 2008: 2 Marks)
effective address calculation. Solution: All the statements are true.
III. T he amount of increment depends on the size Ans. (d)
of the data item accessed.
43. For inclusion to hold between two cache levels L1
(a) I only (b) II only and L2 in a multi-level cache hierarchy, which of
(c) III only (d) II and III only the following are necessary?
(GATE 2008: 2 Marks) I. L1 must be a write-through cache
Solution: Only statement (III) is true. II. L2 must be a write-through cache
Ans. (c) III. T
he associativity of L2 must be greater than
40. Which of the following must be true for the RFE that of L1
(Return from Exception) instruction on a general IV. The L2 cache must be at least as large as the
purpose processor? L1 cache
(a) IV only (b) I and IV only
I. It must be a trap instruction. (c) I, II and IV only (d) I, II, III and IV
II. It must be a privileged instruction.
III. A
n exception cannot be allowed to occur during (GATE 2008: 2 Marks)
execution of an RFE instruction. Solution: L1 and L2 cache are placed between
(a) I only (b) II only CPU and they can be both write through cache
(c) I and II only (d) I, II and III only but not necessarily.
Associativity does not matter.
(GATE 2008: 2 Marks)
L2 cache must be at least as large as L1 cache,
Solution: RFE (Return from Exception) is a since all the words in L1 are also in L2.
privileged trap instruction that is executed when Ans. (a)
44. The use of multiple register windows with over- Solution: Total elements can come in one slot 2048.
lap causes a reduction in the number of memory After 2048 elements, same cache index will be on
accesses for [2][0] and [4][0].
Ans. (b)
I. Function locals and parameters
II. Register saves and restores 47. The cache hit ratio for this initialization loop is
III. Instruction fetches
(a) 0% (b) 25% (c) 50% (d) 75%
(a) I only (b) II only (c) III only (d) I, II and III
(GATE 2008: 2 Marks)
(GATE 2008: 2 Marks)
Solution: Cache hit ratio is found out as follows:
Solution: Multiple register windows with over-
1024 1
lap causes a reduction in the number of memory = = 50%
accesses for register saves and restores. 2048 2
Ans. (b) As we can see in the above, there will be 50% hits.
Ans. (c)
45. Consider a machine with a 2-way set-associative
data cache of size 64 KB and block size 16 bytes. Linked Answer Questions 48 and 49: Delayed
The cache is managed using 32 bit virtual addresses branching can help in the handling of control
and the page size is 4 KB. A program to be run on hazards.
this machine begins as follows:
48. For all delayed conditional branch instructions,
double ARR [1024] [1024]; irrespective of whether the condition evaluates to
int i, j;
true or false:
/* Initialize array ARR to 0.0 */
for(i=0; i<1024; i++) (a) T he instruction following the conditional
for(j=0; j<1024; j++) branch instruction in memory is executed.
ARR [i] [j] =0.0; (b) The first instruction in the fall through path is
executed.
The size of double is 8 bytes. Array ARR is located (c) The first instruction in the taken path is
in memory starting at the beginning of virtual page executed.
0xFF000 and stored in row major order. The cache (d) The branch takes longer to execute than any
is initially empty and no pre-fetching is done. The other instruction.
only data memory references made by the program
(GATE 2008: 2 Marks)
are those to array ARR.
The total size of the tags in the cache directory is Solution: The first instruction following the
branch instruction is always executed (irrespective
(a) 32 Kbits (b) 34 Kbits
of whether the branch is taken or not).
(c) 64 Kbits (d) 68 Kbits
Ans. (b)
(GATE 2008: 2 Marks)
49. The following code is to run on a pipelined proces-
Solution: sor with one branch delay slot:
Virtual address = 32 bits
2-way cache size = 64 KB I1: ADD R2 ← R7 + R8
1 set will contain = 32 KB entries = 15 bits I2: SUB R4 ← R5 − R6
Block size = 16 bytes = 4 bits I3: ADD R1 ← R2 + R3
Tag bits Set bits Word bits I4: STORE Memory [R4] ← R1
17 11 4 BRANCH to Label if R1 == 0
Tag size = 17 × 2 × 1024 = 34 kbits Which of the instructions I1, I2, I3 or I4 can legiti-
Ans. (b) mately occupy the delay slot without any other
program modification?
46. Consider the data given in the above question.
Which of the following array elements has the (a) I1 (b) I2 (c) I3 (d) I4
same cache index as ARR[0][0]? (GATE 2008: 2 Marks)
(a) ARR[0][4] (b) ARR[4][0] Solution: Instruction I2 contains delayed slot. I4
(c) ARR[0][5] (d) ARR[5][0] has data dependency in I2.
(GATE 2008: 2 Marks) Ans. (b)
50. How many 32K × 1 RAM chips are needed to pro- 53. Consider a 4-way set-associative cache (initially
vide a memory capacity of 256 K bytes? empty) with total 16 cache blocks. The main
memory consists of 256 blocks and the request for
(a) 8 (b) 32
memory blocks is in the following order:
(c) 64 (d) 128
0, 255, 1, 4, 3, 8, 133, 159, 216, 129, 63, 8, 48, 32,
Solution: 73, 92, 155
256K × 8 Which one of the following memory block will NOT
Number of chips required = = 64
32K × 1 be in cache if LRU replacement policy is used?
Ans. (c) (a) 3 (b) 8 (c) 129 (d) 216
(GATE 2009: 2 Marks)
51. A CPU generally handles an interrupt by execut-
Solution:
ing an interrupt service routine
To decide the location (address), mod 4 is applied.
(a) as soon as an interrupt is raised.
(b) by checking the interrupt register at the end of Set 0 0, 4, 8,216 → 48, 32, 8, 92
the fetch cycle. Set 1 1, 133, 129, 73
(c) by checking the interrupt register after finishing Set 2 155
the execution of the current instruction. Set 3 255, 3, 159, 63
(d) by checking the interrupt register at fixed time 216 will not be there in cache.
intervals. Ans. (d)
Common Data Questions 54 and 55: A hard disk has
Solution: Interrupts are handled by checking the 63 sectors per track, 10 platters each with 2 record-
interrupt register after finishing the execution of ing surfaces and 1000 cylinders. The address of a
current instruction. sector is given as a triple <c, h, s>, where c is the
Ans. (c) cylinder number, h is the surface number and s is
the sector number. Thus, the 0th sector is addressed
as <0, 0, 0>, the 1st sector as <0, 0, 1>, and so on.
52. Consider a 4-stage pipeline processor. The number
of cycles needed by the four instructions I1, I2, I3,
I4 in stages S1, S2, S3, S4 is shown below: (GATE 2009: 2 Marks)
54. The address <400, 16, 29> corresponds to sector
S1 S2 S3 S4 number:
I1 2 1 1 1 (a) 505035 (b) 505036
(c) 505037 (d) 505038
I2 1 3 2 2
I3 1 1 1 3 Solution:
Total surfaces = 10 × 2 = 20
Address is: 400 × 20 × 63 + 16 × 63 + 29 = 505037
I4 1 2 2 2
Ans. (c)
What is the number of cycles needed to execute the 55. The address of 1039th sector is
(a) <0, 15, 31> (b) <0, 16, 30>
following loop?
for (i=1 to 2) {I1; I2; I3; I4;} (c) <0, 16, 31> (d) <0, 17, 31>
Solution:
(a) 16 (b) 23 (c) 28 (d) 30 Address of 1039th sector = 16 × 31 + 31 = 1039
(GATE 2009: 2 Marks) Ans. (c)
Solution:
Clock 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
I1 S1 S1 S2 S3 S4
I2 S1 S2 S2 S2 S3 S3 S4 S4
I3 S1 S1 S2 S3 S4 S4 S4
I4 S1 S2 S2 S3 S3 S4 S4
For two iterations = 15 × 2 = 30 clock cycles
Ans. (d)
56. A main memory unit with a capacity of 4 mega- Here we have to find X in terms of Y. So,
bytes is built using 1M × 1-bit DRAM chips. Each a a a -1
X = a1 + 2 + 3 + + (nn- =Y
2 2)
DRAM chip has 1 K rows of cells with 1K cells
2 4
in each row. The time taken for a single refresh
operation is 100 ns. The time required to perform
If a0 + 1 + 2 + … + n-1
a a a
one refresh operation on all the cells in the memory 2 4 2( n -1)
unit is
a -1
< a1 + 2 + 3 + … + (nn-
a a
(a) 100 nanoseconds 2 4 2 2)
(b) 100 × 210 nanoseconds
(c) 100 × 220 nanoseconds OR
(d) 3200 × 220 nanoseconds a 1
X = a0 + 1 + 2 + + (nn -
a a
= a0 +
Y
(GATE 2010: 1 Mark) 2 4 2 -1)
2
Solution:
If a0 + 1 + 2 + … + (nn −−11)
a a a
Main memory = 4 MB 2 4
Number of DRAM chips = 4 MB/ 1M × 1 bit = 32
2
Total cells = 32 × 1K × 1K
> a1 + 2 + 3 + … + (nn −−12)
a a a
Time taken to refresh all the cells = 32 × 1K × 2 4 2
1K × 100 ns Hence, we sum up as
Ans. (d) X = MAX(Y, a0 + Y/2)
57. The weight of a sequence a0, a1/2, … an−1 of real Ans. (b)
numbers is defined as a0 + a1/2 + … + an−1/2n−1.
58. A 5-stage pipelined processor has instruction fetch
A subsequence of a sequence is obtained by delet-
(IF), instruction decode (ID), operand fetch (OF),
ing some elements from the sequence, keeping the
perform operation (PO) and write operand (WO)
order of the remaining elements the same. Let X
stages. The IF, ID, OF and WO stages take 1 clock
denote the maximum possible weight of a subse-
quence of a0, a1, …, an−1 and Y the maximum pos-
cycle each for any instruction. The PO stage takes
sible weight of a subsequence of a1, a2, … an−1.
1 clock cycle for ADD and SUB instructions, 3 clock
cycles for MUL instruction and 6 clock cycles for
Then X is equal to
DIV instruction, respectively. Operand forwarding
(a) max(Y, a0 + Y ) (b) max(Y, a0 + Y/2) is used in the pipeline. What is the number of clock
(c) max(Y, a0 + 2Y ) (d) a0 + Y/2 cycles needed to execute the following sequence of
(GATE 2010: 2 Marks) instructions?
Solution: The concepts involve the Dynamic Instruction Meaning of Instruction
Programming in Algorithms.
Given that I0: MUL R2, R0, R1 R2 ← R0 × R1
X = max weight from the sequence (a0, a1, a2, … I1: DIV R5, R3, R4 R5 ← R3/R4
a a a -1
an−1) = a0 + 1 + 2 + + n I2: ADD R2, R5, R2 R2 ← R5 + R2
2 4 2(n -1)
Y= max weight from the sequence (a1, a2, … an−1) I3: SUB R5, R2, R6 R5 ← R2 − R6
a2 a3 a -1
= a1 + + + + (nn -2) (a) 13 (b) 15 (c) 17 (d) 19
2 4 2 (GATE 2010: 2 Marks)
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
I0 IF ID OF PO PO PO WO
I1 IF ID OF - - PO PO PO PO PO PO WO
I2 IF ID OF - - - - - PO WO
I3 IF ID OF PO WO
With operand forwarding, 15 clock cycles are needed.
Ans. (b)
59. The program below uses six temporary variables a, Solution: Access to L1 cache = 2 ns
b, c, d, e, f. Access to L2 cache = 20 ns
a = 1 Block size of L2 is 16 words; data bus size is 4 words.
b = 10 So, time taken for this transfer = 4 × 22 = 88 ns.
c = 20 Ans. (d)
d = a + b 61. When there is a miss in both L1 cache and L2 cache,
e = c + d
first a block is transferred from main memory to
f = c + e
b = c + e L2 cache, and then a block is transferred from L2
e = b + f cache to L1 cache. What is the total time taken for
d = 5 + e these transfers?
return d + f (a) 222 nanoseconds (b) 888 nanoseconds
(c) 902 nanoseconds (d) 968 nanoseconds
Assuming that all operations take their operands
from registers, what is the minimum number of regis- (GATE 2010: 2 Marks)
ters needed to execute this program without spilling? Solution: Block transfer from main memory to L2
cache = 2 + 4 × (20 + 200) = 2 + 880 = 882 ns
(a) 2 (b) 3
L2 to L1 = 882 + 20 + 66 = 968 ns
(c) 4 (d) 6 Ans. (d)
(GATE 2010: 2 Marks) 62. A computer handles several interrupt sources of
which the following are relevant for this question.
Solution: Let us take three Registers R1, R2 and R3
••Interrupt from CPU temperature sensor (raises
R1 R2 R3 interrupt if CPU temperature is too high)
••Interrupt from Mouse (raises interrupt if the
a=1 b = 10 c = 20
mouse is moved or a button is pressed)
d = 11 b = 10 c = 20 ••Interrupt from Keyboard (raises interrupt when
d = 11 e = 21 c = 20 a key is pressed or released)
••Interrupt from Hard Disk (raises interrupt when
f = 41 e = 21 c = 20
a disk read is completed)
b = 41 e = 21 c = 20
Which one of these will be handled at the HIGHEST
e = 42 b = 41 f = b = 41 priority?
d = 47 e = 42 f = 41 (a) Interrupt from Hard Disk
(b) Interrupt from Mouse
All the operations will be completed using three
(c) Interrupt from Keyboard
registers only.
(d) Interrupt from CPU temperature sensor
Ans. (b) (GATE 2011: 1 Mark)
Common Data Questions 60 and 61: A computer Solution: Interrupt from CPU temperature sensor
system has an L1 cache, an L2 cache, and a main will be handled at the highest priority.
memory unit connected as shown below. The block Ans. (d)
size in L1 cache is 4 words. The block size in L2 63. Consider a hypothetical processor with an instruction
cache is 16 words. The memory access times are 2 of type LW R1, 20 (R2), which during execution reads
nanoseconds, 20 nanoseconds and 200 nanoseconds a 32-bit word from memory and stores it in a 32-bit
for L1 cache, L2 cache and main memory unit, register R1. The effective address of the memory loca-
respectively. tion is obtained by the addition of a constant 20 and
Data bus Data bus the contents of register R2. Which of the following
L1 L2 Main best reflects the addressing mode implemented by
Cache Cache memory this instruction for the operand in memory?
4 words 4 words
(a) Immediate addressing
60. When there is a miss in L1 cache and a hit in L2 (b) Register addressing
cache, a block is transferred from L2 cache to (c) Register indirect scaled addressing
L1 cache. What is the time taken for this transfer? (d) Base indexed addressing
(GATE 2011: 1 Mark)
(a) 2 nanoseconds (b) 20 nanoseconds
(c) 22 nanoseconds (d) 88 nanoseconds Solution: Effective address = contents of register
R2 + 20.
(GATE 2010: 2 Marks) Ans. (d)
64. On a non-pipelined sequential processor, a program Memory size for tag bits = 19 + 2 = 21
segment, which is a part of the interrupt service Total size of memory for tags = 21 × 256 = 5376 bits
routine, is given to transfer 500 bytes from an I/O Ans. (d)
device to memory.
66. Consider an instruction pipeline with four stages
Initialize the address register (S1, S2, S3 and S4) each with combinational circuit
Initialize the count to 500 only. The pipeline registers are given in the figure.
LOOP: Load a byte from device
Tag bits Block bits Word bits Solution: Register renaming is done to handle
WAR/WAW hazards.
19 8 5
Ans. (c)
69. The amount of ROM needed to implement a 4-bit 73. Consider the following sequence of micro-operations.
multiplier is MBR ← PC
(a) 64 bits (b) 128 bits (c) 1 Kbits (d) 2 Kbits MAR ← X
PC ← Y
Solution: Amount of ROM required Memory ← MBR
= 22k × 2k (where k = number of bits) Which one of the following is a possible operation
= 22×4 × 2 × 4 performed by this sequence?
= 2 Kbits (GATE 2013: 2 Marks)
Ans. (d)
(a) Instruction fetch
70. A computer has a 256 KB, 4-way set-associative, (b) Operand fetch
write-back data cache with block size of 32 bytes. (c) Conditional branch
The processor sends 32-bit addresses to the cache (d) Initiation of interrupt service
controller. Each cache tag directory entry contains,
in addition to address tag, 2 valid bits, 1 modified Solution: Program counter value is stored in
bit and 1 replacement bit. memory by MBR and gets a new address by Y. This
indicates initialization of interrupt service routine.
The number of bits in the tag field of an address is Ans. (d)
(a) 11 (b) 14 (c) 16 (d) 27
74. Consider a hard disk with 16 recording surfaces
(GATE 2012: 2 Marks)
(0-15) having 16384 cylinders (0-16383) and each
Solution: cylinder contains 64 sectors (0-63). Data storage
256 KB capacity in each sector is 512 bytes. Data are orga-
Number of blocks = = 213 blocks nized cylinder-wise and the addressing format is
32 B
<cylinder no., surface no., sector no.>. A file of size
Due to 4-way set associative = 213/22 = 211 42797 KB is stored in the disk and the starting disk
32 bits location of the file is <1200, 9, 40>. What is the
cylinder number of the last sector of the file, if it is
Ans. (c) stored in a contiguous manner?
71. The size of the cache tag directory is (GATE 2013: 2 Marks)
(a) 160 Kbits (b) 136 bits (a) 1281 (b) 1282 (c) 1283 (d) 1284
(c) 40 Kbits (d) 32 bits Solution: Number of sectors required to store the
(GATE 2012: 2 Marks) 42797 × 1024
file = = 85594 sectors
512
Solution: Tag directory contains: Tag bits + 4 Number of sectors in a cylinder = 16 × 64 = 1024
additional bits = 20 bits
85594
Size of cache tag directory = 20 × 213 = 160 Kbits Total number of cylinders required = = 84
Ans. (a) 1024
Last sector will be stored on 1284th cylinder.
72. In a k-way set associative cache, the cache is divided Ans. (d)
into v sets, each of which consists of k lines. The lines
of a set are placed in sequence one after another. 75. Consider an instruction pipeline with five stages
The lines in set s are sequenced before the lines in without any branch prediction: Fetch Instruction
set (s+1). The main memory blocks are numbered (FI), Decode Instruction (DI), Fetch Operand
0 onwards. The main memory block numbered j (FO), Execute Instruction (EI) and Write Operand
must be mapped to any one of the cache lines from (WO). The stage delays for FI, DI, FO, EI and WO
are 5 ns, 7 ns, 10 ns, 8 ns and 6 ns, respectively.
(a) (j mod v) * k to (j mod v) * k + (k-1) There are intermediate storage buffers after each
(b) (j mod v) to (j mod v) + (k-1) stage and the delay of each buffer is 1 ns. A pro-
(c) (j mod k) to (j mod k) + (v-1) gram consisting of 12 instructions I1, I2, I3, … I12 is
(d) (j mod k) * v to (j mod k) * v + (v-1) executed in this pipelined processor. Instruction I4
(GATE 2013: 1 Mark) is the only branch instruction and its branch target
is I9. If the branch is taken during the execution of
Solution: Number of sets = v
this program, the time (in ns) needed to complete
Number of main memory blocks = j
the program is
Number of lines = k (from 0 to k-1)
Position will be (j mod v)*k to (j mod v)*k +k-1 (a) 132 (b) 165 (c) 176 (d) 328
Ans. (a) (GATE 2013: 2 Marks)
PRACTICE EXERCISES
(c) In the CISC instruction set, all arithmetic/logic 11. The most relevant addressing mode to write posi-
instructions must be register based for fast tion-independent code is
processing. (a) direct mode (b) auto mode
(d) CISC architectures may perform better in net- (c) relative mode (d) indexed mode
work centric applications than RISC.
12. A CPU uses 24-bit instruction. A program starts at
4. The register that holds the address of the loca- address 300 (in decimal). Which one of the follow-
tion to or from which data are to be transferred is ing is a legal program counter content (all values
called in decimal)?
(a) Index register (a) 324 (b) 512
(b) Accumulator (c) 600 (d) 700
(c) Memory address registers
(d) Memory data registers 13. An attempt to access a location not owned by a
program is called
5. Which one of the following is not a type of I/O
(a) data fault (b) address fault
channel?
(c) instruction fault (d) page fault
(a) Multiplexer (b) Selector
14. Which of the following statement about relative
(c) Block multiplexer (d) None of the above
addressing mode is FALSE?
6. The performance of a pipelined processor is (a) It enables reduced program code
degraded if (b) It allows indexing of array element with same
(a) the pipeline stages have different delays instruction
(b) consecutive instructions are to be executed serially (c) It enables easy relocation of data
(c) the pipeline stages share hardware resources (d) It enables faster address calculation than abso-
(d) all of the above lute addressing
7. The minimum time delay between the initiation of 15. Compared to CISC processors, RISC processors contain
two independent memory operations is called (a) more register and smaller instruction set
(a) Access time (b) Cycle time (b) larger instruction set and less registers
(c) Rotational time (d) Latency time (c) less registers and smaller instruction set
(d) more registers and larger instruction set
8. The register which keeps track of the execution of a
16. Micro-programmed control cannot be implemented
program and which contains the memory address of
in RISC architecture because
the instruction currently being executed is known
as (a) it tends to slow down the processor.
(b) it consumes more chip areas and large instruc-
(a) index register
tion set.
(b) memory address register
(c) handling a large number of registers is impos-
(c) program counter
sible in micro-programmed system.
(d) instruction registers
(d) the 1 instruction/cycle timing requirement
9. For interval arithmetic, the best rounding tech- for RISC is difficult to achieve in micro-pro-
nique used is grammed based architecture.
(a) rounding to plus and minus infinity 17. Relocation of the code is easier in irrespec-
(b) rounding to zero tive of the program code
(c) rounding to nearest zero (a) indirect addressing
(d) rounding to the next number (b) indexed addressing
10. Hardwired control unit are faster than micro- (c) base register addressing
programmed control unit because (d) absolute addressing
(a) they do not consist of slower memory elements. 18. In inverted page table organization, the size of the
(b) they do not have slower elements such as gates, page table depends on
flip flops and registers. (a) the number of processes
(c) they consist of elements based on VVLSI design (b) the size of page
technology. (c) the size of main memory
(d) they contain high-speed digital components. (d) the number of frames in the main memory
19. When using the concept of locality of reference, the 25. A device employing INTA line for device inter-
page reference being made by a process rupt puts the CALL instruction on the data bus
(a) will always be to the page used in the previous while
page reference (a) INTA is active. (b) HOLD is active.
(b) is likely to be one of the pages used in the past (c) READY is active. (d) READY is active.
few page references
(c) will always to be one of the pages existing in 26. On receiving an interrupt from an I/O device, the CPU
the main memory (a) b ranches off to halt (or wait) for a predeter-
(d) will always lead to page fault mined time
(b) branches off to the interrupt service after com-
20. If the new version of processor is not made com-
pletion of the current instruction
patible to programs written for its older version, it
(c) branches off to the interrupt service routine
could be able to process at a faster speed
immediately
(a) the statement is true. (d) hands over control of address bus and data bus
(b) the statement is false. to the interrupting device
(c) the speed cannot be predicted.
(d) speed has nothing to do with the compatibility. 27. Using large block size in a fixed block size file
system leads to
21. A certain snooping cache can snoop only on address
(a) b etter disk throughput but poorer disk space
line. Which of the following is true?
utilization
(a) This would adversely affect the system if the (b) better disk throughput and better disk space
write-through protocol is used. utilization
(b) It would run well if the write-through protocol (c) it does not matter as the total memory size is
is used. same
(c) Data snooping is mandatory to be implemented (d) poorer disk throughput but better disk space
on data line. utilization
(d) Data snooping may not be required.
28. Which of the following statements are true about
22. When the frequency of the input signal to a CMOS paging?
gate is increased, the average power dissipation (a) It divides memory into units of equal size.
(a) decreases exponentially (b) It permits implementation of virtual memory.
(b) increases (c) It suffers from internal fragmentation.
(c) decreases (d) It suffers from external fragmentation.
(d) increases exponentially
29. The number of entries in an inverted page table
23. The disadvantage of hardwired control units with (a) is equal to the number of processes.
flip flop is (b) is equal to the number of page frames in the
(a) design becomes complex main memory.
(b) it requires more number of flip flops (c) is equal to the size of the page frame.
(c) control circuit speed does not match with flip (d) is equal to the number of page frames in cache
flops memory.
(d) flip—flops can handle the data unit not the
30. In a virtual memory system, the addresses used by
control unit
the programmer belongs to
24. In a vectored interrupt, (a) memory space (b) physical space
(a) the branch address is assigned to a fixed loca- (c) address space (d) main memory space
tion in memory.
31. Power consumption of processors can be vastly
(b) the interrupting source supplies the branch
reduced by making use of based transistors
information to the processor through an inter-
to implement the ICs.
rupt vector.
(c) the branch address is obtained from a register (a) NMOS only
in the processor. (b) TTL Schottky and PMOS
(d) the branch address is obtained from program (c) PMOS only
counter. (d) NMOS and PMOS
32. Address symbol table is generated by the 41. In a multiprogramming system, which of the fol-
(a) memory management software lowing concepts is used?
(b) assembler (a) Data parallelism (b) Paging
(c) table match of associative memory (c) L1 cache (d) DMA
(d) generated by CPU
42. PAL circuit can be defined as
33. How many 128 × 8 RAM chips are needed to have
(a) fixed OR and programmable AND logic.
a total RAM of 2048 bytes?
(b) programmable OR and programmable AND logic.
(a) 8 (b) 16 (c) 24 (d) 32 (c) fixed AND and programmable OR logic.
(d) fixed OR and fixed AND logic.
34. In 8085 microprocessor, how many I/O devices can
be interfaced in I/O mapped I/O technique? 43. If the clock input applied to a cascaded Mod-6 and
Mod-4 counter is 48 kHz. Then the output of the
(a) Either 256 input devices or 256 output devices
cascaded arrangement shall be of:
(b) 8 I/O devices
(c) 256 input devices and 256 output devices (a) 4.8 kHz (b) 12 kHz
(d) 512 input-output devices (c) 8 kHz (d) 48 kHz
44. If there are four ROM ICs of 8K and two RAM ICs
35. After reset, the CPU starts the execution of instruc-
of 4K words, then the address range of Ist RAM is
tion from memory address
(assume initial addresses correspond to ROMs)
(a) 1111H (b) 8000H
(c) 0000H (d) FFFFH (a) (8000)H to (9FFF)H (b) (5000)H to (7FFF)H
(c) (8000)H to (8FFF)H (d) (5000)H to (9FFF)H
36. In a microprocessor system, suppose TRAP, HOLD
45. The method for updating the main memory as soon
and RESET pin got activated at the same time,
as a word is removed from the cache is called
while the processor was executing some instruc-
tions, the system will (a) Write-through (b) Write-back
(c) Write-save (d) Cache-save
(a) execute the TRAP instruction
(b) execute the HOLD instruction Set 2
(c) execute the RESET instruction
(d) none of these instructions will be executed 1. The most appropriate matching for the following
pairs
37. In 8085 microprocessor, the programmer cannot
access which flag directly? X. Indirect addressing 1. Loops
Y. Immediate addressing 2. Pointers
(a) Sign flag (b) Carry flag Z. Auto-decrement addressing 3. Constants
(c) Auxiliary carry flag (d) Parity flag
(a) X − 3 Y − 2 Z − 1 (b) X - 1 Y - 3 Z - 2
38. Which of the following is a pseudo-instruction for (c) X − 2 Y − 3 Z − 1 (d) X - 3 Y - 1 Z - 2
8085?
2. Which of the following is not a form of memory?
(a) SPHL (b) CMP
(c) NOP (d) END (a) Instruction cache
(b) Instruction register
39. The term “cycle stealing” refers to: (c) Instruction opcode
(d) Translation look aside buffer
(a) Interrupt-based data transfer
(b) DMA-based data transfer 3. In serial data transmission, every byte of data is
(c) Polling mode data transfer padded with a `0’ in the beginning and one or two
(d) Clock cycle overriding 1’ s at the end of byte because
(a) Receiver is to be synchronized for byte reception.
40. Which of the following architecture is not suitable
(b) Receiver recovers lost `0’ and `1’ from these
for the following SIMD architecture?
padded bits.
(a) Vector processor (b) PLA-based processor (c) Padded bits are useful in parity computation.
(c) Von Neumann (d) PAL-based processor (d) None of these.
(c) the register containing the address of the oper- (a) A - 4, B - 3, C -1, D - 2
and is specified inside the instruction. (b) A - 2, B - 1, C -3, D - 4
(d) the location of the operand is implicit. (c) A - 4, B - 3, C -2, D - 1
(d) A - 2, B - 3, C -4, D - 1
14. What are the states of the auxiliary carry (AC)
and carry flag (CY) after executing the following 19. I/O redirection
8085 program? (a) implies changing the name of a file
MVI H, 5DH (b) can be employed to use an existing file as input
MIV L, 6BH file for a program
MOV A, H (c) implies connecting two programs through a pipe
ADD L (d) none of the above
(a) AC = 0, CY = 0 (b) AC = 1, CY = 1 20. The main difference(s) between a CISC and a RISC
(c) AC = 1, CY = 0 (d) AC = 0, CY = 1 processor is/are that a RISC processor typically
17. A microprogram control unit is required to gener- address instructions are required to evaluate it?
ate a total of 25 control signals. Assume that during (a) 4 (b) 6 (c) 8 (d) 10
any microinstruction at most two control signals
are active. Minimum number of bits required in 24. A decimal number has 64 digits. The number of bits
the control word to generate the required control needed for its equivalent binary representation is
signal is . (a) 200 (b) 213 (c) 246 (d) 277
18. The correct matching for the following pairs is 25. Determine the speed up obtained from pipelining if
latencies for each stage in single cycle processor is
A. DMA I/O 1. High-speed RAM given as:
B. Cache 2. Disk
C. Interrupt I/O 3. Printer IF ID ALU MEM WB
D. Condition code 4. ALU 45 ns 20 ns 52 ns 44 ns 18 ns
register
Set 1
11. (c) 18. (d) 25. (a) 32. (b) 39. (b)
12. (c) 19. (b) 26. (b) 33. (b) 40. (c)
13. (b) 20. (a) 27. (a) 34. (c) 41. (b)
14. (d) 21. (a) 28. (c) 35. (c) 42. (a)
15. (a) 22. (b) 29. (b) 36. (d) 43. (a)
16. (a) 23. (b) 30. (c) 37. (c) 44. (c)
17. (c) 24. (a) 31. (d) 38. (d) 45. (b)
Both operands are positive and the result is negative. 12. (a) Push `r’ memory operation needs 2 clocks.
Both operands are negative and the result is positive. 13. (b) In absolute addressing mode, the address of the
So option (b) is true, overflow does not occur when operand is inside the instruction.
positive and negative numbers are added.
14. (c)
5. (d) Different hazards are caused due to various
dependencies. Different dependencies for pipelined Carry Auxilary carry
processor are as follows: 0 1 1 1 1 1 1 1
Structural dependency is due to different delays in (5D) # (0 1 0 1 1 1 0 1)B
pipelined stages. +(6D) # + (0 1 1 0 1 0 1 1)B
Control dependency is due to consecutive instruc-
tions are dependent on each other. (1 1 0 0 1 0 0 0)B
Data dependency is due to hardware resources
AC = 1 and C Y = 0
sharing.
15. (d) Features of horizontal microprogramming are
6. (c) (A) IEEEE 488 - (Q)
(B) IEEEE 796 - (S) (i) It does not require use of signal decoders
(C) IEEEE 696 - (R) (ii) It results in larger-sized microinstructions
(D) RS232-C - (P) than vertical microprogramming
(iii) It uses 1 bit for each control signal
7. (c) When interrupt is caused, the execution of cur-
rent instruction is stopped. After handling inter- 16. (a) The daisy chaining method of establishing
rupt, the program resumes its execution. priority consists of a serial connection of all devices
that request an interrupt. The device with the
8. (a) RAID is random array of independent disks highest priority is placed in the first position, fol-
that combines multiple disk drive components into lowed by lower-priority devices up to the device
a logical unit. RAID configuration provides fault- with the lowest priority, which is placed last in
tolerance and high speed. the chain. The farther the device is from the first
9. (b) Horizontal micro programming has high paral- position, the lower is its priority. Therefore, daisy
lelism than vertical. So, the speed order is: chain gives non-uniform priority to various devices.
17. (10) To generate 25 control signals, 5 bits are 22. (b) Three-address instructions are as follow:
required. To generate two control signals, the fol-
MUL R1, N, O
lowing scheme will be used.
MUL R2, P, Q
ADD R3, M, R1
5 bits to identify first and 5 bits to identify second DIV X, R3, R2
including the case when one of them is not present.
So total bits required = 10. 23. (c) LOAD P (AC ← P)
18. (b) DMA I/O - Disk MPY Q (AC ← AC × Q)
Cache - High-speed RAM STORE X (X ← AC)
Interrupt I/O - Printer LOAD N (AC ← N)
Condition code register - ALU MPY O (AC ← AC × O)
19. (c) I/O redirection implies connection two pro- ADD M (AC ← AC + M)
grams through a pipe. DIV X (AC ← AC/X)
20. (d) The major characteristics of a RISC processor are STORE X (X ← AC)
(i) Relatively few instruction 24. (b) The number of bits is
(ii) Relatively few addressing modes
(iii) More registers 1064 − 1 = 2x − 1
(iv) Hardwired rather than microprogrammed ⇒ 1064 = 2x = x = log2 1064 ≈ 213
control
21. (a) Number of bytes per line = 16 bit × 4 byte = 25. (3.44) Speed up obtained by pipelining
8 bytes
(45 + 20 + 52 + 44 + 18)
Cache size = 8 × 4 × 1024 = 32 KB =
52
= 3.44
12
Number of questions
10
8
6 Marks 1
Marks 2
4
Total number of questions
2
0
2015 2014 2013 2012 2011 2010
Year Concept
2015 Binary tree, Minimum-maximum heap, B+tree, Functions, Recursion, Kth Smallest
element in C, Hashing, Stack, C strings, Postfix equation, Binary search tree,
Merge sort, Pointers, Switch-case, Storage classes, Quick sort, Array, Complexity
2014 Loops, Inorder traversal, Binary search tree, Tree traversal
2013 Binary search tree, Heap sort, Control statements, Recursion, Queues, Array and
Pointer
2012 Switch case, Queue, Trees, Functions, Storage classes
2011 Array, Heaps, Binary search tree, Recursion
2010 Binary tree, Pointers, Heaps, Recursion, Link list
Syllabus: Programming and data structures: programming in C; functions, recursion, parameter passing, scope,
binding; abstract data types, arrays, stacks, queues, linked lists, trees, binary search trees, binary heaps.
writing such a code (or a program) is called program- 1. Procedural Languages: These are those
ming. According to famous programmer Niklaus Wirth, languages which follow a certain procedure. These
are three different types-:
•• Algorithmic languages: The programmer specifies
PROGRAMS = DATA + ALGORITHMS
Every programming language has two important pil- the steps (algorithm) to be followed for accom-
lars- syntax (refers to grammatical representation) and plishing a particular task. Such languages have
semantic (refers to the meaning). A correct program built-in expressions, functions and procedures.
should be correct from syntactic view as well as from For example, C, COBOL, PASCAL.
semantic view. The different kinds of translators check •• Object-oriented languages: Instead of using func-
only the syntactic representation and the programmer tions, objects are used. The data and the func-
takes care of the semantic representation. There are dif- tions are combined in defining an object. Some
ferent generations of programming languages: of the important features are: Abstraction,
1. First Generation/1GL/(Machine level pro- Encapsulation, Polymorphism and Inheritance.
gramming): The program is written in a sequence For example, C++, Java.
of 0s and 1s (binary number system). Such a •• Scripting languages: It is a kind of language that
language is more closer to the machine and the combines different components together to per-
programs are machine dependent and difficult to form a difficult task. For example, Visual Basic
debug. The only advantage is that there is no need script, PERL.
for translator software (as the program is already 2. Non-Procedural Languages: 4GL languages
in machine code) and execution is very fast. are defined as non-procedural languages. These are
2. Second Generation /2GL/(Assembly level two different types-:
•• Functional language: The program consists of
programming): The program is written using
instructions based on mnemonics and hexadecimal
functions, such languages are used in the field
number system. Such a language is also machine-
of artificial intelligence. For example, LISt Pro-
dependent. Assembler is the translator used to
cessing (LISP).
•• Logic-based language: It is a set of rules that
convert assembly level code to machine level code.
The programs more reader friendly so are easier to
are defined and the answer is retrieved using
debug.
if−then rules. For example, PROgramming in
3. Third Generation /3GL/(High-level pro-
LOGic (PROLOG).
gramming): The program is written using
English-like statements and decimal number 3. Problem-Oriented Languages: Especially designed
system. Such a language is more closer to the user to solve certain kind of problems. These are of two dif-
and the programs are machine independent. There ferent types:
is a need of translator (compiler/interpreter) to •• Numerical problems: These contains the built-in
convert the high- level code to machine level code. functions, used to solve mathematical problems.
Such languages are user-friendly. Such languages For example, Mathematica.
are file-oriented. •• Publishing: It contains the built-in functions,
4. Fourth Generation /4GL/(Non-procedural used in publishing industry. For example,
programming): Such languages allow the user to LATEX.
specify the result instead of the describing how the
result is to be obtained. All kinds of query lan- 3.2.3 Procedural Programming and Object-
guages belong to this generation. Such languages Oriented Programming
are database-oriented.
5. Fifth Generation /5GL/(Natural Language 3.2.3.1 Procedural Programming
Programming): They are similar to 4GL and
eliminates the user or programmer to learn a spe- It uses a list of instructions in a stepwise manner to
cific language. The language used, resembles the computer. It relies on procedures. Procedures are also
human speech closely. Example- CLOUT, Q & A, known as routines or subroutines. A procedure consists
HAL (Human Access Language). of a series of computational steps that has to be carried
out. Procedural programming languages are also known
as top-down languages.
3.2.2 Classification of High-Level Languages
3.2.3.2 Object-Oriented Programming
Depending upon their usage, the high-level languages
are classified as: procedural, non-procedural and problem Object-oriented programming (OOP) is a problem-
oriented languages. solving approach in which all computations are carried
out using objects. An object is an entity of a program. 2. Modularization: Dividing the program into
It is used to perform actions and interactions with other small independent modules.
elements of the program. These are the fundamental
units of OOP, for example, a person. 3.2.4 Data Types
The other fundamental concepts of OOP are as
follows: It constitutes the type of values a variable can take and
a set of operations that can be applied to those values.
1. Class: It is a blueprint for an object. It does not
Data types in programming language can be broadly
define any data, rather it will define the object of
classified as shown in Fig. 3.1.
the class it consists and the operations performed
on the object.
2. Abstraction: It is used to provide essential char
information in a program without presenting the integer type
details. For example, in a database system, only int
data is shown, all the information regarding creat-
ing, storing, etc., is hidden. float
3. Encapsulation: It is used to place data and func- Basic data
tions at the same place. types float type
4. Inheritance: It is the process of forming a new class double
from an existing class. The new class is known as the bool
derived class and the existing class is known as the
base class. It uses the concept of reusability of code. void
5. Polymorphism: It is used to provide different
meanings or functions to the existing operators or array
functions.
6. Overloading: It is a branch of polymorphism. Data type Derived data
types function
When the existing operator or function is made to
operate on a new data type, then it is known as
overloading. pointer
3.2.3.3 Structured Programming
structure
A kind of programming approach that revolutionized
the programming concept, proposed by E. Dijkstra’. union
User defined
In 1968, Dijkstra submitted the famous article in data types
ACM Journal, highlighting the drawbacks of using the class
GOTO statement. He advocated that program written
without the use of GOTO statement (or JUMP state- enumeration
ment) were more stable. Nowadays, more and more
Figure 3.1 | Classification of data types in
programmers are following such kind of approach in
writing programs. Other notable attributes of struc- programming languages.
tured programming are:
1. Top-down analysis: A large problem is sub- Table 3.1 gives detail about basic data types with its
divided into smaller sub-problems. storage sizes and value ranges.
Table 3.1 | Basic data types, their storage size and value ranges
Type Storage Size Value Range (Depends on Compiler Type)
char 1 byte −128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte −128 to 127
int 2 or 4 bytes −32,768 to 32,767 or −2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
(Continued)
C language is a weakly typed language so it allows or take decisions and branches. For that purpose, any
implicit and explicit type conversions (Fig. 3.2). The programming language provides control flow statements
compiler promotes each term in a binary expression to that specifies the control flow of program or the order of
the highest precision operand. execution of statements (Fig. 3.3).
Programs consist of several statements and execution is These are used to execute a statement or a group of
not limited to a linear sequence of statements. During statements on the basis of certain conditions. These con-
its execution, a program may repeat segments of code, ditions are as follows:
char
int
unsigned int
Wide conversion Narrow conversion
(High precision) long
(Low precision)
unsigned long
long long
float
double
long double
Control statements
Conditional/ Unconditional
while
Decision control branch
if goto for
nested if continue
else-if return
switch
1. If: When the condition is satisfied then the state- 3.2.5.2 Loops
ments inside the parenthesis {} will be executed.
2. If-Else: If the condition is true then the state- These are used to execute a block of code several number
ments between if and else will be executed. If it of times. The statements are executed sequentially.
is false then the statement after else will be exe- Loop executes a statement or a group of statements
cuted. In nested if, one or more if-else block(s) lie multiple times. When the execution of statements in
within the parent if statement. the loop is unknown, then this concept is known as
3. Else-If: If the condition is true then the state- odd loop.
ments between if and else if will be executed. If it Loops are of the following three types:
is false then the condition in else if is checked, and
if it is true it will be executed. 1. while loop: It repeats a statement or group of
4. Switch: It is also known as matching case state- statements while a given condition is true. It tests
ments. It is used to test for equality against a list the condition before executing the loop body.
of values. It matches the value in variable with 2. for loop: It executes a sequence of statements
any of the case inside the switch. If it matches the multiple times.
case defined in the switch then that match will be 3. do … while loop: It is similar to while statement,
executed. If none of the cases match the statement except that it tests the condition at the end of the
then default will be executed. loop body.
Loops can be nested, that is, execution of one loop inside Note that the first element is numbered 0 and so on.
another loop. Here, the size of the array age is five times the size of
There are various control statements used for changing int because there are five elements.
execution from its normal sequence. These are as follows: Starting address of an array is also known as the base
1. break statement: It terminates the loop or address of the array. Suppose, the starting address of
switch and transfers the execution to the statement age[0] is 1000 and the size of int is 4 bytes. Then, the
immediately following the loop or switch. next address (address of age[1]) will be 1004, address of
2. continue statement: It skips the remainder of age[2] will be 1008 and so on.
the loop body and immediately begins execution
from the top.
3.2.6.2 Important Things to Remember in
3. goto statement: It transfers control to the
labelled statement. C Arrays
4. return statement: It transfers control from the
Following are the important points for arrays:
function to the calling function.
1. Suppose you want to declare an array of 10 stu-
3.2.6 Array dents. For example: arr[10]. Only array members
from arr[0] to arr[9] are used. But, if element arr[10],
Array handles similar types of data. For example, stor- arr[13], etc. are used, then the compiler may not
ing marks of 100 students. show error but may cause fatal error during pro-
An array is a set of homogenous elements, which uses gram execution.
contiguous memory locations. The lowest address cor- 2. The size of the array may not be defined during
responds to the first element and the highest address to initialisation. For example,
the last element. int age[] = {2, 4, 35, 5, 8};
3.2.6.1 Declaring an Array In this case, the compiler determines the size of the
array by calculating the number of elements of an
Array can be declared as follows: array.
type arrayName [arraySize]; 3. If we do not provide value to any position in the
where array it will store 0 by default. For example,
1. type represents the type of element an array stores. int age[5] = {2, 4, 35, 5};
For example, if array stores integer elements then
In this above case, first four places in the array will
type of array is `int’.
be filled by given values and the last will be filled
2. arrayName represents the name given to the array.
by 0.
It can be any string.
3. arraySize represents the number of elements the int age[5]= { };
array stores. It is showed in [].
Now array age is initialised with 0 at every
For example, an array of 10 integers can be defined as position.
follows:
int arr[10] 3.2.6.3 Address Calculation in an Array
Arrays are of the following two types:
The address can be calculated as follows:
1. One-dimensional arrays
2. Multi-dimensional arrays
One-Dimensional Arrays
Initialisation of One-Dimensional Array In one dimension, an array `A’ is declared as follows:
Arrays can be initialised at declaration time in this A[lb… …ub]
source code as follows (Fig. 3.4):
int age[5] = {2, 4, 35, 5, 8} where lb is the lower bound of the array and ub is the
upper bound of the array.
age[0] age[1] age[2] age[3] age[4]
Suppose we want to calculate the ith element address,
2 4 35 5 8 then
Figure 3.4 | Initialisation of one-dimensional array. Address (arr[i]) = BA + (i − lb) * c;
where BA is the base address of array, lb is the lower 3.2.7 Function and Recursion
bound of array and c is the size of each element.
A function is a group of statements, which is used to
Two-Dimensional Arrays perform a task and recursion is a programming tech-
nique, which is used to express operations in terms of
In two dimensions, an array `A’ is declared as follows: themselves.
A[lb1 … …ub1][lb2 … … ub2]
where lb1 is the lower bound for row, lb2 is the lower 3.2.7.1 Function
bound for column, ub1 is the upper bound for row and
ub2 is the upper bound for column. A function is a set of statements that together perform
a task. Every C program has at least one function which
1. Row Major Order: is main(), and programs can have additional functions.
Address (arr[i][j]) = BA+ [i-lb1) * Nc+ In other words, a function is a self-contained block,
(j-lb2)] * c which takes some input and gives output after processing
2. Column Major Order: (Fig. 3.5). A function is known with various names such
as a method or a sub-routine or a procedure, etc.
Address (arr[i][j]) = BA + [j - lb2) * Nr +
(i - lb1)] * c
Input x
where BA is the base address, Nr is the number of
rows = (lb2 − lb1 + 1) and Nc is the number of
columns = (ub2 − ub1 + 1). Function F
Output ƒ(x)
Problem 3.1: Consider the following array:
a[1 … … 500] Figure 3.5 | A view of function.
Base address = 1000
Size of each element = 3 bytes
Find address of a[397]. Example 3.1
Solution: intavg(inta,intb,intc){
float result;
Address (a[397]) = BA + (i − lb) * c result = (a+b+c)/3;
= 1000 + (397 − 1) * 3 return result;
}
= 1000 + 396 * 3
= 2188
Parts of a Function
A function has the following parts:
Problem 3.2: Consider the following array:
1. Function prototype
a[50 … … 299][299 … … 500] 2. Definition of a function
Base address = 0 3. Function call
4. Actual and formal arguments
Size of each element = 5 5. Return statement
Find the address of a[250][499] by row major order.
These parts have been discussed in detail as follows:
Solution: 1. Function prototype: Function prototype con-
Address(arr[i][j])= BA +[i−lb1)* Nc tains function declaration, which tells the compiler
+(j−lb2)]* c about a function name, return type, argument
Address(a[250][499])= BA +[(250−50)* 201 types and function calling. Function declaration
is necessary before calling it. A full declaration
+(499 − 299)] * 5
includes the return type and the type of arguments.
= 0 +(200 * 202 + 200)* 5 Function declaration will be as follows:
= 203000
return_type function_name (parameter list);
For the above definition of function, the avg() 5. Return statement: When a return statement
function declaration is as follows: gets executed, then the function returns the value
intavg(int a, int b, int c); and transfers the control to the calling function.
Execution continues in the calling function by
In function declaration, the arguments are not following the remaining statements. A return state-
important, only their type is required. So, the ment can also return a value to the calling function.
following declaration is also correct:
intavg(int, int, int); Types of C Functions
2. Definition of a function: Function definition The following are two types of functions in C on the
consists of the following parts: basis of whether it is defined by a user or not:
•• Function name: This is the actual name of the
1. Pre-defined function: The function whose
function. It is unique. Function signature is con-
definition is already stored in the library of the
stituted with the function name and parameter
respective language is called pre-defined func-
list together.
tion. These functions are provided in the system
•• Parameters: At the time of function declara-
or particular language library. For example, in C
tion, the parameters are passed into the func-
language, printf(), scanf(), gets(), puts() are avail-
tion. The parameter list shows the type, value
able in stdio.h header file. These functions are also
of parameter and number of arguments in the
called library functions or built-in functions.
function. Parameters field is optional so that a
2. User-defined function: User-defined functions
function may contain no parameters.
are functions created by the user at the time of
•• Function body: The function body contains a
writing the program for modularity purpose. For
collection of statements that defines what the
example,
task is performing by that function.
•• Return type: A function returns a value. The int max (int num1, int num2){
return type is that data type in which a function int result;
should return a value. Some functions perform if(num1 > num2)
the desired operations without returning a value. result = num1;
There are two possibilities, the function either else
returns a value or not. When the function does result = num2;
not return a value then the void data type used. return result;
}
In the above example, the function name is avg,
the function has three integer-type parameters and
the function calculates the average of those three Function Advantages
numbers. Function has the following advantages:
3. Function call: In function call, the control is 1. It provides the reusability of code in an easy manner.
transferred to the called function when the pro- 2. It makes debugging and editing easier.
gram calls the respective function. The name of 3. It reduces the size of a program.
the function must match with the function exactly 4. It makes the logic of the program easier to design
with the name defined in the function prototype. A and understand.
called function performs a defined task, and when
its return statement is executed, it returns program 3.2.8.2 Recursion
control back to the main program. To call the func-
tion, the first step is to pass the arguments with When the function calls itself then it is called recursion.
function name, and then the return value stores in In C language, there is provision for the function to call
a specific manner. itself. When using the recursion, it is necessary to have
4. Actual and formal arguments: A parameter is exit condition from the function; otherwise, the result
a special kind of variable used in a function to refer will be infinite.
to the data provided as input to the function. These
pieces of data are called arguments. Syntactically, Writing Recursive Functions
we can pass any number of parameters to a function.
Parameters are specified within a pair of parenthe- A recursive function has the following two parts:
sis. These parameters are separated by commas (,). 1. Base case: It is the non-recursive solution and a
Parameter written in a function definition is called stopping condition.
`formal parameter’. Parameter written in a func- 2. Recursive case: It is the recursive solution of the
tion call is called `actual parameter’. problem.
3.2.9 Parameter Passing In the call-by-value mechanism, only the formal argu-
ment (variables receiving value in called function from
There are various types of parameter-passing techniques calling function, that is, x and y in the above program)
in programming languages. The parameter passed is gets changed and do not reflect in actual argument (vari-
based on the program requirement. According to the able in function call, that is, a and b in function call to
parameter passing, the passing parameter depends on exchange(a, b)).
the way the calling function calls the called function.
The C programming language supports the following 3.2.9.2 Call-by-Reference Parameter-
two types of parameter-passing techniques: Passing Technique
1. Call by value In call by reference, the parameter is passing through the
2. Call by reference actual address of the value. The address of the variable
never changes throughout the execution of the program.
3.2.9.1 Call-by-Value Parameter-Passing There is a possibility to access and modify the value of
Technique the variable by using a pointer. In case of any changes
in the parameter inside the function, there is a change of
When we call a function, we pass the parameter to the the actual parameter. For example, the declaration of a
called function and it is by default passed by the value variable is as follows:
of variable in the calling function. This is called `call by
int x = 20; which tells the compiler:
value’.
1. To allocate 2-byte memory location on stack frame
of that particular function in which it is declared.
Example 3.4
2. To hold value of type integer and also associate
void exchange(int x, int y); this address with name x.
main() 3. This memory location is initialised with value 20.
Output
Figure 3.6 | Memory map in call by reference. a = 10 b = 20
a = 20 b = 10
The selection of address for a particular variable is com-
piler dependent and is always a whole number. In Fig. 3.6, Consider the declaration of a variable. int *x; does
the value 20 can be accessed by using variable name x not mean that x is going to hold integer value, rather it
and address 1000. The symbol `&’ is used to represent simply means it is going to hold address (memory loca-
the `address of’. tion) of integer type of variable. We are passing address
For example, expression &x in the above case returns of variables a, b to function exchange(), so while receiv-
address of variable x, which happens to be 1000. ing we are using two pointers to integer.
An address is always positive, we use operator %u for Generally, we use call-by-value mechanism if we do
unsigned integer. *’ is a special operator in C used as not need the changes made in calling function to reflect
`value at address’. It is also called indirection operator. in the called function. But if we require the changes to
For example, *(&x), which means value at reflect in the called function, we use the call-by-reference
address(&x), that is, value at address (1000). The exam- mechanism.
ple given below returns the address of a variable, the Also, if we want to return more than one value to the
value of the variable and the value of variable using called function from the calling function, we go for call-
indirection operator. by-reference value, which is not possible ordinarily.
3.2.12.1 Local Variables
3.2.10 Structures and Unions
A local variable is that variable which is declared in
Structures are used to represent a record (e.g. library). A the body of the main function. It can be used only by
union is a special data type in C, which is used to store the function in which it declares. Other function has no
different data types in the same memory location. Table information about local variable. Formal parameter of a
3.2 shows the comparison of the structures and union. function is also treated as a local variable to that func-
tion. In the following example, num, sum, r are local
Table 3.2 | Comparison between structure and union variables.
Structure Union
Example 3.6
1. T
o define a structure, 1. T
o define union, the
the `struct’ keyword `union’ keyword is #include<stdio.h>
is used. used. int main(){
/* Local Variable Declaration*/
2. W
hen a structure 2. W
hen a union int num,sum=0,r;
variable is created, variable is created, printf("Enter a number: ");
the compiler allocates the compiler allocates scanf("%d",&num);
memory for this memory for that while(num){
variable equal to variable equal to the r=num%10;
the sum of size of maximum size of num=num/10;
structure elements. union element. sum=sum+r;
3. E
ach structure element 3. Individual members }
within a structure is of the union share the printf("Sum of digits of number: %d",sum);
assigned to unique memory allocated. return 0;
}
memory location.
4. M
ore than one 4. O
nly one member
structure element can can be initialised at a Variables num, sum and r are local to the function main(),
be initialised at once. time. and cannot be accessible outside the main function.
5. Individual members 5. O
nly one member can
can be accessed at a be accessed at a time. 3.2.12.2 Global Variables
time.
Global variables are those variables which are defined
outside the body of the main function. The global
3.2.11 Enumerated Data Types variables will hold their value throughout the lifetime
of the program. They can be accessed inside any of the
Enumerated data types assume values which are previ- functions defined for the program. Following is an exam-
ously declared. For example, ple using local variable and global variable.
•• If there is more than one executable using the The basic operations on stack are as follows
module at the same time, then multiple copies 1. PUSH: To add an element to the stack.
of it are used in memory. 2. POP: To remove an element from the stack.
3.2.13.2 Late Binding
3.3.1 PUSH Operation on Stack
In late binding, the linker copies only a stub code for the
referenced module into the executable image. It is also When we try to add elements to the stack then
known as dynamic binding. The stub code loads the ref- the o peration is called PUSH operation on stack
erenced module into memory at load/run time. (Fig. 3.7). The element is placed only on the top of
the stack and then the stack position gets increased
1. Advantages of late binding: by 1.
•• The size of the executable image is small.
•• On changing the referenced module, re-link of
the executable is not required.
•• Most operating systems can share the referenced
module. D
2. Disadvantages of late binding: C D C
•• The executable is dependent on the shared B PUSH B
library at runtime. A A
•• Since the module is shared, it must be re-entrant STACK BEFORE STACK AFTER
and thread safe. Figure 3.7 | PUSH operation on stack.
EMPTY
STACK
TOP 15
12 TOP 12
10 10 10 TOP
5 5 5 5 TOP
Following are the applications on stack: (a) If the operator on the top of the stack has
lower precedence, then push the new opera-
1. Parsing: Stacks are used by compilers to check
tor onto the stack.
the syntax of a program and for generating execut-
(b) Else, pop the operator from the top of the
able code.
stack and append it to the output string.
2. Reversing a list: Stack can be used for reversing
Then push the new operator onto the
a list.
stack.
3. Calling function: When a function is called all
local storage for the function is allocated on the (c) Otherwise,
stack and return address is also stored on the stack. (i) If the token is an open parenthesis, then
4. Recursive function: Stack can be used for push it onto the stack.
implementing recursive functions. (ii) If the token is a close parenthesis, then
5. Expression conversion: pop and append the operators from
•• Infix: An infix expression is one in which opera- the stack until the open parenthesis is
tors are located between their operands. This is popped. Discard both parentheses.
Solution:
(A + B) * (C − D) Empty Initial
A + B) * (C − D) ( Push
+ B ) * (C − D) ( A Print
B) * (C − D) (+ A Push
) * (C − D) (+ AB Print
Infix to Prefix Conversion 3. Apply algorithm infix to postfix on the expression
that comes from step 2.
Algorithm for infix to prefix conversion:
4. And finally reverse the output of step 3.
1. Reverse the given infix expression.
2. Make every `(` as ’)’ and every `)’ as `(’.
Solution:
Step 1: Reverse the infix expression.
5 ∧E + D*) C ∧B + A (
Step 2: Make every `(` as ’)’ and every `)’ as `(’
5 ∧E + D * (C ∧B + A)
Step 3: Convert expression to postfix form.
Table | Continued
Expression Stack Output Comment
D * (C ∧B + A) + 5E ∧ Pop and push
∧ ∧
*(C B + A) + 5E D Print
(C ∧B + A) +* 5E ∧D Push
C ∧B + A) +*( 5E ∧D Push
∧ ∧
B + A) +*( 5E DC Print
∧ ∧
B + A) +*( 5E DC Push
∧ ∧
+ A) +*( 5E DCB Print
∧ ∧
A) +*(+ 5E DCB Pop and push
∧ ∧
) +*(+ 5E DCB A Print
∧ ∧
End +* 5E DCB A+ Pop until `(’
∧ ∧
End Empty 5E DCB A+*+ Pop every element
A D I
A
C E H
C X
F B G A D I C E H
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
E M S O Figure 3.17 | Storage representation.
3. Inorder V = {a, b, c, d, e} and E = {(a, b), (a, c), (a, d), (b, e),
•• Traverse the left-most subtree starting at the (c, d), (c, e), (d, e)}
left external node
•• Traverse the root
3.6.6.1 Basic Terminology
•• Traverse the right subtree starting at the left
external node Following are the basic terms used for graphs (see
figure 3.19):
Problem 3.6: Consider the given tree and find the 1. Undirected graphs: It consists of a set V
inorder, preorder and postorder of the tree. of vertices and a set E of edges, each of them
connecting to two different vertices.
9 2. Directed graph: In this, edges provide a
connection from one vertex to another, but not
4 17 necessarily in the opposite direction.
3. Adjacent vertices: Connected by an edge.
4. Degree of a vertex: It is defined as the number
3 6 22 of adjacent vertices.
5. Path: It is defined as a sequence of vertices such
5 7 20 that consecutive vertices are adjacent.
6. Simple path: Path with no repeated vertex.
7. Cycle: A simple path, where the first and last
Solution: vertex are same.
8. Connected graph: When any two vertices are
Inorder Traversal: connected by some path.
The inorder traversal of the above tree will output: 9. Subgraph: When a subset of vertices and edges
form a graph.
3, 4, 5, 6, 7, 9, 17, 20, 22
10. Tree: A connected graph without cycles.
Preorder Traversal: 11. Balanced binary tree: A binary tree in which no
The preorder traversal of the above tree will output: leaf is at much greater depth than any other leaf.
Example of balanced binary search trees are red-
9, 4, 3, 6, 5, 7, 17, 22, 20 black trees, AVL trees, etc. Examples of non-binary
Postorder Traversal: balanced search trees are B trees, B+ trees, etc.
The postorder traversal of the above tree will output:
3, 5, 7, 6, 4, 20, 22, 17, 9 3.6.6.2 Traversing a Graph
a b a b a b a b a b
c d c d c d c d
d e Connected Path Disconnected Tree
Figure 3.18 | A graph G = (V, E). Figure 3.19 | Examples showing connected, path, disconnected and tree graphs.
Example 3.9 2
b
1 c
b
a
c e
a h
e
d i
i
g
f
f
(iii)
2
b b
1
a
a c
e
Q = {a}
i
i
3 Q = {f,i,c,e}
f
f
(i) (iv)
b b
1
a
c
a
i
i
Q = {b,f,i} f g
3
(ii)
f
(v)
2 2
b b 5
1 c
c
1 a 6 h
8
e
a d
4
e
i 7
g
3
i
g f
3
(ix)
f
Q = {i,c,e,g}
(vi)
b
2
b 5
a c
c
1 h
e
a d
i
e d
g
4
i f
Q = {h}
g
3 (x)
f
(vii)
b
b c a c
e
e h
a d
i d
g f g
i
f Q = {e,g,d} {a,b,f,i,c,e,g,d,h}
(viii) (xi)
2. Depth first traversal: It is a preorder traversal before returning to the current node and then tra-
of an ordered tree. It first traverses one subtree verses another subtree. It uses stack.
Example 3.10 2
b
1
b
a c
a c e
e h i
i
d
g f
f (iii)
2
b b
1 1 3
a a c
i i
f f
(i) (iv)
2 2
b b
3
1 1 c
a a
e
d
i i
f
f
(ii) (v)
2 2
b b 3
3 c
1 1
a c h
a e
4
4
d
e
d
i i 5
g
f
f
(vi) (ix)
2 2
b b 3
3
1 c h
c 1 6 4
a h
a e d
e 4
d 5
i
i g
g
f f
(vii) (x)
2 2
b b 3
c h
1 3 1 6 4
a c a e
h d
4 5
e d
i g
i
5
g
7
f f
(viii) (xi)
2 Source
b 8 a
3
h 2
c 5
1 6 4 3
a e 6
d e b
5 10
1
i g 4
2
d c
7
2
Figure 3.20 | Graph for finding the shortest path.
f
(xii)
5 Source
g a
7
2
f 5
3
(xiii) 6
e b
10
b 1
4
2
a c h d c
2
e d
a
i g S = {a}
5
f 2
{a,b,c,d,g,e,f,h,i}
e b
(ix) d=2 d=5
3
3.6.6.3 Shortest Path
d c
For a given directed graph, where each edge has a non-
negative weight or cost, the shortest path problem is to d=∞ d=3
find a path of least total weight from the source to every
other vertex in the graph (Fig. 3.20). (i)
3.6.6.4 Spanning Tree
a
5 S = {a,e} A spanning tree of a graph G is a tree which contains all
2
3 the vertices of the graph G. See Fig. 3.21.
6
d=2 e b d=5 a b a b
10
4
c d e c d e
d c
Figure 3.21 | Spanning tree of a graph.
d=6 d=3
(ii)
Minimum Spanning Tree
a Minimum spanning tree in an undirected connected
S = {a,e,c} weighted graph is a spanning tree with minimum weight.
2 5
3 Prim’s algorithm:
e 1. Begin with all the vertices.
d=2 b d=4
2. Choose and draw any vertex.
3. Find the edge of least weight joining a drawn
4 vertex to a vertex not currently drawn. Draw the
1
weighted edge and the corresponding new vertex.
d c 4. Repeat step 3 until all the vertices are connected,
2
d=3 then STOP.
d=5
(iii)
Example 3.12
a S = {a,e,c,b}
b 10 e
2
4 8 7 6
e 3 b d
d=4
a 9 5 g
d=2 6 1 2 9
8 2
d c c 1 f
2
d=5 d=3 (i)
(iv)
Step 1
2
Closest
e b U V−U Low Cost
3 d=4
d=2 b A 4
1 c A 8
d A ∞
d c
2 e A ∞
d=5 d=3
f A ∞
(v) g A ∞
b 10 e 4 8 7 6
4 8 7 6 a 9 g
d 5
d 2 9
a 9 5 g 2
2 9 8
8 2 c 1 f
(iv)
c 1 f
Step 4
(ii)
U = {a, b, c, d}, V − U = {e, f, g}
Step 2 Closest
U = {a, b}, V − U = {c, d, e, f, g}
U V−U Low Cost
e D 7
Closest
f C 1
U V−U Low Cost
g A ∞
C A 8
D B 8 Select vertex f to include in U.
E B 10 b 10 e
F A ∞
4 8 7 6
G A ∞
d
a 9 5 g
Select vertex d to include in U. 2 9
8 2
b 10 e
c 1 f
4 8 7 6
(v)
d
a 9 g Step 5
5
2 9 U = {a, b, c, d, f }, V − U = {e, g}
8 2
Closest
c 1 f
U V−U Low Cost
(iii)
e F 5
Step 3 g F 2
Step 6 4
U = {a, b, c, d, f, g}, V − U = {e} 44
2 3
Closest 17 78
U V−U Low Cost 1 2 1
e F 5 32 50 88
1 1
48 62
Select vertex e to include in U.
b 10 e
Rotation
3.6.6.5 AVL Tree
It is the restructuring of the tree which maintains the
AVL tree is a binary tree which satisfies the height balance binary search tree property.
property (Fig. 3.22). It has a time complexity of O(log(n)). 1. Types of Rotation:
•• Single-Left rotation: Refer Fig. 3.23.
Z
Original tree
Y Y ZR
X Z Single-Left
X ZL
rotation
XL XR ZL ZR XL XR
Original tree XL Y
Y
Single-Right XR Z
X Z rotation
XL XR ZL ZR
ZL ZR
XL X ZR XL ZL ZR YR
Z
ZL ZR XL ZL
3.6.6.6 Binary Heap or equal to the root element. This binary heap (see
Fig. 3.27) is known as min-heap. In addition, if all the
A complete binary tree is said to be binary heap if all elements are lesser or equal to the root element, this is
the elements below in hierarchy are either greater than called max-heap.
1. The height of a binary heap is O(log n). The
runtime of deleteMin, insert and remove is
6
O(log n).
2. The root node is the highest priority element. To
remove the highest/lowest priority object (root),
7 12 heap data structure is preferred. Priority queue is
the best example to be implemented.
3. The running time of heap sort is O(nlog n).
10 15 17
IMPORTANT FORMULAS
1. Insertion/Deletion in array: O(n) where lb is the lower bound of array and ub is the
upper bound of array.
2. Split/Merge in array: O(n)
Suppose we want to calculate the ith element
3. Time complexity of Push(): O(1) address, then
4. Time complexity of Pop(): O(1) Address (arr[i]) = BA + (i − lb) * c
5. Time complexity of Enqueue(): O(1)
where BA is the base address of array and lb is
6. Time complexity of Dequeue(): O(1) the lower bound of array and c is the size of each
7. Number of elements in queue = element
8. Number of nodes in full binary tree: 2h+1 −1 A[lb1 … … ub1][lb2 … … ub2]
[h: levels] where lb1 is the lower bound for row, lb2 is the
9. Number of leaf nodes in full binary tree: 2h lower bound for column, ub1 is the upper bound for
row and ub2 is the upper bound for column.
10. Number of waste pointers in complete binary tree
of n nodes: n + 1 Row major order
Address (arr[i][j]) = BA + [i − lb1) * Nc +
11. Complexity of inorder, preorder and postorder tree
(j − lb2)] * c
traversal: O(n)
12. Minimum number of moves for tower of Hanoi:
2n − 1 Column major order
2n
13. Number of unique binary trees: n
C Address (arr[i][j]) = BA + [j − lb2) * Nr +
n +1 (i − lb1)] * c
14. One-dimensional arrays where BA is the base address, Nr is the number of
In one dimension, an array `A’ is declared as: rows = (lb2 − lb1 + 1), and Nc is the number of
A[lb … … ub] columns = (ub2 − ub1 +1).
SOLVED EXAMPLES
(c) Spanning tree cannot have at most two edges (a) Associativity
repeated. (b) Commutativity
(d) Spanning tree cannot be minimum and (c) Mathematical induction
maximum. (d) Factorial
Solution: A spanning tree is a subgraph of a given Solution: Mathematical induction is the method
graph G, which covers all the vertices of G and through which correctness of recursive function can
should be a tree. Spanning tree could be minimum be proved.
or maximum. Ans. (c)
Ans. (d)
11. In a doubly linked list, the number of pointers
9. Which of the following has compilation error in C? affected for an insertion operation will be:
(a) int n = 17; (a) 4
(b) char ch = 99; (b) 0
(c) float f = (float) 99.32; (c) 1
(d) #include<stdio.h> (d) Depends upon the nodes of the doubly linked
list.
Solution: The syntax for inclusion of header file
is #include <stdio.h>. There should be space Solution: Let a node X be inserted after node 1,
between #include and <stdio.h>. then both pointer of node X(Prv and Nxt) will be
Ans. (d) affected and node 1’s Nxt pointer will point to node
X and node 2’s Prv pointer will also point to node
10. is often used to prove the correctness X. So, total four pointers will be affected.
of a recursive function.
Ans. (a)
12. The queue data structure is to realised by using Solution: Let there are 6 elements (1, 2, 3, 4, 5
stack. The number of stacks needed would be: and 6). First we will push them in stack 1 and then
pop from stack 1 and push in stack2. The same
(a) It cannot be implemented.
behaviour will be observed when we will pop from
(b) 2 stacks.
stack2
(c) 4 stacks.
(d) 1 stacks.
Element6 Element1
Element5 Element2
Element4 Element3
Element3 Element4
Element2 Element5
Element1 Element6
Stack1 Stack2
Solution:
(A*B − (C *D))/(F/D + E)
*B − (C *D))/(F/D + E) A (
B − (C *D))/(F/D + E) A (*
− (C *D))/(F/D + E) AB (*
(C *D))/(F/D + E) AB* (−
*D))/(F/D + E) AB*C (−(
D))/(F/D + E) AB*C (−(*
/(F/D + E) AB*CD (−(*))
/(F/D + E) AB*CD*−
(F/D + E) AB*CD*− /
/D + E) AB*CD*−F /
D + E) AB*CD*−F /(/
+ E) AB*CD*−FD /(/
E) AB*CD*−FD/ /(+
AB*CD*−FD/E /(+)
AB*CD*−FD/E+/
Ans. (a)
14. There are six nodes. The different types of trees Solution: Passing an element of an array by call
that can be realised are . by name behaves similar to call by value. Therefore,
if array elements are passed as parameter then they
(a) 10 (b) 64
can produce different results for call-by-reference
(c) 58 (d) 65
and call-by-name parameter passing.
Solution: 58 trees can be realized. Ans. (d)
Ans. (c)
16. A variant record in Pascal is defined by
15. In which of the following case(s) is it possible to
obtain different results for call-by-reference and typevarirec = record
number: integer;
call-by-name parameter passing
case (var1, var2) of
(a) Passing an expression as a parameter var1; (x, y : integer)
(b) Passing an array as a parameter var2: (p,q: real)
(c) Passing a pointer as a parameter end
(d) Passing an array elements as a parameter end
Suppose an array of 100 records was declared on a {float y (4 bytes) and long z(8 bytes)} 12 bytes.
machine which uses 6 bytes for an integer and 10 So, total size will be 22 bytes (10 + 12).
bytes for a real. How much space would the com- Ans. (a)
piler have to reserve for the array
(a) 3800 (b) 3200 19. In a compact single-dimensional array represen-
(c) 2800 (d) 4000 tation for lower triangular matrices (i.e. all the
elements above the diagonal are zero) of size n × n,
Solution: There are 100 records non-zero elements (i.e. elements of the lower
So 600 B for `record no’: [100 × 6] triangle) of each row are stored one after another,
starting from the first row, the index of the (i,j)th
1200 B for var(x, y): [so, (6 + 6)*100] element of the lower triangular matrix in this new
2000 B for var(p, q) : [so, (10 + 10)*100] representation is
Ans. (a) (a) i + j (b) i + j −1
17. What is the result of the following program? i −1 j ( j − 1)
(c) ( j − 1) + (d) i +
Program side-effect (input, output) 2 2
var x, result: integer;
Function f (var x: integer): integer; Solution: To find location in lower triangular
begin matrix formula is given as
x: x+1;
f:=x; i (i − 1)
LOC (i,j ) = ( j − 1) +
end 2
begin
x:=5; Ans. (c)
result:=f(x)*f(x);
writeln (result) 20. The following sequence of operation is performed
end on a stack:
(a) 5 (b) 25 PUSH(10), PUSH(20), POP, PUSH(10),
(c) 36 (d) 42 PUSH(20), POP, POP, POP, PUSH(20), POP
Solution: Function `f ’ uses `x ’ as local variable. The sequence of values popped out is
So value of `x’ does not change globally and both
(a) 20, 10, 20, 10, 20 (b) 20, 20, 10, 10, 20
the time x = 5 is passed to the function `f ’.
(c) 10, 20, 20, 10, 20 (d) 20, 20, 10, 20, 10
Ans. (c)
Solution:
18. Consider the following C declaration
struct { Operation Stack Popped
short s [5] Elements List
union{
float y; Push 10
long z; Push 10, 20
}u;
Pop 10 20
}t;
Push 10, 10 20
Assume that objects of the type short, float and
Push 10, 10, 20 20
long occupy 2 bytes, 4 bytes and 8 bytes, respec-
tively. The memory requirement for variable t, Pop 10, 10 20, 20
ignoring alignment considerations, is Pop 10 20, 20, 10
(a) 22 bytes (b) 18 bytes Pop 20, 20, 10, 10
(c) 14 bytes (d) 10 bytes Push 20 20, 20, 10, 10
Solution: The structure is created with total Pop 20, 20, 10, 10, 20
size of `short and union’ short have size of 2 byte,
as it is an array of five elements so it will take So, option (b) is correct.
10 bytes. Union will consider the maximum of Ans. (b)
21. The postfix expression for the infix expression Postfix conversion steps
A + B * (C + D)/F + D * E is A + B * CD + F/+ DE*
(a) AB + CD +* F/D + E* A + BCD +*F/DE*+
(b) ABCD + *F/DE *++ ABCD +*F/DE*++
(c) A * B + CD/F * DE++
(d) A + * BCD/F * DE++ So, option (b) is correct.
Ans. (b)
Solution: Given expression A + B * (C + D)/F
+D*E
1. Consider the following C function. Solution: B[1] is address of second one dimen-
float f(float x, int y) { sional array. Address can not be assigned to simple
float p, s; int i; variable. So it will produce compile time error.
for (s=1,p=1,i=1; i<y; i++) { Ans. (a)
p *= x/i;
3. Let T(n) be the number of different binary
s+=p;
search trees on n distinct elements. Then T(n) =
} n
return s; ∑T (k − 1) T (x), where x is
} k =1
For large values of y, the return value of the func- (a) n − k + 1 (b) n − k
tion f best approximates (c) n − k − 1 (d) n − k − 2
(a) xy (b) ex (c) ln(1 + x) (d) xx (GATE 2003: 1 Mark)
(GATE 2003: 1 Mark)
Solution:
Solution: n
Applying iterative method: T(n) = ∑ T (k − 1) T (x)
k =0
i=1 p = x/1 s=1+x T(n) = T(x) [T(0) + … + T(n−1)]
i =2 p = x * x/2 s = 1 + x + x2/2 x=n−k+1
i=3 p = x3/ 6 s = 1 + x + x2/2 + x3/ 6 Ans. (a)
I. Deletion of the smallest element to the start of the pop operation that removes m
II. Insertion of an element if it is not already from S. The average stack-life of an element of this
present in the set stack is
Which of the following data structures can be used (a) n(X + Y). (b) 3Y + 2X.
for this purpose? (c) n(X + Y) −X. (d) Y + 2X
(a) A heap can be used but not a balanced binary (GATE 2003: 2 Marks)
search tree.
(b) A balanced binary search tree can be used but
Solution : Time taken by Pop and Push
not a heap.
opé rations : X + X = 2X
(c) Both balanced binary search tree and heap can
be used. Time delay between two stack operations = Y
(d) Neither balanced binary search tree nor heap Total time taken = 2X + Y
can be used. Ans. (d)
(GATE 2003: 2 Marks)
7. Consider the following 2−3−4 tree (i.e. B-tree with
a minimum degree of two) in which each data item
Solution: Time taken by heap to insert and
is a letter. The usual alphabetical ordering of let-
delete n elements is O(n). Only balanced binary
ters is used in constructing the tree.
search tree takes O(log n) time for insertion and
deletion.
Ans. (b)
(a) P (b) P
GL U HL U
B HI N QT VXZ BG I N QT VXZ
(a) (b)
BGH L N Q T VXZ
Solution: 2-3-4 B tree has minimum degree 2 and maximum 4. According to max degree, atmost 3 elements can
be there in node.
L P U L P
G
VXZ VXZ
BHI BGH
N QT N Q
I P U
BGH L N Q T VXZ
Ans. (c)
Common Data Questions 8 and 9: The following Solution: In dynamic scoping, the variables and
program fragment is written in a programming values are resolved at run time.
language that allows global variables and does not P(100 + 5) = P(105)
allow nested declarations of functions. First Print x + 10 will print 115
global int i = 100, j = 5; Second Print x will update x = 200 + 20 = 220 and
void P(x) { will print 220
int i = 10; Ans. (a)
print(x + 10);
i = 200; 10. In the following C program fragment, j, k, n and
j = 20; TwoLog_n are integer variables, and A is an array
print (x); of integers.
}
The variable n is initialised to an integer ≥ 3,
main() {P(i + j);}
and TwoLog_n is initialised to the value of n
8. If the programming language uses static scoping 2*log2(n)
and call by need parameter passing mechanism, for (k=3; k <= n; k++)
the values printed by the above program are A[k] = 0;
(a) 115, 220. (b) 25, 220. for (k=2; k <= TwoLog_n; k++)
(c) 25, 15. (d) 115, 105. for (j=k+1; j <= n; j++)
A[j] = A[j] || (j%k);
(GATE 2003: 2 Marks) for (j=3; j <= n; j++)
if (!A[j]) printf("% d", j);
Solution: In static scoping, the variables and
values are resolved at compile time. The set of numbers printed by this program frag-
P(100 + 5) = P(105) ment is
Print x + 10 will print 115 (a) {m m ≤ n, (∃i) [m = i!]}
Print x will print 105 (b) {m m ≤ n, (∃i) [m = i2]}
Ans. (d) (c) {m m ≤ n, m is prime}
(d) {}.
9. If the programming language uses dynamic scoping (GATE 2003: 2 Marks)
and call-by-name parameter-passing mechanism,
the values printed by the above program are Solution: The condition ! A[j] is equivalent to A[j] =
(a) 115, 220. (b) 25, 220. = 0. But in array A no element is zero. So it will
(c) 25, 15. (d) 115, 105. print nothing.
(GATE 2003: 2 Marks) Ans. (d)
11. Consider the C program shown below. (a) The list is empty or has exactly one element.
(b) The elements in the list are sorted in non-
#include <stdio.h>
decreasing order of data value.
#define print(x) printf("% d —, x)
int x; (c) The elements in the list are sorted in non-
void Q(int z) { increasing order of data value.
z += x; print(z); (d) Not all elements in the list have the same data
} value.
void P(int *y) { (GATE 2003: 2 Marks)
int x = *y+2;
Q(x); *y = x-1; Solution: The given function returns 1 if the
print(x); elements are unique and are sorted increasing
} order.
main(void) {
x = 5; Ans. (b)
P(&x)
print(x); 13. Consider the following class definitions in a
} hypothetical object oriented language that sup-
ports inheritance and uses dynamic binding. The
The output of this program is language should not be assumed to be either Java
(a) 12 7 6 (b) 22 12 11 or C++, though the syntax is similar.
(c) 14 6 6 (d) 7 6 6
class P Class Q subclass of
(GATE 2003: 2 Marks) { P
void f(int i) {
Solution: { void f(int i)
P(&x) where x = 5 print(i); {
} print(2*i);
x = *y + 2 = 5 + 2 = 7 } }
Q(x) = Q (7) }
→ Here z = 7 Now consider the following program fragment:
→ z = z + x = 7 + 5 = 12 Px = new Q();
→ Print 12 Qy = new Q();
*y = x − 1 = 7 − 1 = 6 Pz = new Q();
print(x) = print 7 x.f(1); ((P)y).f(1); z.f(1);
Value returned by function P is 6. So main will where ((P)y) denotes a typecast of y to P. The
print 6. output produced by executing the above program
So, answer is 12 7 6 fragment will be
14. Which of the following is NOT an advantage ends of the array. Variables top 1 and top 2 (top
of using shared, dynamically linked libraries as 1 < top 2) point to the location of the topmost
opposed to using statically linked libraries? element in each of the stacks. If the space is to be
used efficiently, the condition for `stack full’ is
(a) Smaller sizes of executable files
(b) Lesser overall page fault rate in the system (a) ( top 1 = MAXSIZE/2) and (top 2 =
(c) Faster program startup MAXSIZE/2 + 1)
(d) Existing programs need not be re-linked to take (b) top 1 + top 2 = MAXSIZE
advantage of newer versions of libraries (c) (top 1 = MAXSIZE/2) or (top 2 = MAXSIZE)
(GATE 2003: 2 Marks) (d) top 1 = top 2 − 1
(GATE 2004: 1 Mark)
Solution: The shared, dynamic linked libraries
use smaller sizes of executable files, lesser overall Solution: If we are to use space efficiently then
page fault rate and faster startup of programs. size of any stack can be more than MAXSIZE/2.
Ans. (d) Both stacks will grow from both ends and if any
of the stack top reaches near to the other top then
15. The goal of structured programming is to
stacks are full. So the condition will be top1 =
(a) Have well-indented programs. top2 −1 (given that top1 < top2)
(b) Be able to infer the flow of control from the Ans. (d)
compiled code.
18. The following numbers are inserted into an empty
(c) Be able to infer the flow of control from the
binary search tree in the given order: 10, 1, 3, 5,
program text.
15, 12, 16. What is the height of the binary search
(d) Avoid the use of GOTO statements.
tree (the height is the maximum distance of a leaf
(GATE 2004: 1 Mark) node from the root)?
Solution: The goal of structured programming is (a) 2 (b) 3
that the user can control the flow according his (c) 4 (d) 6
requirement. (GATE 2004: 1 Mark)
Ans. (c)
Solution:
16. Consider the following C function
10
void swap (int a, int b)
{ 1 15
int temp;
temp = a ;
3 12 16
a=b;
b= temp ;
} 5
Ans. (b)
In order to exchange the values of two variables x
and y: 19. Given the following input (4322, 1334, 1471, 9679,
1989, 6171, 6173, 4199) and the hash function x mod
(a) Call swap(x, y)
10, which of the following statements are true?
(b) Call swap(&x, &y)
(c) swap(x, y) cannot be used as it does not return I. 9679, 1989, 4199 hash to the same value.
any value. II. 1471, 6171 hash to the same value.
(d) swap(x, y) cannot be used as the parameters III. All elements hash to the same value.
are passed by value. IV. Each element hashes to a different value.
(GATE 2004: 1 Mark) (a) I only (b) II only
(c) I and II only (d) III or IV
Solution: The value will not be interchanged. The
(GATE 2004: 1 Mark)
values are passed by value, not by reference.
Ans. (d) Solution:
17. A single array A[1..MAXSIZE] is used to implement Mod 10 of all the elements in the list is given below:
two stacks. The two stacks grow from opposite
Solution: Stack is used to check balancing of Solution: A loop invariant is a condition that must
parenthesis. be true before and after each iteration of a loop. So
Ans. (b) n = d1d2…dm−1 and rev = dmdm−1…dm−1+1 pro-
21. Level order traversal of a rooted tree can be done vides the correct loop invariant condition.
by starting from the root and performing Ans. (a)
(a) Preorder traversal. (b) Inorder traversal. 24. Consider the following C program segment:
(c) Depth first search. (d) Breadth first search. char p [20]
(GATE 2004: 1 Mark) char * s = "string";
int length = strlen (s);
Solution: Breath first search gives the level order for (i = 0 ; i < length; i++)
traversal. p[i] = s[length - i];
Ans. (d) print f("%",p);
22. Consider the following C function: The output of the program is
int f(int n) (a) gnirts. (b) String.
{ staticint i = 1 ; (c) gnirt. (d) No output is printed.
if (n >= 5) return n;
(GATE 2004: 2 Marks)
n = n+i;
i++; Solution: In C value of pointer variable cannot be
return f(n); assigned to normal character variable.
} Ans. (d)
The value returned by f(1) is
25. A circularly linked list is used to represent a queue.
(a) 5. (b) 6. A single variable p is used to access the queue.
(c) 7. (d) 8. To which node should p point such that both the
(GATE 2004: 2 Marks) operations enqueue and dequeue can be performed
Solution: in constant time?
Iteration n i
0 1 1
1 1+1=2 2
2 2+2=4 3 Front Rear
3 4+3=7 4
4 7 ≥ 5 Return 7
P ?
Ans. (c)
36. A program P reads in 500 integers in the range 38. Consider the following C program:
[0, 100] representing the scores of 500 students. It double foo(double); /* Line 1 */
then prints the frequency of each score above 50. int main() {
What would be the best way for P to store the double da, db;
frequencies? // input da
(a) An array of 50 numbers db = foo(da);
}
(b) An array of 100 numbers
double foo(double a) {
(c) An array of 500 numbers
return a;
(d) A dynamically allocated array of 550 numbers
}
(GATE 2005: 1 Mark)
The above code compiled without any error or
Solution: We need to know the frequency of num- warning. If line 1 is deleted, the above code will
bers above 50. So, array of size 50 will be sufficient. show:
Ans. (a)
(a) No compile warning or error.
37. Consider the following C program: (b) Some compiler warnings not leading to unin-
tended results.
void foo (int n, int sum 0) {
(c) Some compiler warnings due to type-mismatch
int k = 0, j = 0;
if (n==0) return;
eventually leading to unintended results.
k = n % 10; j = n / 10; (d) Compiler errors.
sum = sum + k; (GATE 2005: 2 Marks)
foo (j, sum);
printf ("%d,", k); Solution: Compilation error will be thrown
} by deleting Line1. Function is called before it is
int main () { declared anywhere.
int a = 2048, sum = 0; Ans. (c)
foo (a, sum);
printf("%d\n", sum); 39. Postorder traversal of a given binary search tree T
} produces the following sequence of keys
What does the above program print? 10, 9, 23, 22, 27, 25, 15, 50, 95, 60, 40, 29
Which one of the following sequences of keys can 43. Consider line number 3 of the following C program:
be the result of an inorder traversal of the tree T ?
int min ( ) { /* Line 1 */
(a) 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95 int I, N; /* Line 2 */
(b) 9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29 fro (I =0, I<N, I++); /* Line 3 */
(c) 29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95 }
(d) 95, 50, 60, 40, 27, 23, 22, 25, 10, 9, 15, 29 Identify the compiler’s response about this line
(GATE 2005: 2 Marks) while creating the object-module:
46. In a binary max-heap containing n numbers, the Solution: Best algorithm to find leader is pass the
smallest element can be found in time array from right side. With each traversal of node,
maximum will be updated. Hence leader can be
(a) O(n) (b) O(log n)
updated in O(n). So, option (b) is correct.
(c) O(log log n) (d) O(1)
Ans. (b)
(GATE 2006: 1 Mark)
50. An implementation of a queue Q, using two stacks
Solution: In max heap smallest element will be S1 and S2, is given below:
present in the leaf node. To get it, n nodes need to
void insert (Q, x) {
be traversed.
push (S1, x);
Ans. (b)
}
47. Let T be a depth first search tree in an undirected void delete (Q) {
graph G. Vertices u and v are leaves of this tree T. if (stack-empty(S2)) then
The degrees of both u and v in G are at least 2. if (stack-empty(S1)) then {
Which one of the following statements is true? print("Q is empty");
return;
(a) There must exist a vertex w adjacent to both }
u and v in G else while (!(stack-empty(S1))){
(b) There must exist a vertex w whose removal x=pop(S1);
disconnects u and v in G push(S2,x);
(c) There must exist a cycle in G containing u• }
and v x=pop(S2);
(d) There must exist a cycle in G containing u and }
all its neighbours in G. Let n insert and m (≤ n) delete operations be
(GATE 2006: 1 Mark) performed in an arbitrary order on an empty
queue Q. Let x and y be the number of push and
Solution: There must exist a vertex w adjacent to pop operations performed, respectively, in the pro-
both u and v in G. So, option (a) is true. cess. Which one of the following is true for all m
Ans. (a) and n?
48. A scheme for storing binary trees in an array X is (a) n + m ≤ x < 2n and 2m ≤ y ≤ n + m
as follows. Indexing of X starts at 1 instead of 0. (b) n + m ≤ x < 2n and 2m ≤ y ≤ 2n
The root is stored at X[1]. For a node stored at (c) 2m ≤ x < 2n and 2m ≤ y ≤ n + m
X[i], the left child, if any, is stored in X[2i] and the (d) 2m ≤ x ≤ 2n and 2m ≤ y ≤ 2n
right child, if any, in X[2i + 1]. To be able to store (GATE 2006: 2 Marks)
any binary tree on n vertices, the minimum size of
Solution:
X should be
Number of push operation = insert operation +
(a) log2 n (b) n delete operation
(c) 2n + 1 (d) 2n − 1 ⇒n + m
(GATE 2006: 1 Mark) ⇒n + m ≤ x
⇒ n + m ≤ x < 2n and 2m ≤ y ≤ n + m
Solution: In case of right skewed tree, the size of Ans. (a)
array will be 2n − 1.
Ans. (d) 51. Consider the following C function in which a[n] and
b[m] are two sorted integer arrays and c[n + m]
49. An element in an array X is called a leader if it is be another integer array.
greater than all elements to the right of it in X.
The best algorithm to find all leaders in an array void xyz(int a[], int b [], int c []){
int i,j,k;
(a) Solves it in linear time using a left to right pass i=j=k=0;
of the array while ((i<n) && (j<m))
(b) Solves it in linear time using a right to left pass if(a[i] < b[j])
of the array c[k++] = a[i++];
(c) Solves it using divide and conquer in time else c[k++] = b[j++];
Θ(n log n) }
(d) Solves it in time Θ(n2) Which of the following condition(s) hold(s) after
(GATE 2006: 1 Mark) the termination of the while loop?
(i) j < m, k = n + j − 1 and a[n − 1] < b[j] if i = n (c) S1 is true and S2 is false
(ii) i < n, k = m + i − 1 and b[m − 1] ≤ a[i] if j = m (d) S1 is true and S2 is true
(a) Only (i) (GATE 2006: 2 Marks)
(b) Only (ii)
(c) Either (i) or (ii) but not both Solution:
(d) Neither (i) nor (ii) S1: True, both work1 and work2 perform the same
task
(GATE 2006: 2 Marks)
S2: True, work2 will improve the performance
Solution: because it reduces calculations using temporary
x[i] ∧ ~y[i]) = only 1’s in x where corresponding variables to store results.
entry in y is 0 → x − y S1 and S2 are correct statements.
~x[i] ∧ y[i] = only 1s in y where corresponding Ans. (d)
entry in x is 0 → y − x. 54. Consider the following code written in a pass-by-
The operator “V” results in union of the above two sets. reference language such as FORTRAN and these
Ans. (c) statements about the code.
52. Given two arrays of numbers a1, ..., an and subroutine swap(ix,iy)
b1, ..., bn where each number is 0 or 1, the fastest it = ix
algorithm to find the largest span (i, j) such that L1 : ix = iy
ai + ai+1 + … + aj = bi + bi+1 + … + bj, or report L2 :iy = it
end
that there is not such span,
ia = 3
(a) Takes O(3n) and Ω(2n) time if hashing is permitted. ib = 8
(b) Takes O(n3) and Ω(n2.5) time in the key call swap (ia, 1b+5)
comparison model. print *, ia, ib
(c) Takes Θ(n) time and space. end
(d) Takes O(√n) time only if the sum of the 2n S1: The compiler will generate code to allocate a
elements is an even number. temporary nameless cell, initialise it to 13 and pass
(GATE 2006: 2 Marks) the address of the cell swap
S2: On execution, the code will generate a runtime
Solution: To find the largest span (i, j), average error on line L1
Θ(n) time and space is required. S3: On execution, the code will generate a runtime
Ans. (c) error on line L2
53. Consider these two functions and two statements S4: The program will print 13 and 8
S1 and S2 about them: S5: The program will print 13 and −2
Exactly the following set of statement(s) is correct:
int work1(int *a, int work2(int *a, int (a) S1 and S2 (b) S1 and S4
int i, int j) i, int j)
(c) S3 (d) S1 and S5
{ {
int x = a[i+2]; int t1 = i+2; (GATE 2006: 2 Marks)
a[j] = x+1; int t2 = a[t1];
return a[i+2] a[j] = t2+1; Solution: S1 and S2 are correct statements.
– 3; return t2 – 3; Ans. (a)
} }
55. Consider this C code to swap two integers and
S1: The transformation from work 1 to work 2 is these five statements: the code
valid, that is, for any program state and input argu- void swap (int *px, int *py) {
ments, work 2 will compute the same output and *px = *px - *py;
have the same effect on program state as work 1. *py = *px + *py;
S2: All the transformations applied to work 1 to get *px = *py - *px;
work 2 will always improve the performance (i.e }
reduce CPU time) of work 2 compared to work 1.
S1: Will generate a compilation error
(a) S1 is false and S2 is false S2: May generate a segmentation fault at runtime
(b) S1 is false and S2 is true depending on the arguments passed
56. Which one of the following is a valid sequence of Solution: Maximum number of binary trees = 2n
elements in an array representing 3-ary max-heap? − n = 23 − 3 = 5
(a) 1, 3, 5, 6, 8, 9 (b) 9, 6, 3, 1, 8, 5 Ans. (b)
(c) 9, 3, 6, 8, 5, 1 (d) 9, 5, 6, 8, 3, 1 60. The height of a binary tree is the maximum number
(GATE 2006: 2 Marks) of edges in any root to leaf path. The maximum
number of nodes in a binary tree of height h is:
Solution: The 3-ary max heap can be formed by (GATE 2007: 1 Mark)
(a) 2h− 1 (b) 2h−1−1 (c) 2h+1− 1 (d) 2h+1.
9, 5, 6, 8, 3, 1.
9
Solution: Maximum number of node with height
h = 1 + 2 + 4 + ….. + 2h = 2h+1 − 1
56 8 Ans. (c)
3 1 61. The following postfix expression with single-digit
Ans. (d) operands is evaluated using a stack:
∧
823 /23*+51*−
57. Suppose the elements 7, 2, 10 and 4 are inserted, in
∧
that order, into the valid 3-ary max-heap found in Note that is the exponentiation operator. The
the above question Q.56. Which one of the follow- top two elements of the stack after the first * is
ing is the sequence of items in the array represent- evaluated are:
ing the resultant heap?
(a) 6, 1 (b) 5, 7
(a) 10, 7, 9, 8, 3, 1, 5, 2, 6, 4 (c) 3, 2 (d) 1, 5
(b) 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 (GATE 2007: 2 Marks)
va
lue = value + GetValue 69. Which combination of the integer variables x, y
(ptr->leftChild) + GetValue and z makes the variable a get the value 4 in the
(ptr->rightChild); following expression?
}
return(value); a = (x > y) ? ((x > z) ? x :z):((y > z) ? y :z)
} (a) x = 3, y = 4, z = 2
The value returned by GetValue when a pointer (b) x = 6, y = 5, z = 3
to the root of a binary tree is passed as its argu- (c) x = 6, y = 3, z = 5
ment is: (d) x = 5, y = 4, z = 5
(GATE 2008: 1 Mark)
(a) The number of nodes in the tree
Solution: In option A, 3 > 4 is false so it moves
(b) The number of internal nodes in the tree
to else part. In else condition, 4 > 2 is true and 4
(c) The number of leaf nodes in the tree
(d) The height of the tree
will be printed.
(GATE 2007: 2 Marks) Ans. (a)
Solution: As we can see in else part of given 70. The breadth first search algorithm has been
program, left and right subtrees are traversed implemented using the queue data structure. One
recursively and value is incremented if they have possible order of visiting the nodes of the following
leaf nodes. graph is
Ans. (c)
Insertion of 7th key 78. The running time of f1(n) and f2(n) are
To maximize splits, let us insert a value in a node
(a) Θ(n) and Θ(n) (b) Θ(2n) and Θ(n)
that has key in access. Let us insert 15
(c) Θ(n) and Θ(2n) (d) Θ(2n) and Θ(2n)
8*30 (GATE 2008: 2 Marks)
79. f1(8) and f2(8) return the values 82. Consider the program below:
(a) 1661 and 1640 (b) 59 and 59 # include <stdio.h>
(c) 1640 and 1640 (d) 1640 and 1661 int fun(int n, int * f_p) {
int t, f;
(GATE 2008: 2 Marks)
if (n <= 1) {
Solution: Implementing the code will give the * f_p =1;
output 1640, 1640. return 1;
}
Ans. (c) t = fun (n - 1, f_p);
Common Data for Questions 80 and 81: Consider f = t+ * f_p;
the following C program that attempts to locate an * f_p = t;
return f;
element x in an array Y[ ] using binary search. The
}
program is erroneous.
int main() {
1. f(int Y[10], int x) { int x = 15;
2. int u, j, k; printf (" % d\ n", fun(5, & x));
3. i = 0; j = 9; return 0;
4. do { }
5. k = (i + j) / 2; The value printed is
6. if(Y[k] < x) i = k;
else j = k; (a) 6 (b) 8 (c) 14 (d) 15
7. } while ((Y[k]! = x) && (GATE 2009: 1 Mark)
(i < j));
8. if(Y [k] == x) print f ("x is in Solution:
the array");
Iteration n *f_p t f
9. else print f ("x is not in the
array"); 5 15 ® 3 5 8 ® Ans
10. }
4 15 ® 2 3 5
80. On which of the following contents of Y and x does 3 15 ® 1 2 3
the program fail? 2 15 ® 1 1 2
(a) Y is [1 2 3 4 5 6 7 8 9 10] and x < 10. 1 15
(b) Y is [1 3 5 7 9 11 13 15 17 19] and x < 1.
(c) Y is [2 2 2 2 2 2 2 2 2 2] and x > 2. The function will return 8.
(d) Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 Ans. (b)
and x is even. 83. What is the maximum height of any AVL tree with
(GATE 2008: 2 Marks) 7 nodes? Assume that the height of a tree with a
Solution: Binary search fails for Y [2 2 2 2 2 2 2 single node is 0.
2 2 2] and x > 2 (a) 2 (b) 3 (c) 4 (d) 5
Ans. (b)
(GATE 2009: 2 Marks)
81. The correction needed in the program to make it
work properly is Solution: The height of an AVL tree storing n
items is O(log(n)).
(a) Change line 6 to: if (Y[k] < x) i = k + 1; else Ans. (b)
j = k − 1;
Linked Answer Questions 84 and 85: Consider a
(b) Change line 6 to: if (Y[k] < x) i = k − 1; else
binary max-heap implemented using an array.
j = k + 1;
(c) Change line 6 to: if (Y[k] <= x) i = k; else 84. Which one of the following array represents a
j = k; binary max-heap?
(d) Change line 7 to:} while ((Y[k] == x) && (i < j));
(a) {25,12,16,13,10,8,14}
(GATE 2008: 2 Marks) (b) {25,14,13, 16,10,8,12}
Solution: Change line 6 according to option A to (c) {25,14,16,13,10,8,12}
correct the program. (d) {25,14,12,13,10,8,16}
Ans. (c) (GATE 2009: 2 Marks)
/
/
{
8 10
if (n<= 0)return 0;
Ans. (d) el
se if(*a% 2= = 0)
86. In a binary tree with n nodes, every node has an return * a + f(a
odd number of descendants. Every node is consid- +1,n - 1);
ered to be its own descendant. What is the number el
se return * a -
of nodes in the tree that has exactly one child? f(a + 1, n - 1);
}
(a) 0 (b) 1 int main( )
(c) (n − 1)/2 (d) n − 1 {
(GATE 2010: 1 Mark) in
t a[ ] = {12, 7,
13, 4, 11, 6};
Solution: The given tree is called full binary printf("%d",
f(a,6));
tree which has 0 or 2 children. No node will have return 0;
exactly one child node. }
Ans. (a)
(a) −9 (b) 5 (c) 15 (d) 19
87. What does the following program print?
(GATE 2010: 2 Marks)
#include <stdio.h>
void f (int *p, int * g) { Solution:
p = q;
*p = 2; f(a,6) 12 + f(7,5)
}
int i = 0, j =1; f(7,5) 12 + 7 − f(13,4)
int main ( ){
f(13,4) 12 + 7 −(13 − f(4,3))
f(&i, & j);
print f ("%d%d / n", i, j ; f(4,3) 12 + 7 −(13 − ( 4 + f(11,2)))
return 0;
} f(11,2) 12 + 7 − (13 − (4 + 11 − f(6,1)))
(d) (c)
5
2 8
2
3
1 4 6 10
3 4
(GATE 2011: 1 Mark)
Ans. (b) 2
Solution:
Given expression is:
7
Precedence of ↑ is higher, so first the sub-expres-
sion (3↑ 4 ↑3) will be solved. In this 4 ↑ 3 will be
solved first because ↑ is right to left associative. So,
the expression will be solved as ((7 ↓(3↑(4 ↑3))).
3
↓ operator is left to right associative, so ((7 ↓(3↑(4
↑3))) will be solved first. Build the in order expres-
4 sion considering operator precedence.
((7 ↓(3↑(4 ↑3)))↓2
3 2 Ans. (b)
Common Data Questions 94 and 95: Consider the
following recursive C function that takes two
(b) arguments:
unsigned int foo (unsigned int n,
2 unsigned int r) {
if (n>0) return ((n% r) + foo (n/r, r) );
else return 0;
7 }
Solution: The above pseudocode is traversing tree 100. What output will be generated by the given code
to calculate the tree height. segment if:
Box 1: it should be replaced with the code that will
Line 1 is replaced by autoint a = 1;
traverse the right sub tree, so
Line 2 is replaced by registerint a = 2;
B1: (1 + height(n → right))
Box2: Now both left and right sub tree height is (a) (b)
calculated. 3 1 42
So the maximum out of both will be chosen.
4 1 61
B2: (1 + max(h1, h2)).
Ans. (a) 4 2 61
99. What output will be generated by the given code (a) 1/8 (b) 1 (c) 7 (d) 8
segment? (GATE 2013: 1 Mark)
(a) (b) Solution: P(e) = (1/2)
31 42 Number of ways vertices can be chosen of cycle
three = 8C3 × (1/2)3 = 7
41 61
Ans. (c)
42 61
102. Which of the following statements is/are TRUE
(c) (d) for undirected graphs?
42 31
P : Number of odd degree vertices is even.
62 52
Q: Sum of degrees of all vertices is even.
20 52 (GATE 2013: 1 Mark)
(GATE 2012: 2 Marks) (a) P only (b) Q only
(c) Both P and Q (d) Neither P nor Q
Solution: A is static variable, its value will be
preserved between different function calls. Solution: Both statements P and Q are correct.
First call to prtFun ( ): a = 2 and b = 1
a = 2 +2 = 4 Ans. (c)
Print 4 2 103. What is the return value of f(p, p), if the value of
Second call to prtFun ( ): a = 4 and b = 1 p is initialised to 5 before the call? Note that the
a=4+2=6 first parameter is passed by reference whereas the
Print 6 2 second parameter is passed by value.
Main printf function prints : 2 0 int f (int&x, int c) {
Ans. (c) c = c - 1;
PRACTICE EXERCISES
Set 1
(a) Will execute successfully (a) ascending order. (b) descending order.
(b) Incorrect syntax (c) random order. (d) none of these.
(c) May execute on some compiler
(d) Will result in execution error 22. Which of the following is not an application of
binary search trees?
12. Global variables in different files are checked
(a) Finding a particular alphabet in a given string
(a) at load time. (b) Sorting of characters in a string
(b) at assembly time. (c) Faster searching as compared to linear search
(c) at linking time. (d) Minimising the length of a string
(d) during execution time.
23. Which of the following is known as two-way list?
13. Indirect addressing uses:
(a) Grounded header list
(a) Arrays (b) Pointers (b) Circular header list
(c) Structures (d) Array with pointers (c) Linked list with header and trailer
14. typedef char *ch; (d) None of the above
const ch P; 24. Which of the following operators cannot be over-
In this code, what is P? loaded in C++?
(a) P is a constant. (a) + (b) ?
(b) P is a character constant. (c) = = (d) : :
(c) P is a character type.
(d) P is a pointer of character type. 25. The correct way to round off a floating number x
to an integer value is
15. Which of the following will have efficient space
complexity and lesser time complexities? (a) y = (int) (x + 0.5)
(b) y = int (x − 0.5)
(a) Incomplete binary tree
(c) y = int (x + 0.5)
(b) Complete binary tree
(d) y = (int) ((int)x + 0.5)
(c) B+ tree
(d) Left-inclined binary tree 26. Given two sorted list of size `m’ and `n’, respec-
16. Binary tree can be represented as tively. The number of comparison needed in the
worst case by the merge sort algorithm will be
(a) Linked list and graph. (b) Array and tree.
(c) Array and linked list. (d) Stack and queue. (a) m * log n (b) min (m, n)
(c) avg (m, n) (d) m + n − 1
17. The worst case height of AVL tree with n nodes is
27. Consider a forest of t-trees having n vertices. The
(a) 1.44 log(n) (b) 1.44 log(n + 1)
number of edges will be:
(c) 1.44 log(n + 2) (d) 1.44 nlogn
(a) nlog t (b) n − t
18. Which of the following sorting algorithm has almost (c) n * t (d) (n * t)/2
the same worst case and best case complexity?
28. Consider a binary search tree, traversal
(a) Quick sort (b) Merge sort
will result in nodes arranged in ascending order?
(c) Heap sort (d) Shell sort
(a) Inorder (b) Polish
19. Which of the following is max-heap property if A is
(c) Reverse polish (d) Depth first search
an array and i is the index of the array?
(a) A[Parent(i) ≥ A[i]]
29. The balance factor of height balance trees is:
(b) A[A[i]>Parent(i)]
(c) A[Parent(i) ≤ A[i]] (d) A[A[i]<Parent(i)] (a) −1, 0, 1 (b) 1, 0, 1
(c) −1, 2, −1 (d) 1, 2, 1
20. Complete binary tree can be implement by making
use of 30. The term `push’ and `pop’ is related to the
(a) array. (b) dequeue. (a) array. (b) linked list.
(c) priority queue. (d) stack. (c) queue. (d) stack.
21. If a binary search tree traversed in inorder then the 31. A digraph when represented in an adjacency
number of nodes will be printed in matrix, the matrix is a
2. FORTRAN implementation do not permit recur- (a) An array, each element of which is a pointer to
sion because a structure of type node.
(b) A structure of 2 fields, each field being a pointer
(a) They use static allocation for variables
to an array of 10 elements.
(b) They use dynamic allocation for variables
(c) A structure of 3 fields: an integer, a float and
(c) Stacks are not available on all machines
an array of 10 elements.
(d) It is not possible to implement recursion on all
(d) An array, each element of which is a structure
machines
of type node.
3. An unrestricted use of the `goto’ statement is 7. Aliasing in the context of programming language
harmful because refers to
(a) It makes it more difficult to verify programs. (a) multiple variables having the same memory
(b) It increases the running time of the programs. location.
(c) It increases the memory required for the (b) multiple variables having the same value.
programs. (c) multiple variables having the same identifier.
(d) It results in the compiler generating longer (d) multiple uses of the same variable.
machine code.
8. Consider the following three C functions:
4. What is the value of X printed by the following
[P1] int* g(void)
program? {
Program COMPUTE (input, output); int x = 10;
Var X: integer return (&x);
Procedure FIND (X: real) }
Begin X: sqrt(X); end [P2] int* px;
begin {
X:=2 int *px;
Find (X) *px= 10
Writeln(X) return px;
end }
[P3] int* g(void)
(a) 2 (b) 4
{
(c) Runtime error (d) None of the above
int *px;
px = (int*)malloc(size of (int));
5. Consider the following C function definition *px =10;
int Trial (int a, int b, int c) return px;
{ }
if ((a>=b)&& (c <b) return b;
el
se if (a >=b) Which of the above three functions is likely to
return Trial (a, c, b); cause problems with pointers?
else return Trial (b, a, c); (a) Only P3 (b) Only P1 and P3
} (c) Only P1 and P2 (d) P1, P2 and P3
The function trial: 9. The value of j at the end of the execution of the
(a) Finds the maximum of a, b and c. following C program
(b) Finds the minimum of a, b and c. int incr (int i)
(c) Finds the middle number of a, b and c. {
(d) None of these. staticint count = 0;
count = count + i
6. The following C declarations return (count);
}
struct node {
main (){
int i;
int i, j;
float j;
for (i=0; i<=4; i++)
};
j =incr(i)
struct node *s[10];
}
Define s to be is
21. In the balanced binary tree in the figure given of a complete n-ary tree, the number of leaves in it
below, how many nodes will become unbalanced is given by
when a node is inserted as a child of the node `g’?
(a) x(n − 1) + 1 (b) xn − 1
(c) xn +1 (d) x(n + 1)
a
27. Let LASTPOST, LASTIN and LASTPRE denote
the latest vertex visited in a postorder, inorder
b e and preorder traversal, respectively, of a complete
binary tree. Which of the following is always true?
c d (a) LASTIN = LASTPOST
f (b) LASTIN = LASTPRE
(c) LASTPRE = LASTPOST
g (d) None of these
31. Consider the following code segment. What is the final value of x in both call by value
void foo(int x, int y) and call by reference, respectively?
{ (a) 5 and 16 (b) 5 and 12
x+=y;
(c) 5 and 20 (d) 12 and 20
y+=x;
} 32. What is the maximum height of any AVL tree with
main() 7 nodes? Assume that the height of a tree with a
{ single node is 0.
int x=5.5;
foo(x,x);
}
Set 1
Set 2
1. (a) The given program is for swapping values of 6. (a) S is an array, each element of which is a pointer
two variables. to a structure of type node.
2. (a) In recursion, stack is used to store the latest 7. (a) Aliasing describes a situation in which a data
value of variables, means value of variables change location in memory can be accessed through differ-
with the recursive call. But FORTRAN uses ent symbolic names in the program.
static allocation for variables which does not allow 8. (c) In P1, there is no parameter passing and type
changes in every different function call. casting so it is wrong.
3. (a) A goto statement does not increase the running In P2, pointer is defined as pointer function which
time or memory requirement of the program. It is wrong.
also does not contribute to longer machine code. 9. (a) Count is declared as static variable in incr( ).
But the use of goto can result in unstructured So, statement static int count = 0 will assign count
code with multiple entry and exit points which can to 0 only in the first incr call. Remaining calls to
cause an issue for program verification. incr function will use old values of count.
4. (b) Global variable value not changed because of at first incr(0) call count will become 0
static scoping. after incr(1) call count will become 1
after incr(2) call count will become 3
5. (d) Just take a = 21, b = 8 and c = 14 and verify after incr(3) call count will become 6
the options.
after incr(4) call count will become 10
10. (c) Initial value of x and y will be 10, 3 and address fun (101)
are 1000, 2000, respectively. Then after first state- fun = 101−10 = 91
ment execution in the function, value at address
1000 will be {(10 as old value) + (4) = 14} because Return value of fun is 91. So, option (c) is correct.
y at the called function refers to address of x. After
16. (b) If a record is inserted in the circular-linked list
second statement execution in the function, value
at address 1000 will be {3 + 14 + 14 = 31} because
then two pointers have to be modified. Let a record
k be inserted between the node m and n, then
`x’ at called function refers to y in the calling func-
tion and `z’ at the called function refers to address (a) A
ddress field of node m contains address of
of x. So final values will be node x.
x(address = 1000) = 31 (b) Address field of node x contains address of
node n.
y(address = 2000) = 3
11. (a) To find location of an element in a two- 17. (b) With linked list binary search will take O(n)
dimensional array by row-major order is: time to sort the element list. Thus, binary search
performs worst with linked list.
A[i][j] = Base address + (i − 1)*r + (j − 1), where
r = no. of rows. 18. (d) On linked list we can perform only linear
Given base address = 100 and number of rows = 15 search. The number of comparisons required using
= 100 + (i − 1) × 15 + (j − 1) linear search is O(n).
= 100 + 15i − 15 + j − 1
= 100 + 15i + j − 16 19. (b) In a binary tree, two nodes are connected by a
= 84 + 5i + j single node to having degree 2. If a tree has n leaf
nodes, then the number of nodes with degree 2 will
11. (a) Let an array S[1 … 8] always be less than n. So option (b) is the strong
answer.
1 2 3 4 5 6 7 8
20. (b)
S = 7, k = 3 1
a
reverse (S, 1, 3) we get [3, 2, 1, 4, 5, 6, 7, 8]
reverse (S, 4, 8) we get [3, 2, 1, 8, 7, 6, 5, 4] 1
b e 1
reverse (S, 1, 8) we get [4, 5, 6, 7, 8, 1, 2, 3]
after performing above three reverse, we can see it d
1 c
is equivalent to the option (a). f
0
12. (b) Take a stack and perform push and pop opera- 0
0 g
tion to verify the given options.
21. (b) The binary search tree is as follows: In postorder traversal, root node is traversed in the
end. So we can remove option (a) and (c).
50 Let us take an example for option (b).
15 62
4
5 20 58 91
5 6
3 8
37 60
7 8 9
24
Inorder: 7 5 8 4 9 6
In the above binary tree, number of nodes in the
left sub-tree of root is 7 and in the right sub-tree Preorder: 4 5 7 8 6 9
of the root is 4. When a tree is a complete binary tree with full
at last level, then only last-visited node will
22. (b) Not possible always be same for inorder traversal and preorder
(b) 2 3 50 40 0 43 traversal.
23. (d) Inorder sequence: 53124768 Ans. (d)
3 Given n = 4i + 1
7
Total nodes = internal nodes (i) + leaf nodes (l)
n=i+l
i=n−l
1 4
6 8
n = 4(n − l) + 1
2 n = 4n − 4l + 1
4l = (2n + 1)
24. (b) A labelled rooted binary tree cannot be con- l = (2n + 1)/4
structed uniquely when inorder traversal is given
along with postorder or preorder traversal. 28. (c) According to program,
F(n) = n + 2F(n − 1) for n ≥ 2
25. (a) Total number of nodes in n-ary tree = ni + 1,
= 0 for n = 1
where i is internal nodes 1
F(4) = 4 + 2(3 + 2(2 + 2(0)))) = 18
Total nodes = internal nodes (i) + leaf nodes (l ) F(3) = 3 + 2(2 + 2(0)))) = 7
2 Ans. (c)
From 1 and 2
29. (c) 0, 1, 8, 9, 100, 101, 110 is an octal number
i + l = ni + 1
format.
l = (n − 1)I + 1
Replace I with x, because in question internal node 30. (b) B tree of order k, every internal node must
is given as x. have k/2 to k children and number of keys will be
So number of leaf nodes in n-ary tree with x inter- one less than the children.
nal nodes is (n − 1)x + 1.
31. (c) In call by value, there is no change in the value
26. (d) In the question, it is given that the tree is a com- of x because function foo( ) operates on copy of x.
plete binary tree. For a complete binary tree, the In call by reference, first value of x changes to 5 + 5
last visited node will always be the same for inorder and then it changes to 10 + 10 = 20.
traversal and preorder traversal because in both the
traversal right child is traversed in the last. 32. (3) Draw AVL tree to find the height.
9
8
Number of questions
7
6
Marks 1
5
Marks 2
4 Total number of questions
3
2
1
0
2015 2014 2013 2012 2011 2010
ALGORITHMS
Syllabus: Algorithms: Analysis, Asymptotic notation, Notions of space and time complexity, Worst- and average-case
analysis; Design: Greedy approach, Dynamic programming, Divide-and-conquer; Tree and graph traversals, Connected
components, Spanning trees, Shortest paths; Hashing, Sorting, Searching. Asymptotic analysis (best, worst, average
cases) of time and space, Upper and lower bounds, Basic concepts of complexity classes — P, NP, NP-hard, NP-complete.
Problem 4.1: Consider the following functions: Problem 4.2: Consider the following functions:
f (n) = n f (n) = n
n g(n) = n
g(n) =
8
For f(n) = O(g(n)), find the values of c and n0.
For f(n) = Q(g(n)), find the values of c1, c2 and n0.
Solution: From the definition of Big-O notation, we
Solution: From the definition of theta (Q) notation, have
f (n) ≤ c(g(n)) ∀n ≥ n0
we have
n0 c ⋅ n ≤ n2 ∀ n ≥ n0
Figure 4.2 | Big-O notation (O). The above condition is satisfied. So, c = 1 and n0 = 1.
O(nn)
O(an)
O(nk)
O(n2)
O(n)
Time
O(√n)
Olog(n)
O(log(log n)
O(1)
Complexities
Figure 4.4 | Time complexities in increasing order.
4.3.3.4 Quadratic Probing
0 Arrow
The probe sequence is
1
h(key), h(key) + 1, h(key) + 4, h(key) + 9, …,
2 Cat wrapping around as necessary
3 The offsets are perfect squares: h + 12, h + 22, h + 32, ….
Examples:
4 Eye
a. `Ape’ (h = 0): try 0, 0 + 1 − open!
5 b. `Ball’ (h = 1): try 1, 1 + 1, 1 + 4, 1 + 9 − open!
... ... ... Advantage: It reduces clustering.
22 When 0 Arrow
23 White 1 Ape
24 Yellow 2 Cat
25 Zero 3
4 Eye
4.3.3.3 Linear Probing 5 Ball
In case of linear probing the probe sequence is h(key), ... ... ...
h(key) + 1, h(key) + 2, …, uptill required. 22 When
Examples: The item `Ape’ (h = 0) will be placed at
23 White
position 1, because position 0 is already occupied. then
`Ball’ (h = 1): try to place it at position 1, 1+1, 1+2. 24 Yellow
Position 3 is open so it will be placed here.
25 Zero
Advantage: If open position is available, linear probing
will definitely find it.
Disadvantage: It may fail to find an existing open
Disadvantage: If there are clusters of filled positions,
position. For example, let table size = 10, and x denote
then length of subsequent probes increases. Probe length
the occupied blocks. Trying to insert a key with h(key) =
is the number of positions considered during a probe.
0 offsets the probe sequence shown in italic.
0 Arrow
0 x 5 x 25
1 Ape
2 Cat 1 x 1 81 6 x 16 36
3 Ball 2 7
4 Eye 3 8
5 x
4 x 4 64 9 9 49
... ... ...
Examples:
0 Arrow
1. h1 = our previous h
2. h2 = number of characters in the string 1 1
3. `Ape’ (h1 = 0, h2 = 3): try 0, 0 + 3 − open!
4. `Ball’ (h1 = 1, h2 = 4): try 1 − open!
2 Cat
3 Ball
Advantages:
4 Eye
1. It combines the good features of linear and qua-
dratic probing: 5
2. It reduces clustering
3. It will find an open position if there is one, pro- ... ... ...
vided the table size is a prime number 22 When
23 White
0 Arrow 24 Yellow
1 Ball 25 Zero
2 Cat
3 Ape
4.4 BINARY HEAP
4 Eye
5 A binary heap is a binary tree with two additional constraints:
... ... ... 1. Binary heap should satisfy the property of com-
22 When plete binary tree.
2. It should satisfy the heap ordering property, that
23 White is, all the nodes should be either greater than or
equal to or less than or equal to its children.
24 Yellow
According to the heap ordering property, binary heaps
25 Zero
are of two types:
Example 4.6
1. Insert the given value (5) in the given min-heap tree. 4. To satisfy the property of min-heap, swap (5, 7).
2. Element 5 is inserted at position of left child of node 5. Now, element 5 is in its right position. This is the
14. To satisfy the property of min-heap, swap (5, 14). final tree.
3. To satisfy the property of min-heap, swap (5, 10).
7 7
7
10 17 10 17
10 17
14 23 21 42 5 23 21 42
14 23 21 42
5 14
1 2 3
7 5
5 17 7 17
10 23 21 42 10 23 21 42
14 14
4 5
4. This represents the final tree, which is in the min- a set of numbers in an ascending or descending
heap property. order.
3. In internal sorting, the data which is to be sorted fits
7 42 into the main memory. Internal sorting has an advan-
tage of the random access nature of the main memory.
10 17 10 17 4. In external sorting, large amount of data is
sorted. This large amount of data cannot be fit
14 23 21 42 14 23 21 in main memory, so the data is placed in auxil-
iary memory. When sorting has to perform, small
1 2
chunks of data are brought into main memory,
10 10 sorted and placed back to a temporary file. At last
all the sub-files are merged in one large file.
42 17 14 17 5. In stable sorting, the order of equal elements is
preserved in a sorted list. Some of the sorting algo-
rithms that show characteristics of stable sort are
14 23 21 42 23 21
insertion sort, bubble sort, merge sort, etc.; whereas
3 4 heap sort, quick sort, etc., are not stable sorts.
6. In-place sorting: In this, the input is transformed
using a data structure with a small, constant amount
4.4.3 Time Complexity of extra storage space. The input is usually overwrit-
ten by the output as the algorithm executes. Quicksort
1. The worst-case running time for insertion is is commonly described as an in-place algorithm.
O(log n) because at most (log n) swaps are required 7. Measurement of time complexity: The time
to stable the min-heap property of the tree. complexity of a sorting algorithm is measured
2. The worst-case running time for deletion is O(log n) by the number of critical operations performed.
because at most (log n) swaps are required to stable Examples of critical operations are as follows:
• Key comparisons
the min-heap property of the tree.
Some important points of binary heap are as follows: • Movements of records
1. Because of its structure, a heap with height k will • Interchanges of two records
have between 2k and 2k+1 - 1 elements. Therefore, 8. We use asymptotic analysis to denote the time
a heap with n elements will have height = log2n. efficiency of an algorithm. Efficiency of an algorithm
2. In heap data structure, element with highest or lowest also depends on the type of data for input, so we
priority is stored as root element. Heap is not consid- define best-, worst- and average-case efficiencies.
ered as sorted structure, rather it is partially ordered.
There is no relation among nodes at a given level. 4.5.1 Linear Search
Heap is very useful data structure for implement
ing priority queues. Element with highest priority Linear search is a sequential way of finding an element in
can be easily removed. a given list. Linear search is a special case of brute force
3. Since a binary heap is a complete binary tree, it search. Its worst case is proportional to the number of
can be easily represented as an array, and an array- elements in list.
based representation is space efficient. If the parent
node is stored at index I, the left child can be cal-
4.5.1.1 Pseudocode for Linear Search
culated by 2 ×I + 1 and right child by 2 ×I + 2.
LINEAR_SEARCH (A[ ], x)
4.5 SEARCHING AND SORTING 1. i = 1
2. while (A[i] = = x and i < n)
3. i = i + 1
4. if (A[i] = = x)
Searching and sorting are the most common activities
5. return i
performed by the computer. These operations are the 6. else return 0
most important part for managing the data.
1. Searching is the algorithmic process of finding an 4.5.1.2 Time Complexity
item in a collection of items. The search result is
either true or false depending on whether or not From the analysis of above pseudocode, it can be
the item is present or not in the list. observed that with the growth of input list, average and
2. Sorting is ordering a list of objects according to a worst case complexities also grow linearly. In general, if
given condition or property. For example, ordering there are n elements in the list, the worst-case requires n
The insertion sort inserts each element into its proper The worst-case running time is O(n2)
position. It chooses one element, inserts it to its posi- The best-case running time is O(n)
tion and shifts the rest of the list by one location. This
process is repeated until no input element remains. Step- 4.5.6 Heap Sort
by-step procedure is given as follows.
1. Take an element from unsorted list. Heap sort is one of the comparison based sort that uses
2. Compare that element with sorted list from right heap data structure. It is a special case of selection sort.
to left. Its typical implementation is not stable, but can be made
3. Shift the list. stable. Heap can be built by the following two types:
Here, it is assumed that first element is already sorted. 1. Min-Heap: The parent node will be less than or
equal to its children nodes. So, the minimum ele-
ment will be at root.
4.5.5.1 Pseudocode for Insertion Sort 2. Max-Heap: The parent node will be greater than
or equal to its children nodes. The maximum ele-
INSERTION_SORT(A)
1. for j = 2 to n ment will be at the root.
2. key A [j]
3. // Insert A[j] into the sorted sequence A[1..j-1] 4.5.6.1 Heap Sort Algorithm for Sorting in
4. j i– 1 Increasing Order
5. while i > 0 and A[i] > key
6. A[i+1] A[i] 1. First build a max-heap from the input List.
7. i i– 1 2. In max heap, the largest element is at the root. So,
8. A[j+1] key swap the root with last element. Re-heapify the
tree. Repeat the above steps until the size of the 4.5.6.3 Pseudocode for BuildHeap Function
heap is greater than 1.
•• Heapify function: Heapify is an important BUILD_HEAP(A)
subroutine for manipulating max-heaps. Its 1. heapsize <- length(A)
2. for i <- floor( length/2 ) down to 1
inputs are an array A and an index i into the
3. Heapify(A, i)
array. When Heapify is called, it is assumed
that the binary tree rooted at left(i) and right(i) 4.5.6.4 Pseudocode for Heap Sort
is max_heap, but that A[i] may be smaller
than its children, thus violating the max-heap HEAP_SORT(A)
property. 1. BuildHeap(A)
•• Build Heap function: Heapify procedure 2. for i <- length(A) down to 2
is used in a bottom-up to convert an array A 3. exchange A[1] <-> A[i]
4. heapsize <- heapsize -1
into a max-heap. The elements in the subarray
5. Heapify(A, 1)
A[(n/2 + 1)⋅⋅⋅n] are all leaves of the tree, and
so each is a one-element heap to begin with. The
procedure BuildHeap goes through the remain- 4.5.6.5 Recurrence Relation of Heapify
ing nodes of the tree and runs Heapify on each Function
node. The recurrence relation of Heapify() is
2n
4.5.6.2 Pseudocode for Heapify Function T (n) ≤ T + Q(1)
3
HEAPIFY(A, i)
1. le <- left(i) 4.5.6.6 Time Complexity
2. ri <- right(i)
3. if (le<=heapsize) and (A[le]>A[i]) The time complexity of Heapify() is O(log n). The time
4. largest <- le
5. else
complexity of BuildHeap() is O(n log n), and the overall
6. largest <- i time complexity of the heap sort is O(n log n).
7. if (ri<=heapsize) and (A[ri]>A[largest])
8. largest <- ri 4.5.6.7 Applications of Heap Sort
9. if (largest != i)
10. exchange A[i] <-> A[largest] 1. It sorts a nearly sorted (or k-sorted) array.
11. HEAPIFY(A, largest) 2. It sorts k largest (or smallest) elements in an array.
Example 4.11
Let {6, 5, 3, 1, 8, 7, 2, 4} be the list that we want to sort recursively checked if another swap is needed, to keep
from the smallest to the largest. (Note: For `building larger numbers above smaller numbers on the heap
the heap’ step, larger nodes do not stay below smaller binary tree.)
node parents. They are swapped with parents, and then 1. Build the heap:
Example 4.12
Sort the given list of elements: (54, 26, 93, 17, 77, 31, 44, Step 2: After dividing the array, merge sort calls the
55 and 20). MERGE function to put the subarray into a sorted order.
Step 1: A recursive merge sort is called on the array Array A:
of elements. Merge sort is a recursive function which
divides an array into two parts; and calls itself recur- 54 26 93 17 77 31 44 55 20
sively to divide the array into sub-parts until each part
is having one element.
p r
Merge sort (A, 0, 8) 54 26 93 17 77 31 44 55 20
0 1 2 3 4 5 6 7 8
54 26 93 17 77 31 44 55 20
54 26 93 17 77 31 44 55 20
54 26 93 17 77 31 44 55 20
54 26
Merge (A, p, q, r)
54 26 93 17 77 31 44 55 20
26 54 93 17 77 31 44 20 55
26 54 93 17 77 31 44 20 55
17 26 54 77 93 20 31 44 55
Completed 17 20 26 31 44 54 55 77 93
3. Output each object from the input sequence fol- • It does not have any parallel edge.
lowed by decreasing its count by 1.
(a) Process the input data: 1, 4, 1, 2, 7, 5, 2. Posi-
tion of 1 is 2.
(b) Put data 1 at index 2 in the output. Decrease
count by 1 to place the next data 1 at an index 1
smaller than this index.
(c) Put data 2 at index 4 in the output. Decrease Figure 4.5 | Simple graph.
count by 1 to place the next data 2 at an index 3
smaller than this index. 2. Multigraph: A graph with a self-loop and parallel
(d) Now, move to the next element 3 which has count 4, edges is called a multigraph (Fig. 4.6).
and index 4 is already filled by 2 which means ele-
ment 3 does not exist in the given input list.
(e) Follow this process for all the elements.
4.6.2.1 Null Graph
A graph G = (V, E) is a set of vertices and edges where
each edge consist of pair of vertices.
If in the given graph there is no edge, then it is called a
1. Finite set of vertices are known as nodes of graph. null graph. In other words, a null graph does not have
2. (u, v) is a pair of vertices called edge. any edge between any two vertices (Fig. 4.7).
3. For directed graph (u, v) is an ordered pair that
represents an edge from node u to node v. A
D
4.6.1 Types of Graph
B
There are two types of graphs:
1. Simple graph: A graph is called a simple graph E
C
(Fig. 4.5) if
• It contains no self-loop. Figure 4.7 | Null graph.
Vertex 1 2 3 4 5
1 2
1 0 1 1 1 0
5 2 1 0 1 1 1
3 1 1 0 1 0
1-Regular 2-Regular 3-Regular 4 1 1 1 0 1
Figure 4.8 | Regular graph.
3 4
5 0 1 0 1 0
The important terms are given as follows: knapsack exactly; otherwise, we could add a fraction of
1. Solution space: The set of all possible solutions one of the remaining objects and increase the value of
for given n inputs is called solution space. the load. Thus, in an optimal solution, nSi = 1xiwi = W.
2. Feasible solution: The feasible solution is one of
the solutions from the solution space which satis- 4.7.2.1 Pseudocode for Fractional
fies the given condition.
Knapsack
3. Optimal solution: The optimal solution is one
of the feasible solutions which optimizes our goal. Greedy-fractional-knapsack (w, v, W)
1. for i =1 to n
4.7.1 Applications of Greedy Approach
2. Do x[i] =0
The following are the applications of the greedy approach: 3. weight = 0
4. while weight < W
1. Knapsack problem (fractional)
5. Do i = best remaining item
2. Job sequence with deadline
6. if ( weight + w[i] ≤ W)
3. Huffman coding
4. Minimum cost spanning tree: 7. Then x[i] = 1
a. Kruskal’s algorithm 8. weight = weight + w[i]
b. Prim’s algorithm 9. else
5. Singlesource shortest path:
10. x[i] = (w - weight) / w[i]
a. Dijkstra’s algorithm
b. Bellman—Ford algorithm 11. weight = W
12. Return x
4.7.2 Fractional Knapsack
4.7.2.2 Time Complexity
In this problem, items are given along with its weight
and profit. The target is to maximize the profit consider- If the ratio of vi/wi is already sorted in decreasing order,
ing the weight constraint. Unlike 0-1 knapsack problem, then the time taken by the while loop will be O(n). So,
fraction of item can be considered. Fractional knapsack the total time required will be O(n log n). If heap data
problem can be solved by Greedy approach. structure is used, which keeps highest value/weight ratio
Greedy Algorithm: The solution can be obtained by at root. The construction of heap will take O(n) time
making different choices. At each stage, the best possible and while loop will take O(log n) time (heap property is
choice is made. In fractional knapsack, first of all profit restored for each root removal). This does not improve
value/weight ratios are calculated and then sorted in the worst case, but it is faster if small number of items
descending order. Item with highest value/weight ratio are to be filled in knapsack.
is chosen and added in the collection. The maximum
weight is checked after adding each item. If the entire Example 4.16
item cannot be included, then fraction of it is added to
Consider the following details:
the collection.
1. Knapsack size = 20
2. Number of objects = 3
Example 4.15
There are n items in a store. For i = 1, 2, …, n, item i Object Obj1 Obj2 Obj3
has weight wi > 0 and worth vi > 0. A thief can carry a Profit 25 24 15
maximum weight of W pounds in a knapsack. In this ver- Weight 18 15 10
sion of a problem, the items can be broken into smaller
pieces, so the thief may decide to carry only a fraction xi Solution:
of object i, where 0 ≤ xi ≤ 1. Item i contributes xiwi to
the total weight in the knapsack, and xivi to the value Step 1: Calculate ratio (profit/weight):
of the load. Object Obj1 Obj2 Obj3
In symbol, the fraction knapsack problem can be Profit 25 24 15
stated as follows.
Weight 18 15 10
Maximize nSi = 1xivi subject to constraint nSi = 1xiwi
≤ W. It is clear that an optimal solution must fill the
Ratio 1.38 1.6 1.5
Step 2: Put according to decreasing ratio (profit/ 4.7.3.2 Pseudocode for Huffman Coding
weight):
HUFFMAN(f[1...n])
Object Obj2 Obj3 Obj1 1. T = empty binary tree
2. Q = priority queue of pairs (i, f[i]),
Profit 24 15 25
i = 1...n, with f as comparison key
Weight 15 10 18 3. for each k = 1...n − 1
Ratio 1.6 1.5 1.38 4. i = extractMin(Q)
5. j = extractMin(Q)
Step 3: Put items into the knapsack till the knapsack 6. f[n + k] = f[i] + f[j]
is full: 7. insertNode(T, n + k) with children i, j
8. insertRear(Q,(n + k, f[n + k]))
(a) Put object 2 into the knapsack; it will add (value = 9. return T
24 and weight = 15) to the knapsack. Knapsack has
5 units capacity remaining.
(b) Now object 3 cannot be put completely into the 4.7.3.3 Time Complexity
knapsack, so put a fraction of it. Add (value = 7.5
and weight = 5) to the knapsack. Knapsack is com- From the above pseudocode, we can see that with each
pletely full, which means no other object can be put iteration,the problem size is reduced by 1. Hence, there
into it. are exactly n iterations. The ith iteration consists of
locating the two minimum values in a list of length n −
Step 4: Total knapsack value = 31.5 (which is optimum).
i + 1. This is a linear operation, and so Huffman’s
algorithm clearly has a time complexityof O(n2).
4.7.3 Huffman Coding However, it would be faster to sort the weights ini-
tially, and then maintain two lists. The first list consists of
Huffman coding is a common technique of encoding, weights that have not been combined yet, and the second
includinglossless data compression. The algorithm’s list consists of trees that have been formed by combining
output can be viewed as a variable-length code table weights. This initial ordering is obtained at the cost of
for encoding a source symbol (such as a character in a O(n log n). Obtaining the minimum two trees at each step
file). Huffman coding is based on the frequency of occur- then consists of two comparisons (comparing the heads of
rence of a data item (pixel in images). The main aim the two lists, and then comparing the larger item with the
of Huffman coding is to encode the data that occurs item after the smaller). The ordering of the second list can
frequently using less number of bits. Code books store be maintained cheaply by using a binary search to insert
codes that are constructed for images. Both the code new elements. Since at step i there are i − 1 elements in
book and encoded data are transmitted to decode the the second list, O(log i) comparisons are needed for inser-
information. tion. Over the entire duration of the algorithm, the cost of
Huffman codes can be constructed using following two keeping this list sorted is O(n log n). Therefore, the overall
ideas: time complexity of Huffman’s algorithm is O(n log n).
1. For an optimum code, symbols that have high
probability should be assigned shorter code words. Example 4.17
2. To make optimum prefix code, two symbols that
In the following example five different symbols are given
occur least frequently should be assigned same
along with their frequency.
length.
Symbol Frequency
4.7.3.1 Principle of Huffman Codes
A 24
1. Starting with the two least probable symbols, g B 12
and d, of an alphabet A, if the code word for g is C 10
[m]0, the code word for d would be [m]1, where [m] D 8
is a string of 1s and 0s.
2. Now, the two symbols can be combined into a E 8
group, which represents a new symbol y in the
There are a total of 186 bits (with 3 bits per code word).
alphabet set.
3. The symbol y has the probability P(g ) + P(d ). Here, the least frequency symbols are E and D. First
Recursively, determine the bit pattern [m] using of all, we will connect them and frequency of node will
the new alphabet set. become 16. Then B and C are combined to make the
frequency 22. These will be two new nodes. In the next 3. At each step, an edge is added to the graph that
step, these two nodes are brought together and act as has minimum weight and is the neighbour to the
subordinate to the root node. A also acts as subordinate previous vertices.
to root node.
Steps of Prim’s Algorithm
1. Find the minimum weight edge from graph and
4.7.3.4 Code Tree According to Huffman
mark it.
2. Consider the neighboring edges of the selected
edges and mark the minimum weight edge among
those edges.
0 62 1 3. Continue this until we cover n-1 edges.
4. The resultant tree is minimum spanning tree and it
A
should always be connected.
24 0 38 1 Input: Undirected connected weighted graph G = (V, E )
in adjacency list representation, source vertex s in V and
w is an array representing weights of edges.
Output: p[1⋅⋅⋅|V |], representing the set of edges com-
0 22 1 16 1
0
posing an MST of G.
B C E
Note: The field p(v) names the parent of v in the tree.
D
12 10 8 8
Key(v) is the minimum weight of any edge connect-
ing v to a vertex in tree. Key(u)←∞ means there is no
Symbol Frequency Code Code Total such edge.
Length Length
PRIM (G, w, s)
A 24 0 1 24 1. for each u in V(G)
B 12 100 3 36 2. do key(u) ∞
3. π(v) NIL
C 10 101 3 30
4. key [r] 0
D 8 110 3 24 5. Q V [G]
E 8 111 3 24 6. while Q != empty
7. do u Extract-Min(Q)
8. for each v in Adjacent[u]
9. do if v ∈ Q and w(u, v) < key [u]
The total length is 138 bits.
Prim’s algorithm is a method to find minimum spanning Problem 4.5: Use Prim’s algorithm to find an MST in
tree. In this algorithm minimum spanning tree formed the following weighted graph. Use alphabetical order
should always be connected. Prim’s algorithm is an to break ties.
example of Greedy approach. b 5 d
Properties of Prim’s Algorithm 2 2
Step 3: Now select the next minimum weight edge and 2. Calculate the temporary distance of neighbours of
draw the corresponding edge in disconnected graph. active nodes. Distance is calculated by adding up
the weights associated with those edges.
b c d 3. Update the distance, and set the current node an
2 antecessor if such a calculated distance of a node is
smaller as the current one. This step is also called
i update and is Dijkstra’s central idea.
a e
4. Set the node as active node which has minimal tem-
porary distance. Mark the distance as permanent.
5. Repeat steps 2 to 4 until there are no nodes left
h g f with a permanent distance, whose neighbours still
1 2 have temporary distances.
Here, there are two edges containing weight 2, add all
possible edges of same weight until they form loop in the Dijkstra’s Pseudocode
tree (since tree doesnot contain any loop but a graph
DIJKSTRA (G , S):
may contain). That is why edge(g,f) and edge(i,c) both 1. for each vertex v in Graph:
are added. 2. distance[v] := infinity
Step 4: Continue this process and check for the next 3. previous[v] := undefined
smallest edge and add it into tree. Do this till we get 4. distance[ S ] := 0
(n − 1) edges in the tree. 5. Q := the set of all nodes in Graph
6. While Q is not empty:
The final minimum spanning tree would be: 7. u := node in Q with smallest distance[ ]
8. remove u from Q
b c d 9. for each neighbour v of u:
10. alt := distance[u] + distance_between(u, v)
11. If alt < distance[v]
i e 12. distance[v] := alt
a
13. previous[v] := u
14. Return previous[ ]
h g f
Time Complexity
Like Prim’s algorithm, Dijkstra’s algorithm runs in O(E
4.7.5 Single-Source Shortest Path log V) time.
1. Dijkstra’s algorithm with list: O(V2)
Given a weighted graph G, find the shortest path from 2. Dijkstra’s algorithm with modified binary heap:
a given vertex to every other vertex in G. Note that we O((E + V) log V )
can solve this problem quite easily with BFS traversal in 3. Dijkstra’s algorithm with Fibonacci heap: O(E +
the special case when all weights are equal. The Greedy V log V )
approach to this problem is repeatedly selecting the best
choice from those available at that time. Example
The following are the steps:
1. Start from the source node; find the distance of all
4.7.5.1 Dijkstra’s Algorithm
nodes reachable from the source.
Dijkstra’s algorithm is a single source shortest path algo- 2. Then pick the node which has the minimum dis-
rithm. It calculates the shortest distance starting from tance, from the pool of nodes. Source
the start node till another node in the graph. While cal- 3. Calculate again the distance from the selected A B C
culating the shortest distance, it excludes the longer dis- node, and follow the above two steps till the entire ∞ ∞ ∞ ∞
tance paths. Dijkstra’s algorithm does not operate on nodes are selected. Nil Nil N
negative weights. For calculating shortest distance with O 10 3 ∞
2 A Nil A A N
negative edges, Bellman-Ford algorithm is used. Steps to B D
10 O 4
perform Dijkstra’s algorithm are as follows:
1 4 8 7 9 AC Nil C
1. Assign all the nodes with distance infinity and 0 to A
3 O
initial node.Distance of start node s permanent and Nil
3 ACB
of all the other nodes is temporary.Set start node
C E O
as active node.
ACBD Nil
Source to know that the node has been explored before or not.
A B C D E The re-traversal becomes more for dense graph, which
∞ ∞ ∞ ∞ ∞ Cost increase computation time. Tree traversal is a special
Nil Nil Nil Nil Parent case of graph traversal. The following are two graph
O 10 3 ∞ ∞ traversal methods:
2 A Nil A A Nil Nil
B D 1. Breadth-first traversal (BFT)
Source O 4 11 6 2. Depth-first traversal (DFT)
A 4 B8 C 7 D 9 E AC Nil C C C
2
∞ ∞ ∞ ∞ ∞ Cost O 6 6 B D
4.8.1 Breadth-First Traversal
Nil Nil Nil Nil ParentNil
ACB B C A 1
CO 10 3 ∞E ∞
3
O 6 3 C The breath-first
E traversal starts from the root node and
Nil A A Nil Nil ACBD Nil 3 traverses all its neighbours. Then for each neigh-
C then
O 4 11 6 bour, its corresponding unvisited neighbour is processed.
Nil C C C
2 The pseudocode for BFT is given as follows:
O 6 6 B D
Nil B C A 1 BST (V)
1. visited(V) =1
O 6 3 C
3
E 2. add(V, Q)
Nil C 3. while(Q is not empty)
4. u = delete(Q)
4.7.5.2 Bellman—Ford Algorithm 5. for all x adjacent to u
6. if( x is not visited)
The Bellman—Ford algorithm is used to compute 7. visited(x) = 1
single source shortest path to all other vertices of 8. add(Q, x)
weighted directed graph. This algorithm also consid-
ers negative weights of graph edges. This algorithm 4.8.1.1 Time Complexity
is slower but is capable of even finding a negative
weight cycle in graph. The time complexity of BFT is O(V + E), where V is
the number of vertices in the graph and E is the number
Bellman-Ford Pseudocode of edges in the graph.
J
Graph traversal means visiting all the nodes in a graph in
some order. In graph traversal, some nodes may be vis- Solution: A is the starting node and Q is a queue data
ited more than once. This is because it is not necessary structure to store the values.
C D E
4.8.2 Depth-First Traversal
4. u = delete (Q) → C
As already discussed, there are many ways of tree tra-
For all adjacent nodes to C which are not already
versal. Consider the tree given in Fig. 4.10:
added in the queue, Add (Q, X), where X = (F, G)
is the adjacent node to C.
Tree
D E F G
5. u = delete (Q) → D I Root
For all adjacent nodes to D which are not already
added in the queue, Add (Q, X), where X = (H) is
the adjacent node to D. F K
E F G H
6. u = delete (Q) → E A H Z
For all adjacent nodes to E which are not already
added in the queue, Add (Q, X), where X = (F) is
the adjacent node to E. D
F G H Figure 4.10 | DFT of a tree.
7. u = delete (Q) → F
For all adjacent nodes to F which are not already Preorder traversal for given tree: I F A D H K Z
added in the queue, Add (Q, X), where X = (I) is Preorder traversal also results in depth first traversal.
the adjacent node to F. This is because the traversal goes deeper before travers-
G H I ing its siblings. In this tree, the descendants of F, that
is, A, H, D are traversed first and after that sibling of F,
8. u = delete (Q) → G that is, K are traversed.
For all adjacent nodes to G which are not already
added in the queue, Add (Q, X), where X = (I) is
the adjacent node to G. 4.8.2.1 Pseudocode for DST
H I DFT (V)
1. visited (V) =1
9. u = delete (Q) → H 2. for all x adjacent to V
For all adjacent nodes to H which are not already 3. if (x is not visited)
added in the queue, Add (Q, X), where X = (J) is 4. visited (x) =1
the adjacent node to H. 5. DFT (x)
I J
The above is the recursive code for DFT.
4.8.2.2 Time Complexity
4. POP (S) → D
The time complexity of DFT is O(V + E ), where V is For all adjacent nodes to D which are not already
the number of vertices in the graph and E is the number added in the stack, PUSH (S, X). Every neighbour
of edges in the graph. element will be stored in the stack from right to left.
Here, X = (H) is the adjacent node to D.
4.8.2.3 Application of DFT H E C
The following are the applications of BFT. It can be 5. POP (S) → H
used to find out For all adjacent nodes to H which are not already
added in the stack, PUSH (S, X). Every neighbour
1. whether the given graph is connected. element will be stored in the stack from right to left.
2. the number of connected components in a graph. Here, X = (F, G) is the adjacent node to H.
3. whether the given graph contains a cycle.
4. whether the given directed graph is strongly F G E C
6. POP (S) → F
connected.
For all adjacent nodes to F which are not already
Problem 4.7: Consider the graph G = (V, E) and find added in the stack, PUSH (S, X). Every neighbour
the DFT starting node A. element will be stored in the stack from right to left.
Here, X = (F) is the adjacent node to F.
Graph(G) A
G E C
7. POP (S) → G
B C For all adjacent nodes to G which are not already
added in the stack, PUSH (S, X). Every neighbour
D E F G element will be stored in the stack from right to left.
Here, X = (F) is the adjacent node to G.
H E C
8. POP (S) → E
Solution: A is the starting node and S is a stack data For all adjacent nodes to E which are not already
structure to store the values. Stack is internally used added in the stack, PUSH (S, X). Every neighbour
by the recursive call. element will be stored in the stack from right to left.
Here, X = (F) is the adjacent node to E.
DFT (A)
1. Visited (A) =1 C
PUSH(S, A) 9. POP (S) C
A For all adjacent nodes to C which are not already
added in the stack, PUSH (S, X). Every neighbour
2. POP(S) → A element will be stored in the stack from right to left.
For all adjacent nodes to A which are not already Here, X = (F) is the adjacent node to C.
added in the stack, PUSH (S, X). Every neighbour
element will be stored in the stack from right to left.
Here, X = (B, C) is the adjacent node to A. 10. The stack is empty, which means the graph is
traversed completely.
B C
3. POP(S) → B
For all adjacent nodes to B which are not already 4.9 DYNAMIC PROGRAMMING
added in the stack, PUSH (S, X). Every neighbour
element will be stored in the stack from right to left.
Here, X = (D, E) is the adjacent node to B. Dynamic programming is a method for solving complex
problems by breaking them down into simpler sub-prob-
D E C lems. It is applicable to problems exhibiting the proper-
ties of overlapping sub-problemsand optimal substructure.
Dynamic programming algorithms are used for optimiza- The total running time = O(2n).
tion (e.g. finding the shortest path between two points, or Note: Most of the recursive programs contain very less
the fastest way to multiply many matrices). A dynamic number of distinct calls and more number of repeated sub-
programming algorithm will examine all possible ways to problems or function calls. These problems are known as
solve the problem and will pick the best solution. overlapping sub-problems.
Time Complexity
Fib(5) Fib(4)
1. Fibonacci series without dynamic programing
[space = O(n) and time = O(2n)]
Fib(4) Fib(3) Fib(3) Fib(2) 2. Fibonacci series with dynamic programing [space =
Level 6 O(n) and time = O(n)]
Fib(3) Fib(2) Fib(2) Fib(1) Fib(2) Fib(1)Fib(1) Fib(0) 4.9.3 0/1 Knapsack
The time complexity of the knapsack problem is as follows: Given a directed graph G = (V, E ), where each edge (v, w)
1. Without dynamic programing = O(2n) has a non-negative cost C[v, w], for all pairs of vertices (v,
2. With dynamic programing = O(mn) w), find the cost of the lowest-cost path from v to w. It is
a generalization of the single-sourceshortest-path problem.
It is one of the NPC problems.
4.10.1 Floyd’s Algorithm
4.9.4 Longest Common Subsequence
Let Ak(i, j) be the minimum cost required to go from
A subsequence of a given sequence is just the given sub- vertex i to vertex j for which all intermediate vertices are
sequence in which zero or more symbols are left out. in the set (0, 1, 2, 3, …, k) (Fig. 4.12).
Let sequence (S) = {A, B, B, A, B, B}, subsequence
1
(S1) = {A, A}, subsequence (S2) = {A, B, A, B} and
subsequence (S3) = {B, A, B, A}. 2
i j
4.9.4.1 Common Subsequence
LCS(i, j)
4.11.1 P-Complexity Class
0, if i = 0 or j = 0
= 1 + LCS(i - 1, j - 1), if X[i] = Y [ j ] P in computational complexity theory is known as poly-
nomial time or deterministic time. This is the most basic
Max[{LCS(i - 1, j)}{LCS(i, j - 1)}], if X[i] ≠ Y [ j ]
complexity class. This consists of all the problems that
are solvable in polynomial time. These problems can be 5. Subgraph isomorphism problem
solved in O(nk) time, where k is some constant. In class 6. Subset sum problem
P, problems are efficiently solvable or traceable. 7. Clique problem
A language L is in class P iff there exists some Turing 8. Vertex cover problem
machine M such that: 9. Independent set problem
10. Dominating set problem
1. Turing machine runs in polynomial time for all the 11. Graph colouring problem
inputs.
2. For all y in L, M must return 1, otherwise 0. 4.11.4 NP-Hard
P includes many natural problems like linear programming,
finding maximum matching and calculating the greatest NP-hard is a class that includes problems which are “at
common divisor. Prime number is also a part of class P now. least as hard as hardest problem in NP”. A problem is
said to be NP hard if
IMPORTANT FORMULAS
8. Binary search: Q(log n). 22. Dijkstra’s algorithm: O(E log V).
SOLVED EXAMPLES
Thus, hash table takes less search time. g(n) = n log b⋅a = n log 2⋅4 = n2
Ans. (c) Here, order of f(n) = order of g(n), so we get
15. Which of the following statements is false about T (n) = O(n log b⋅a log2 n) = O(n2 log2 n)
Prim’s algorithm? Ans. (b)
(a) The time complexity is O(E log V) using a 19. Solve the following given recurrence relation:
binary heap. T (n) = 16 T (n/4) + n3.
(b) It may use binomial max-heap to represent the
priority queue. (a) Q(n log2 n) (b) Q(n2 log2 n)
(c) The time complexity is O(E + V log V) using (c) Q(n3) (d) None of the above
a Fibonacci heap. Solution: From the master theorem, we have
(d) Initially the roots key and nodes are set to zero. a = 16, b = 4 and f(n) = n 2. So
Solution: g(n) = n log b⋅a = n log 4⋅16 = n2
(a) True, The time complexity is O(E log V) using
a binary heap. Order of f(n) > order of g(n), so we get
(c) True, Prim’s algorithm’s time complexity is T (n) = O(f (n))
O(E + V log V) using a Fibonacci heap. Ans. (c)
(d) True, Initial distance to the roots key and
nodes is kept Zero. 20. Consider the following two functions:
n3 , for 0 ≤ n ≤ 10, 000
g1(n) = 3
Only option (b) is false.
Ans. (b) n , for 0 ≤ n ≥ 10, 000
n, for 0 ≤ n ≤ 10, 000
g2 (n) = 3
16. Express the following recurrence relation in asymp-
totic notations: n , for n > 10, 000
n
T (n) = 2T + n
2
Which of the following is true?
(a) g1(n) is O(g2 (n)). (b) g1(n) is O(n3 ).
(a) Q(n log n) (b) Q(n log2n) (c) g2 (n) is O(g1(n)). (d) g2 (n) is O(n).
(c) Q(n2) (d) O(n3)
Solution: Looking at the following figure:
Solution: By using Master Theorem,
g2(n) = n3
T(n) = 2T(n/2) + n
a = 2, b = 2, f(n) = n
g(n)= (n logb a ) = n log2 2 = Θ(n) g1(n) = n3
Order of g(n) = order of f (n), so by case 2, T(n) = g1(n) = n2
Θ(f (n)⋅log n) = Θ(n log n)
Ans. (a)
g2(n) = n3
17. What is the height of n-node, k-ary heap?
(a) Q(logk n) (b) Q(nk) 100 10,000
(c) Q(k log2 n) (d) Q(n/k)
g2(n) = n
Solution: If h is the height of k-ary heap (h ≥ 2)
H, then the number of nodes `n’ in H such that Therefore, we get
k(h) - 1 k(h + 1) - 1
<n≤ n2 ≤ n3 for N ≥ 10, 000
k -1 k -1
So h = Q(logk n). g1(n) = O(g2 (n))
Ans. (a) Ans. (a)
1. Consider the following three claims: Which are depth-first traversals of the above graph?
(I) (n + k) = Q(n ),where k and m are constants
m m
(a) I, II and IV only (b) I and IV only
(II) 2n+1 = O(2n) (c) II, III and IV only (d) I, III and IV only
(III) 22n+1=O(2n) (GATE 2003: 1 Mark)
Which of these claims is correct?
Solution: Option II is incorrect. There is no edge
(a) I and II (b) I and III between f and e.
(c) II and III (d) I, II and III Ans. (d)
(GATE 2003: 1 Mark)
4. The usual Q(n2) implementation of insertion sort
Solution: Option I: Consider k = 1; the expansion to sort an array uses linear search to identify the
of (n + 1)m = f(n). position where an element is to be inserted into the
Option II: 2n+1 = 2.2n = O(2n). already sorted part of the array. If, instead, we use
Option III: 22n+1 = 2.22n = O(22n). binary search to identify the position, the worst-
Options I and II are correct. case running time will
Ans. (a)
(a) remain Q(n2) (b) become Q(n(log n)2)
2. Let G be an arbitrary graph with n nodes and k (c) become Q(n log n) (d) become Q(n)
components. If a vertex is removed from G, the (GATE 2003: 1 Mark)
number of components in the resultant graph must
necessarily lie between Solution: Worst-case complexity will remain O(n2).
This is because binary search is used to locate
(a) k and n (b) k − 1 and k + 1 the position of element will take log2(n) time, but
(c) k − 1 and n − 1 (d) k + 1 and n − k inserting an element at its desired position will
take O(n) time in worst case.
Solution: Minimum components comes out to be Ans. (a)
when the independent vertex, which itself is a com-
ponent, is removed. In that case, the number of 5. In a heap with n elements with the smallest element at
components left is k − 1. the root, the 7th smallest element can be found in time
(a) Q(n log n) (b) Q(n)
The maximum range comes when the removed
(c) Q(log n) (d) Q(1)
vertex isolates all the vertices, In that case, the
number of components comes out to be n − 1.
(GATE 2003: 1 Mark)
Ans. (c)
Solution: Minimum element as root means it is
3. Consider the following graph:
min heap. Searching 7th minimum element takes
O(log n) time.
a Ans. (c)
(0 + 1)* if P = NP
L=
h f otherwise
Solution: If a language L belongs to class P and 10. What would be the worst-case time complexity of the
class NP then it is called recursive. insertion sort algorithm, if the inputs are restricted
Ans. (a) to permutations of 1…n with at most n inversions?
7. A graph G = (V, E ) satisfies |E | ≤3 |V | −6. The (a) Q(n2) (b) Q(n log n)
min-degree of G is defined as (c) Q(n )
1.5
(d) Q(n)
min{degree(v)}
(GATE 2003: 2 Marks)
v∈ V
Solution: With at most n inversions, insertion
Therefore, min-degree of G cannot be sort takes log n time. So, if there are n elements,
complexity will be O(n log n).
(a) 3 (b) 4 (c) 5 (d) 6
Ans. (a)
(GATE 2003: 2 Marks)
11. The cube root of a natural number n is defined as
Solution: Substituting value of E = V(V − 1)/2, the largest natural number m such that m3 ≤ n.
we get V = 3 or 4. Thus, the minimum degree of The complexity of computing the cube root of n (n
each vertex could be 1 or 2. Hence, 3 cannot be the is represented in binary notation) is
min degree.
Ans. (a) (a) O(n) but not O(n0.5)
(b) O(n0.5) but not O((log n)k) for any constant k > 0
8. Consider two languages L1 and L2, each on the (c) O((logn)k) for some constant k > 0 but not
alphabet ∑. Let f: ∑→∑ be a polynomial time O((log log n)m) for any constant m > 0
computable bijection such that (∀ x)[x ∈ L1 iff f (x) (d) O((log log n)k) for some constant k > 0.5 but
∈ L2]. Further, let f −1 also be polynomial time not O((log log n)0.5)
computable. (GATE 2003: 2 Marks)
Which of the following CANNOT be true? Solution: The complexity of computing the cube
(A) L1 ∈ P and L2 is finite root of n is O(K3 n2 log(B)). Using binary search it
(B) L1 ∈ NP and L2 ∈ P takes O((log n)k) for some constant k > 0 but not
(C) L1 is undecidable and L2 is decidable O((log n)m for any constant m > 0.
(D) L1 is recursively enumerable and L2 is recursive Ans. (c)
(GATE 2003: 2 Marks) 12. Let G = (V, E) be an undirected graph with a sub-
graph G1 = (V1, E1). Weights are assigned to edges
Solution: L1 is undecidable and L2 is decidable
are contradictory, as L1 ≤p L2. Thus, if L2 is decid-
of G as follows:
able ⇒L1 is decidable. 0 if e ∈ E1
w(e) =
Ans. (c) 1 otherwise
Common Data Questions 8 and 9: In a permuta- A single-source shortest path algorithm is executed
tion a1…an of n distinct integers, an inversion is a on the weighted graph (V, E, w)with an arbitrary
pair (ai, aj) such that i < j and ai > aj. vertex v1 of V1 as the source. Which of the fol-
lowing can always be inferred from the path costs
9. If all permutations are equally likely, what is the
computed?
expected number of inversions in a randomly
chosen permutation of 1…n? (a) N
umber of edges in the shortest paths from v1
n(n - 1) n(n - 1)
to all vertices of G.
(a) (b) (b) G1 is connected.
2 4 (c) V1 forms a clique in G.
n(n + 1)
(c) (d) 2n[log2 n ] (d) G1 is a tree.
4 (GATE 2003: 2 Marks)
(GATE 2003: 2 Marks)
Solution: Let us take four vertices v1, v2, v3 and v4.
Solution: Inversion means i < j and ai > aj in pair
The vertex v1 is in G1 such that v1 is adjacent to v3
(ai, aj).
and v2 is adjacent to v4. Now after apply shortest path
1 n(n - 1)
Average number of inversions = (n C2 ) = algorithm we get v1v2 = 1, v1v4 = 1 and v1v3 = 0. From
2 2 these results, we can say that v1 is not a clique in G1.
Ans. (b) Ans. (b)
13. What is the weight of a minimum spanning tree of Let A be an n × n array initialized as follows:
the following graph?
1 if ( j, k) ∈ E
A[ j, k ] =
2 g 19
j 0 otherwise
b
6 8 14 Consider the following algorithm:
3
c 5 for i = 1 to n
1 2
for j = 1 to n
a
for k = 1 to n
2 15 h 4
d
i A [j, k] = max (A[j, k] (A[j, i]+ A [i, k]);
4 9
8 8 Which of the following statements is necessarily true
11 A
f
2
for all j and k after terminal of the above algorithm?
(a) A[j, k] ≤ n.
e
(b) If A[j, j] ≥ n − 1, then G has a Hamiltonian
cycle.
(a) 29 (b) 31 (c) 38 (d) 41
(c) If there exists a path from j to k, A[j, k] contains
(GATE 2003: 2 Marks)
the longest path lens from j to k.
Solution: A minimum spanning tree will contain (d) If there exists a path from j to k, every simple
n − 1 edges. Here, 10 − 1 = 9 edges will be included. path from j to k contain most A[j, k] edges.
Total weight = 1 + 2 + 2 + 2 + 3 + 4 + 4 + 5 (GATE 2003: 2 Marks)
+ 8 = 31. Solution: If there exists a path from j to k, every
Ans. (b) simple path from j to k, it must contain A[j, k] edges.
14. The following are the starting and ending times of Ans. (d)
activities A, B, C, D, E, F, G and H, respectively, 16. Consider the label sequences obtained by the fol-
in chronological order: lowing pairs of traversals on a labelled binary tree:
`as bs as ae ds ae es fs be de gs ee fe hs ge he’
(i) Preorder and postorder
Here, xs denotes the starting time and xe denotes (ii) Inorder and postorder
the ending time of activity X. We need to schedule (iii) Preorder and inorder
the activities in a set of rooms available to us. An (iv) Level order and postorder
activity can be scheduled in a room only if the room
is reserved for the activity for its entire duration. Which of these pairs identifies a tree uniquely?
What is the minimum number of rooms required? (a) (i) only (b) (ii) and (iii)
(a) 3 (b) 4 (c) 5 (d) 6 (c) (iii) only (d) (iv) only
(GATE 2003: 2 Marks) (GATE 2004: 2 Marks)
18. Suppose each set is represented as a linked list with Solution: The maximum time taken by a for
elements in an arbitrary order. Which of the opera- loop is n. So, the complexity is Q(n).
tions among union, intersection, membership and Ans. (c)
cardinality will be the slowest?
21. The time complexity of the following C function is
(a) Union only (assume n > 0):
(b) Intersection and membership int recursive (int n) {
(c) Membership and cardinality if (n == 1)
(d) Union and intersection return (1);
(GATE 2004: 2 Marks) else
return (recursive (n – 1) + recursive (n – 1));
Solution: To calculate cardinality just counts the }
number of nodes in a list and for membership, just (a) O(n) (b) O(n log n)
traverse the list and look for a match. So, cardinal- (c) O(n2) (d) O(2n)
ity and membership is not the slowest. (GATE 2004: 2 Marks)
But For getting intersection and union of L1 and
L2, we have to perform search in both list L1 and Solution: We have
L2. So, they are the slowest one in the given list. T (n) = 2T (n - 1), n>1
Ans. (d)
T (2) = 2T (1) = 21
19. Suppose we run Dijkstra’s single-source shortest-
path algorithm on the following edge-weighted T (3) = 2T (2) = 22
directed graph with vertex P as the source: …
n -1
1 T (n) = 2 = O(2n )
Q R Ans. (d)
1 4 2 22. The recurrence equation
6 1 T (1) = 1
P S
2 T (n) = 2T (n - 1) + n, n≥2
3 evaluates to:
7
T U (a) 2n+1 − n − 2 (b)2n − n
2 (c) 2n+1 − 2n − 2 (d)2n + n
In what order do the nodes get included into the (GATE 2004: 2 Marks)
set of vertices for which the shortest path distances Solution: We have
are finalized?
T (n) = 2T (n - 1) + n, n≥2
(a) P, Q, R, S, T, U (b) P, Q, R, U, S, T
T (2) = 2T (1) + 2 = 4
(c) P, Q, R, U, T, S (d) P, Q, T, R, U, S
(GATE 2004: 2 Marks) T (3) = 2T (2) + 3 = 11
Solution: Nodes with minimum weight are included T (4) = 2T (3) + 4 = 26
first. The sequence will be P, Q, R, U, S, T. …
Ans. (b)
T (n) = 2n +1 - n - 2
20. Let A [1,…, n ] be an array storing a bit (1 or 0) at Ans. (a)
each location, and f(m) is a function whose time
complexity is Θ(m).
23. Let G1 = (V, E1) and G2 = V, E2 be connected
graphs on the same vertex set V with more than
Consider the following program fragment written two vertices. If G1 ∩ G2 = (V, E1 ∩ E2) is not a con-
in a C-like language: nected graph, then the graph G1 ∪G2 = (V, E1 ∪ E2)
counter = 0; (a) cannot have a cut vertex.
for (i = l ; i < = n; i++)
(b) must have a cycle.
{if (A [i] == 1) counter++;
else {f (counter); counter = 0;} (c) must have a cut-edge (bridge).
} (d) has chromatic number strictly greater than
those of G1 and G2.
The complexity of this program fragment is
(GATE 2004: 2 Marks)
(a) W(n2) (b) W(n log n) and O(n2)
(c) Q(n) (d) o(n) Solution: Option (b) is the most appropriate option.
(GATE 2004: 2 Marks) Ans. (b)
30. The space complexity of the above function is: 34. Let S be an NP-complete problem and Q and R
be two other problems not known to be in NP. Q
(a) O(1) (b) O(n)
is polynomial time reducible to S, and S is polyno-
(c) O(n!) (d) O(nn)
mial time reducible to R. Which one of the follow-
Solution: The space complexity is the number ing statements is true?
of elements to be stored. This will depend on the (a) R is NP-complete. (b) R is NP-hard.
height of tree produced. Maximum storage required (c) Q is NP-complete. (d) Q is NP-hard.
is O(n). (GATE 2006: 1 mark)
Ans. (b)
Solution: R is NP-hard because S is polynomial
31. Suppose we modify the above function foo() and time reducible to R.
store the values of foo(i), 0 < = I < n, as and Ans. (b)
when they are computed. With this modification,
the time complexity for function foo() is signifi- 35. A set X can be represented by an array x[n] as
cantly reduced. The space complexity of the modi- follows:
fied function would be: 1 if i ∈ X
x[i] =
(a) O(1) (b) O(n) 0 otherwise
(c) O(n2) (d) O(n!) Consider the following algorithm in which x, y and
z are Boolean arrays of size n:
Solution: Complexity will remain the same. With
modification also, the required space will be same. algorithm zzz(x[] , y[], z [])
Ans. (b) {
int i;
Linked Answer Questions 32 and 33: We are given for (i=0; i<n; ++i)
9 tasks T1, T2, …. T9. The execution of each task z[i] = (x[i] ^ ~y[i]) V (~x[i] ^ y[i])
requires one unit of time. We can execute one task }
at a time. Each task Ti has a profit Pi and a dead- The set Z computed by the algorithm is:
line di. Profit Pi is earned if the task is completed
before the end of the (di)th unit of time. (a) (X intersection Y )
(b) (X union Y )
Task T1 T2 T3 T4 T5 T6 T7 T8 T9 (c) (X−Y ) intersection (Y−X )
(d) (X−Y ) union (Y−X )
Profit 15 20 30 18 18 10 23 16 25 (GATE 2006: 2 Marks)
Deadline 7 2 5 3 4 5 2 7 3 Solution:
x[i]^~y[i] = only 1's in x where corresponding
32. Are all tasks completed in the schedule that gives entry in y is 0 → X − Y
maximum profit? ~x[i]^y[i] = only 1's in y where corresponding
(a) All tasks are completed entry in x is 0 → Y − X
(b) T1 and T6 are left out The operator `V ’ results in a union of the above
(c) T1 and T8 are left out two sets.
(d) T4 and T6 are left out Ans. (d)
(GATE 2005: 2 Marks)
36. Let SHAM3 be the problem of finding a Hamiltonian
Solution: cycle in a graph G = (V,E) with V divisible by 3
T2 T7 T9 T5 T3 T8 T1 and DHAM3 be the problem of determining if a
Hamiltonian cycle exists in such graphs. Which one
T4 and T6 are left out. of the following is true?
Ans. (d)
33. What is the maximum profit earned? A B AoB
(a) 147 (b) 165 (c) 167 (d) 175 True True True
(GATE 2005: 2 Marks) True False True
Solution: Maximum profit = 20 +23 + 25 + 18 + False True False
30 + 15 + 16 = 147 False False True
Ans. (a)
(a) Both DHAM3 and SHAM3 are NP-hard Solution: If we use Queue data structure to im‑
(b) SHAM3 is NP-hard, but DHAM3 is not plement Dijkstra’s shortest path algorithm on
(c) DHAM3 is NP-hard, but SHAM3 is not unweighted graphs Then shortest path will be
(d) Neither DHAM3 nor SHAM3 is NP-hard calculated in linear time O(|V | + |E |). Basically we
(GATE 2006: 2 Marks) do BFS traversal of graph to get the shortest path.
Ans. (a)
Solution: Both are NP-hard problems, and finding 40. Consider the following graph:
the Hamiltonian cycle is also an NP-hard problem.
Ans. (a) 2
37. Consider the following recurrence:
(
T (n) = 2T n + 1,
) T (1) = 1
1
b 4 d
1
Which one of the following is true? 2
a 3 3 f
(a) T(n) = Q(log log n) (b) T(n) = Q(log n) 5
(c) T(n) = Q(sqrt(n)) (d) T(n) = Q(n) 6 4
c e
(GATE 2006: 2 Marks)
7
Solution: Let n = 2m. Then, we have
43. Let w be the minimum weight among all edge weights 46. In the following C function, let n ≥ m:
in an undirected connected graph. Let e be a specific int gcd(n,m)
edge of weight w. Which of the following is false? {
if (n%m ==0) return m;
(a) There is a minimum spanning tree containing e.
n = n%m;
(b) If e is not in a minimum spanning tree T, then return gcd(m,n);
in the cycle formed by adding e to T, all edges }
have the same weight.
(c) Every minimum spanning tree has an edge of How many recursive calls are made by this function?
weight w. (a) Q(log2n) (b) W(n)
(d) e is present in every minimum spanning tree. (c) Q(log2log2n) (d) Q(sqrt(n))
(GATE 2007: 2 Marks) (GATE 2007: 2 Marks)
Solution: Options (a), (b) and (c) are correct. But
Solution:
option (d) is incorrect as there may be many edges
Here, Tn = 1 + (T0 T1 + + Tn -1 ) = Θ(log 2 n)
1
of weight w in the graph and edge e may not be in n
some of the minimum spanning trees. Ans. (a)
Ans. (d)
44. Consider the following C code segment: 47. What is the time complexity of the following recur-
sive function?
int IsPrime(n)
{ int DoSomething (int n)
int i,n; {
for(i=2;i<=sqrt(n);i++) if (n <= 2)
if(n%i == 0) return 1;
{printf(‘Not Prime\n’); return 0;} else
return 1; return (DoSomething (floor(sqrt(n)))+ n);
} }
Let T(n) denote the number of times the for loop (a) Q(n) (b) Q(n log2n)
is executed by the program on input n. Which of (c) Q(log n) (d) Q(log2log2n)
the following is true? (GATE 2007: 2 Marks)
(a) T(n) = O(sqrt(n)) and T(n) = W(sqrt(n))
Solution: Recursive relation for the DoSomething() is
(b) T(n) = O(sqrt(n)) and T(n) = W(1)
(c) T(n) = O(n) and T(n) = W(sqrt(n)) ( T (n) = T ( n ) + C1 ) if n > 2
(d) None of the above Let n = 2m, so T(n) = T( 2m )
(GATE 2007: 2 Marks) Let T(2m) = S(m)
Solution: The for loop runs maximum for sqrt(n) From the above two expressions, we have
and minimum for 1. Therefore, T(n) = S(m)
T(n) = O(sqrt(n)) and T(n) = W(1) S(m) = S(m/2) + C1
Ans. (b) S(m) = O(log2m) (by Master Theorem)
S(m) = O(log2log2n) (because n = 2m)
45. In an unweighted, undirected connected graph, the
From the above, we have S(m) = T(n) = O(log2log2n)
shortest path from a node S to every other node
Ans. (d)
is computed most efficiently in terms of time com-
plexity by Linked Answer Questions 48 and 49: Suppose
(a) Dijkstra’s algorithm starting from S the letters a, b, c, d, e, f have probabilities
(b) Warshall’s algorithm 1 1 1 1 1 1
, , , , , , respectively.
(c) Performing a DFS starting from S 2 4 8 16 32 32
(d) Performing a BFS starting from S
(GATE 2007: 2 Marks) 48. Which of the following is the Huffman code for the
letter a, b, c, d, e, f?
Solution: Time complexity of BFS is O(|E | +
|V |), whereas time complexity of Dijkstra’s is (a) 0, 10, 110, 1110, 11110, 11111
(b) 11, 10, 011, 010, 001, 000
O(½V½2 + E ) and Warshall’s algorithm is O(|V |3 )
(c) 11, 10, 01, 001, 0001, 0000
and DFS cannot be used for finding shortest path. (d) 110, 100, 010, 000, 001, 111
Ans. (d) (GATE 2007: 2 Marks)
Solution: The minimum number of comparison 54. The subset-sum problem is defined as follows:
would be 1 since if an element exists more than n/2 Given a set S of n positive integers and a positive
times in a sorted list than it has to be in middle. integer W, determine whether there is a subset of S
The middle index of an array can be reached in a whose elements sum to W. An algorithm Q solves
constant time. this problem in O(nW ) time. Which of the follow-
Ans. (a) ing statements is false?
56. Which entry of the array X, if true, implies that Similarly, proceeding further the same way, we can
there is a subset whose elements sum to W ? reach all nodes and compute the shortest path.
Ans. (d)
(a) X[1, W] (b) X[n, 0]
(c) X[n, W] (d) X[n −1, n] 58. Consider the following functions:
(GATE 2008: 2 Marks)
f (n) = 2n g(n) = n ! h(n) = n log n
Solution:
Which of the following statements about the
If we get the entry X[n, W] as true, then there is a
asymptotic behaviour of f(n), g(n) and h(n) is true?
subset of {a1, a2, …, an} that has sum as W.
Ans. (c) (a) f(n) = O(g(n)); g(n) = O(h(n))
(b) f(n) = W(g(n)); g(n) = O(h(n))
57. Dijkstra’s single-source shortest path algorithm (c) g(n) = O(f(n)); h(n) = O(f(n))
when run from vertex a in the following graph (d) h(n) = O(f(n)); g(n) = W(f(n))
computes the correct shortest path distance to (GATE 2008: 2 Marks)
Solution: The recursive expression is Solution: The value can be computed in either
n 3n row-major or column-major order.
T (n) = T + T + cn
4 4
Ans. (b)
T (n) = Q(n log n) 67. Two alternative packages A and B are available for
processing a database having 10k records. Package
This expression is of randomized quick sort. A requires 0.0001 n2 time units and package B
Ans. (b) requires 10nlog10n time units to process n records.
Common Data Questions 65 and 66: A subsequence What is the smallest value of k for which the pack-
of a given sequence is just the given sequence with age B will be preferred over A?
some elements (possibly none or all) left out. We (a) 12 (b) 10
are given two sequences X[m] and Y[n] of lengths (c) 6 (d) 5
m and n, respectively, with indexes of X and Y (GATE 2010: 1 mark)
starting from 0.
Solution:
0.0001 n2< 10 n logn ⇒
65. We wish to find the length of the longest common
subsequence (LCS) of X[m] and Y[n] as l(m, n),
where an incomplete recursive definition for the 10-5 n < log n ⇒ 10-5 × 10k < k (as log10 n = k)
function l(i, j) to compute the length of the LCS of
Hence, k −5 > 0 or k> 6. Thus, for k = 6, B will
X[m] and Y[n] is given as follows:
be preferred.
l (i, j) 0, if either i=0 or j=0 Ans. (c)
= expr1, if i,j>0 and X[i -1] = Y[j - 1]
= expr2, if i,j>0 and X[i - 1] = Y[j - 1] 68. The weight of a sequence a0, a1, … an −1 of real
Which one of the following options is correct? numbers is defined as
(a) expr1 ≡ (i −1, j) + 1
(b) expr1 ≡ l(i, j −1) a0 + a1 /2 + + an -1 / 2n -1
(c) expr2 ≡ max(l(i −1, j), l(i, j −1)) A subsequence of a sequence is obtained by delet-
(d) expr2 ≡ max(l(i −1, j −1), l(i, j)) ing some elements from the sequence, keeping the
(GATE 2009: 2 Marks) order of the remaining elements the same. Let X
Solution: LCS problem is solved using dynamic denote the maximum possible weight of a subse-
programing in which every row is dependent upon quence of a0, a1, …, an − 1 and Y the maximum
previous row and column. So, if two characters at ith possible weight of a subsequence of a1, a2, …, an − 1.
row and jthcolumn do not match then the length is Then X is equal to
calculated as: (a) max(Y, a0 + Y ) (b) max(Y, a0 + Y )
expr2 = max(l(i −1, j), l(i, j −1)) (c) max(Y, a0 + 2Y ) (d) a0 + Y/2
The values of l(i, j) could be obtained by Solution: This involves dynamic programming in
dynamic programming based on the correct recur- algorithms. Given that
sive definition of l(i, j) of the form given above, X= Maximum weight from the sequence (a0,a1,a2,
using an array L[M, N], where M = m + 1 and N
…an −1) = a + a1 + a2 + + n -1
= n + 1, such that L[i, j] = l(i, j). a
2n -1
Ans. (c) 0
2 4
66. Which one of the following statements would be Y = Maximum weight from the sequence (a1,a2, …
true regarding the dynamic programming solution
a2 a3 a -1
for the recursive definition of l(i, j)? an−1) = a1 + + + + nn-
2 4 2 2
(a) All elements L should be initialized to 0 for the
values of l(i, j) to be properly computed. Here, we have to find X in terms of Y.
(b) The values of l(i, j) may be computed in a row- a2 a3 a -1
major order or column-major order of L(M, N). So, X = a1 + + + + nn- =Y
2 4 2 2
(c) The values of l(i, j) cannot be computed in
+ + nn -
either row-major order or column-major order a1 a2 a 1
if a0 + +
of L(M, N). 2 4 2 -1
(d) L[p, q] needs to be computed before L[r, s] if a a a -1
either p < r or q < s. < a1 + 2 + 3 + + nn-
(GATE 2009: 2 Marks) 2 4 2 2
Solution: Path: 1 → 0 → 4 → 2
Weight: 1 + 4 + 3 = 8 4 5 2 4 5 1 2
Ans. (b)
4 5 2 4 5 1 2
(c) (d) 76. Which of the given options provides the increasing
10 order of asymptotic complexity of functions f1, f2,
5
f3 and f4?
f1(n) = 2n; f2(n) = n3/2; f3(n) = n log2n;
5 6 2 8 f4 (n) = n log2 n
(a) f3, f2, f4, f1 (b) f3, f2, f1, f4
(c) f2, f3, f1, f4 (d) f2, f3, f4, f1
4 8 2 1 1 4 6 10 (GATE 2011: 2 Marks)
78. What will be the cost of the minimum spanning MST with 6 nodes have weight = 3 + 4 + 6 + 8
tree (MST) of such a graph with n nodes? + 10 = 31
Ans. (c)
(a) (1/12) (11n2 − 5n) (b) n2 −n + 1
(c) 6n −11 (d) 2n + 1 80. Assuming P! = NP, which of the following is true?
(GATE 2011: 2 Marks)
(a) NP-complete = NP
Solution: (b) NP-complete ∩ P = F
Let us have 2 nodes in the tree, then minimum (c) NP-hard = NP
spanning tree is (d) P = NP-complete
v1 - v2 (GATE 2012: 1 mark)
Total weight = 3
Minimum spanning tree for 3 nodes would be Solution:
v1 - v2
|
v3 NP
Total weight= 3 + 4 = 7 NPC
P NP-hard
Minimum spanning tree for 4 nodes would be
v1 - v2 - v4
|
v3
Total weight= 3 + 4 + 6 = 13 Diagram clearly shows that NP-complete ∩ P = e.
From the above examples, we observe that when we Ans. (b)
add ith node, the weight of spanning tree increases
81. The worst-case running time to search for an ele-
by 2i − 2. Let T(n) be the weight of minimum
ment in a balanced binary search tree with n2n
spanning tree.
elements is
ìT ( n - 1) + ( 2n - 2 ) , n>2
ï (b) O(n2n)
T (n) = í
(a) O(n log n)
3, n =1 (c) O(n) (d) O(log n)
ï
î 0, n=2 (GATE 2012: 1 mark)
The recurrence can be written as sum of series Solution: The worst-case complexity of a bal-
(2n − 2) + (2n − 4) + (2n − 6) + (2n − 8) + … 3, anced binary search tree is Q(log n), where n is the
and solution of this recurrence is n2 − n + 1. number of elements; here, n is n2n, so
Ans. (b)
log(n2n ) = log n + n log 2 = log n + n
79. The length of the path from v5 to v6 in the MST of
previous question with n = 10 is So,the complexity is Q(n).
Ans. (c)
(a) 11 (b) 25 (c) 31 (d) 41
(GATE 2011: 2 Marks) 82. The recurrence relation capturing the optimal exe-
Solution: Basic structure of all MSTs with more cution time of the Tower of Hanoi problem with n
than 5 nodes(for the above given graph specifica- discs is
tion) would be following: (a) T (n) = 2T (n - 2) + 2
(b) T (n) = 2T (n - 1) + n
1 2 4 6
(c) T (n) = 2T (n/2) + 1
even number of nodes (d) T (n) = 2T (n - 1) + 1
3 (GATE 2012: 1 mark)
83. Let G be a simple undirected planar graph on 10 S and T. Which one will be reported by Dijkstra’s
vertices with 15 edges. If G is a connected graph, shortest path algorithm? Assume that, in any itera-
then the number of bounded faces in any embed- tion, the shortest path to a vertex v is updated only
ding of G on the plane is equal to when a strictly shorter path to v is discovered.
(a) 3 (b) 4 2
(c) 5 (d) 6
(GATE 2012: 1 mark) E 2 G
C 1
Solution: We have 1
A 1 4 3
Given that vertices (V ) = 10 and edges (E) = 15 3
Number of bounded and unbounded faces (F) = ? 4
We know that V - E + F = 2 7 3
D
10 - 15 + F = 2 ⇒ F = 7 S T
4 5
3 5
So, the bounded faces = F −1 = 7 −1 = 6
Ans. (d)
3
B F
84. Let w(n) and A(n) denote, respectively, the worst-
case and average-case running time of an algorithm (a) SDT (b) SBDT
executed on an input of size n. Which of the follow- (c) SACDT (d) SACET
ing is always true? (GATE 2012: 2 Marks)
(a) A(n) = W(W(n)) (b) A(n) = Q(W(n)) Solution: Apply Dijkstra’s shortest path algorithm.
(c) A(n) = O(W(n)) (d) A(n) = o(W(n))
(GATE 2012: 1 mark) S A B C D E G T
0 — — — — — — —
Solution: 0 4 3 — 7 — — —
A(n) = O(W(n))
Ans. (c) 0 4 3 5(A) 7 — — —
0 4 3 5(A) 7 6(C) — —
85. Let G be a complete undirected graph on 6 verti- 0 4 3 5(A) 7 6(C) 8(E) 10(E)
ces. If vertices of G are labelled, then the number
of distinct cycles of length 4 in G is equal to So, the minimum path will cover S → A → C → E
(a) 15 (b) 30 → T, with total weight 10.
(c) 90 (d) 360 Ans. (d)
(GATE 2012: 2 Marks) 88. Which one of the following is the tightest upper
bound that represents the number of swaps
Solution: The number of distinct cycles of length
required to sort n numbers using selection sort?
4 is
C4 × (4 - 1)! = 6C4 × 3 ! = 90
6 (a) O(log n) (b) O(n)
(c) O(n log n) (d) O(n2)
Ans. (c) (GATE 2013: 1 mark)
86. A list of n strings, each of length n, is sorted into Solution: The maximum number of swaps
lexicographic order using the mergesort algo- required for n elements is n.
rithm. The worst-case running time of this com‑ Ans. (b)
putation is
89. Which one of the following is the tightest upper
(a) O(n log n) (b) O(n2 log n) bound that represents the time complexity of insert-
(c) O(n2 + log n) (d) O(n2) ing an object into a binary search tree of n nodes?
(GATE 2012: 2 Marks)
(a) O(1) (b) O(log n)
Solution: The worst-case runtime: n log n. (c) O(n) (d) O(n log n)
For n strings: n(n log n), which gives n2 log n. (GATE 2013: 1 mark)
Ans. (b) Solution: In case of skewed binary tree, the tightest
87. Consider the directed graph shown in the figure below. upper bound of the insert node is O(n).
There are multiple shortest paths between vertices Ans. (c)
log n
91. Which of the following statements are TRUE?
K = Θ
(1) The problem of determining whether there log log n
exists a cycle in an undirected graph is in P.
log n log n
(2) The problem of determining whether there Θ(k log k) = Θ
log log n log log n
log
exists a cycle in an undirected graph is in NP.
(3) If a problem A is NP-Complete, there exists log n
⇒ Θ (log log n - log log log n)
a non-deterministic polynomial time algorithm log log n
to solve A.
log n
(a) 1, 2 and 3 (b) 1 and 2 only ⇒ Θ (log log n) ⇒ Θ(log n)
(c) 2 and 3 only (d) 1 and 3 only log log n
(GATE 2013: 1 mark) Ans. (d)
95. Consider the following operation along with What is the worst-case time complexity of a
enqueue and dequeue operations on queues, where sequence of n MultiDequeue() operations on an ini-
k is a global parameter: tially empty queue?
MultiDequeue(Q){ (a) Q(n) (b) Q(n + k)
m = k (c) Q(nk) (d) Q(n2)
while (Q is not empty and m > 0) { (GATE 2013: 2 Marks)
Dequeue(Q)
m = m - 1 Solution: Any combination of n enqueue and
}} dequeue operations will take Q(n).
Ans. (a)
PRACTICE EXERCISES
Set 1
1. What will be the height of a d-ary heap having n 8. The number of nodes in a complete binary tree of
elements? level 5 is —————————.
(a) O(log n ×d) (b) O(d log n) 9. A graph is non-planar if and only if
(c) O(log n/log d) (d) O(log d/log n)
(a) It does not contain subgraphs homeomorphic
2. Quick sort gives O(n log n) worst-case performance to k5 and k3,3.
if the pivot is selected as: (b) It does not contain subgraphs isomorphic to k5
and k3,3.
(a) First element of the array
(c) It does contain subgraph isomorphic to k5 and
(b) Median of first, last and middle elements
k3,3.
(c) Arithmetic mean of the first and last elements
(d) It does contain subgraph homeomorphic to k5
(d) None of the above
and k3,3.
3. A complete full binary tree with 10 leaves
10. To find all pairs of shortest distance in a graph, we
(a) cannot have more than 4 nodes can use
(b) has exactly 19 nodes
(a) dynamic programming approach
(c) has exactly 17 nodes
(b) bidirectional search approach
(d) cannot have more than 19 nodes
(c) merging approach
4. What is the minimum number of edges to be (d) recursion approach
removed from a complete bipartite graph of 6
nodes K(6), so that graph becomes planar? 11. Sorting is useful for
32. Which of the following sorting algorithms is stable? 41. Which of the following is not the in-place sorting?
(a) Quick sort and insertion sort (a) Insertion sort (b) Heap sort
(b) Insertion sort and bubble sort (c) Merge sort (d) Selection sort
(c) Quick sort and heap sort
42. Which of the following does not use the concept of
(d) Quick sort and bubble sort
dynamic programming?
33. The minimum number of edges in a connected
(a) Bellman-Ford algorithm
cyclic graph with n vertices is
(b) Kruskal’s algorithm
(a) n (b) n −1 (c) Dijkstra’s algorithm
(c) n (d) n + 1 (d) Prim’s minimum spanning tree
34. If there exists at least one path between every pair 43. We use dynamic programming approach when
of vertices in a graph, the graph is called (a) It can be implemented on 0—1 knapsack problem
(a) Complete graph (b) Hamiltonian graph (b) The solution has optimal substructure
(c) Connected graph (d) Mesh (c) The given problem can be reduced to the 3-SAT
problem
35. The depth of a complete binary tree with n nodes is (d) The brute-force algorithm is not applicable
(a) log(n + 1) −1 (b) log(n) −1 44. Which of the following algorithms is not a divide
(c) log(n −1) (d) log(n) and conquer algorithm by nature?
36. The maximum number of nodes contained by a (a) Dijkstra’s algorithm (b) Heap sort
binary tree of level l is (c) Selection sort (d) Quick sort
(a) 2l (b) 2l 45. What is recurrence for worst case of quick sort and
(c) l2 (d) log(l −1) what is the time complexity in worst case?
37. f(n) = O(g(n)) implies (a) R ecurrence is T (n) = T (n/2) + Ο(n/2) and
(a) f(n)= O(g(n)) only time complexity is O(n2).
(b) f(n)= W(g(n)) only (b) Recurrence is T (n) = T (n - 1) + O(n) and time
(c) f(n) = O(g(n)) and f(n) = W(g(n)) complexity is O(n2).
(d) f(n) = Q(g(n)) only (c) Recurrence is T (n) = 2T (n / 2) + O(log n) and
time complexity is O(n log n).
38. Assuming P! = NP, which of the following is true? (d) Recurrence is T (n) = T (n / 10) + Ο(n × 9/10)
(a) P = NP and time complexity is O(n3).
(b) NP-complete ∩P = F Set 2
(c) NP-hard = NP
(d) NP-complete = P 1. Map the following statements regarding quick sort
39. Which of the following statements is true? with true (T) or false (F), respectively.
1. The problem of determining whether there exists a S1: Quick sort is an in-place sorting algorithm.
cycle in an undirected graph is in P. S2: Quick sort is stable.
2. The problem of determining whether there exists a S3: Quick sort performs best when numbers are
cycle in an undirected graph is in NP. sorted or almost sorted.
3. If a problem A is NP-complete, there exists a non- S4: The worst running time of quick sort using ran-
deterministic polynomial time algorithm to solve A. domizedpartition is O(n log n).
S5: It is an example of greedy technique.
(a) 1, 2 and 3 (b) 1 and 3
(c) 2 and 3 (d) 1 and 3 (a) TFFFF (b) TTFTT
(c) TFFTF (d) FTFTF
40. Let X is a problem that belongs to the class NP.
Which of the following is true? 2. What will be the output after the second pass of
bubble sort to sort the following number sequence
(a) There is no polynomial time algorithm for X. in an ascending order?
(b) If X can be solved, then P = NP.
(c) If X is NP-hard, then it is NP-complete. 357642
(d) X may be represented by Bachmann-Landau (a) 3 5 6 4 2 7 (b) 3 5 4 6 2 7
notations. (c) 3 5 4 2 6 7 (d) 2 3 4 6 5 7
3. The number of spanning trees for a complete graph 10. Solve the following given recurrence relation:
with seven vertices is T (n) = 2T (n/2) + n2.
(a) 25 (b) 75 (a) Q(n2) (b) Q(n2 lognn)
(c) 35 (d) 22×5 (c) Q(n log2n) (d) None of the above
18. Let P be the quick sort program to sort numbers in 25. A sorting technique is called stable if
ascending order. Let t1 and t2 be the time taken by
(a) it takes O(n log n) time
the program for the input [1 2 3 4] and [5 4 3 2 1],
(b) it maintains the relative order of occurrence of
respectively. Which of the following holds?
non-distinct elements
(a) t1 = t2 (b) t1 > t2 (c) it uses divide and conquer paradigm
(c) t1 < t2 (d) t1 = t2 + 5 log 5 (d) it takes O(n) space
19. Which of the following algorithm(s) can be used to 26. If one uses straight two-way merge sort algorithm
sort n integers in the range [1…n3] in O(n) time? to sort the following elements in ascending order:
20, 47, 25, 8, 9, 4, 40, 30, 12, 17, then the order of
(a) Heap sort (b) Quick sort these elements after second pass of the algorithm is
(c) Merge sort (d) Radix sort
(a) 8, 9, 15, 20, 47, 4, 12, 7, 30, 30
20. The recurrence relation that arises in relation with (b) 8, 15, 20, 47, 4, 9, 30, 40, 12, 17
the complexity of binary search is (c) 15, 20, 47, 4, 8, 9, 12, 30, 40, 17
(a) T (n) = T (n/2) + k, where k is a constant (d) 4 8, 9, 15, 20, 47, 12, 17, 30, 40
(b) T (n) = 2T (n/2) + k, where k is a constant
27. Randomized quick sort is an extension of quick sort
(c) T (n) = T (n/2) + log n where the pivot is chosen randomly. What is the
(d) T (n) = T (n/2) + n worst-case complexity of sorting n numbers using
randomized quick sort?
21. Which one of the following statements is false?
(a) O(n) (b) (n log n)
(a) O ptimal binary search tree construction can be
(c) O(n2) (d) O(n!)
performed efficiently using dynamic programming.
(b) Breadth-first search cannot be used to find con-
28. Kruskal’s algorithm for finding a minimum span-
nected component of a graph.
ning tree of a weighted graph G with n vertices and
(c) Given the prefix and postfix walks over a
m edges has the time complexity of
binary tree, the binary tree cannot be uniquely
constructed. (a) O(n2) (b) O(mn)
(d) Depth-first search can be used to find con- (c) O(m2) (d) O(m log n)
nected components of a graph.
29. A binary search tree is generated by inserting in
22. The recurrence relation T(1) = 2 and T(n) = order the following integers: 50, 15, 62, 5, 20, 58, 91,
T (n) = 3T (n/4) + n has the solution T(n) equal to
3, 8, 37, 60, 24. The number of nodes in the left sub-
(a) O(n) (b) O(log n) tree and right subtree of the root, respectively, is
3/4
(c) O(n ) (d) None of the above (a) (4, 7) (b) (7, 4)
(c) (8, 3) (d) (3, 8)
23. Quick sort is run on two inputs shown below to
sort in ascending order 30. Which one of the following algorithm design tech-
(i) 1, 2, 3, …, n niques is used in finding all pairs of shortest dis-
(ii) n, n −1, n −2, …, 2, 1 tances in a graph?
Let C1 and C2 be the number of comparisons made (a) Dynamic programming (b) Backtracking
for the inputs (i) and (ii), respectively. Then (c) Greedy (d) Divide and conquer
(a) C1 < C2
(b) C1 > C2 31. Give the correct matching for the following pairs:
(c) C1 = C2
(d) We cannot compare for arbitrary n A. O(log n) P. Selection
24. Let T(n) be the function defined by T(1) =1, B. O(n) Q. Insertion sort
T (n) = 2T ([n/2]) + n for n ≥ 2. Which of the
following statements is true? C. O(n log n) R. Binary search
(a) T (n) = O( n ) (c) T (n) = O(log n)
D. O(n2) S. Merge sort
(b) T (n) = O(n) (d) None of the above
Set 1
1. (c) 9. (d) 17. (b) 25. (d) 33. (c) 41. (c)
2. (d) 10. (a) 18. (a) 26. (b) 34. (c) 42. (d)
3. (c) 11. (d) 19. (a) 27. (52) 35. (a) 43. (b)
4. (b) 12. (c) 20. (b) 28. (d) 36. (b) 44. (b)
5. (c) 13. (c) 21. (a) 29. (d) 37. (c) 45. (b)
6. (a) 14. (c) 22. (b) 30. (b) 28. (b)
7. (c) 15. (b) 23. (b) 31. (b) 39. (a)
8. (63) 16. (b) 24. (c) 32. (c) 40. (c)
Set 2
1. (a) Quick sort is an in-place sorting technique but not 4. (b) Let the two sets be Q and R and if X ∈Q, then
stable. It performs worst when numbers are sorted. the BFS tree contains X in level 0, all the nodes of
The worst running time of quick sort using random- R in level 1 and remaining nodes of Q other than X
ized partition is O(n2). Quick sort is an example of a in level 2, so maximum height is 2.
divide-and-conquer algorithm technique.
5. (c) The recurrence relation T(n) for time complex-
2. (c) ity of linear(n) is
k -1
Pass 1: Pass 2: After second T (k) = c + ∑ T (i), if k > 1 = c
pass: i =1
3 5 7 6 4 2 3 5 6 4 2 7 354267 = else
3 5 7 6 4 2 3 5 6 4 2 7 k -2
3 5 6 7 4 2 3 5 4 6 2 7 T (k) = c + ∑ T (i) + T (k - 1), for k > 1
3 5 6 4 2 7 3 5 4 2 6 7 i =1
T (k) = T (k - 1) + T (k - 1) = 2 × T (k - 1)
3. (b) The number of spanning trees for Kn is nn −2 =
So
77−2 = 75.
T (k) = 2k -1 c
6. (b) Space complexity of large(k) is proportional Case 2: If A[i] is found in the second attempt,
to the height of recursion tree which is k. Thus, number of comparisons = 2. Thus, probability of
S(k) = k = Q(log(T (k)), since T(k) = Q(2k). the case is given as
n -1 1
7. (a) Can be done using DFS in O(|V |) time. ×
n n
8. (b) By case 1 of the Master Theorem Case 3: If A[i] is found in the third attempt,
9. (a) The depth of a binary tree with n nodes is number of comparisons = 3. Thus, probability of
the case is given as
2d +1 - 1 = N n -1 n -1 1
× ×
So, we have n n n
2d +1 = N + 1 There are actually infinite such cases. So, we have
Taking log, we get the following infinite series:
1 n -1 1 n -1 n -1 1
(d + 1) log 2 = (N + 1) + × ×2 + f = × × ×3 +
n n n n n n
d + 1 = log2 (N + 1)
After solving this series, we will get `n’ comparisons.
d = log2 (N + 1) - 1 17. (c) Recursive relation for procedure A(n) is
10. (a) From the master theorem, here we have a = 2, T (n) = T ( n ) + c1 if n > 2
b = 2 and f(n) = n2. So
Use substitution method to solve this.
g(n) = n log b⋅a = n
18. (a) Quick sort takes the worst time if the elements
Here, order of f(n) > order of g(n), so we get are already in sorted orders (it could be in ascend-
T (n) = O(f (n)) = O(n2 ) ≈ Q(n2 )
ing or descending). As both the inputs are already
sorted, it will take the same time in both the cases.
11. (a) For w, there is f(n) > cg(n). So there is no 19. (d) Quick sort, merge sort and heap sort running
equality. Thus, I is correct and II is false. time are (n log n), whereas radix sort is a non-
12. (c) We have f (n) = O(g(n)) which means comparative integer sorting algorithm that sorts
f (n) < cg(n) data in O(n) time.
It is true only if g(n) >c(f(n)) which means 20. (a) In binary search, only half of the array where
g(n) = W(f (n)) the element exists is searched and the other half is
left. So, we get
13. (b) The generating function for the Fibonacci
n
T + K
number G(z) is
2
z
G(z) =
1 - z - z2 21. (b) Depth-first search and breadth-first search are
capable of finding whether a given graph is con-
14. (b) We have
nected and are also capable of finding connected
n
∑ O(n) = O(1) + O(2) + O(3) + + O(n)
components of a graph in linear time. So, option
(b) is false.
22. (a) Using case 1 of master theorem, we get n log 4 3 < n
i=0
n(n + 1
= O
2
= O(n2 )
23. (c) Both of the given cases are quick sort worst-
case problems, so the number of comparisons will
15. (d) The worst-case search time in a singly linked be equal.
list is when the element is in the last position or
the element does not exist in the linked list. So it 24. (b) Use case 1 of master theorem.
is similar to linear search and requires `n’ compari- 25. (b) A sorting algorithm is said to be stable if two
sons in the worst case. objects with equal keys appear in the same order
16. (a) (Number of comparisons for a case × probabil- in the sorted output as they appear in the input
ity for the case) unsorted array. Some sorting algorithms are stable
by nature such as insertion sort, merge sort, bubble
Case 1: If A[i] is found in the first attempt, number of sort, etc. And some sorting algorithms are not,
comparisons = 1. Thus, probability of the case = 1/n. such as heap sort, quick sort, etc.
26. (b) Given: 20, 47, 15, 8, 9, 4, 40, 30, 12, 17. The number of nodes in the left subtree and the
right subtree of the root, respectively, is (7, 4).
Here, two-way merge sort is used, so a group of Therefore, option (b) is correct.
two is taken at once. The second pass is shown as
follows: 30. (a) Bellman-Ford algorithm is used to find all-pair
8, 15, 20, 47 4, 9, 30, 40 12, 17 shortest distances in a graph, and it is a dynamic
programming technique.
20, 47 8, 45 4, 9 30, 40 12, 17 31. (b) Merge sort took O(n log n) to sort a list of ele-
ment. Binary search time complexity is O(log n).
20, 47 15, 8 9, 4 40, 30 12, 17 Insertion sort worst-case running time complexity
is O(n2 ) .
After second pass of the algorithm element, the
32. (c) d(r, u) and d(r, v) will be equal when u and
order is 8, 15, 20, 47, 4, 9, 30, 40, 12, 17.
v are at same level; otherwise, d(r, u) will be less
27. (c) Randomized quick sort worst-case complexity
than d(r, v).
is O(n2 ); when all the elements are same, it took
worst time. 33. (d) In an undirected graph, if it is a complete
graph, then it has n(n −1)/2 edges. We can choose
28. (d)
to have (or not have) any of the n(n −1)/2 edges.
29. (b) Look at the following figure: So, the total number of undirected graphs with n
vertices is 2(n(n−1)/2).
5 20 58 91
3 8 37 60
24
6
Number of questions
4 Marks 1
Marks 2
3 Total number of questions
2
0
2015 2014 2013 2012 2011 2010
Year Concepts
2015 Turing machine, Regular expression and languages, DFA,
Context free grammar, Recursive language, NDPDA
2014 Regular languages, DFA, RE and REC
2013 Regular languages, DFA, CFL, CFG
2012 Regular languages, DFA and NFA, CFL
2011 Power of DFA, NFA and TM, Regular language, PDA
2010 Regular expression, Recursive language, LR and LL, CFL
THEORY OF COMPUTATION
Syllabus: Regular languages and finite automata, context-free languages and pushdown automata, recursively
enumerable sets and Turing machines, undecidability.
“Automata theory is the subject which describes the where e is called the identity element. Here, Σ*
behaviour of automatic machines mathematically”. In contains every string possible by a, b.
other words, we can say that “Automata Theory is the
Σ+ = Σ* − {Null character (l)}, so Σ+ will con-
study of self-operating virtual machines to help in logical
tain {a, b, aa, bb, ab, …}.
understanding of input and output process without or
with intermediate stages of computation”.
2. Strings: Sequences of zero or more symbols are
The model of automation is shown in Fig. 5.1.
called string of a language. Let there be languages
such as
Finite
automation L1 = a *, then it will consist {e, a, aa, aaa, aaaa, …}
Input lines Output lines string set.
with n states
(q1, q2, ..., qn) L2 = ab*, then the string will be {a, ab, abb, abbb, …}.
L3 = 0*1*, then it will consist {e, 0, 1, 01, 001,
0001, 011, 0111, …} string set.
Figure 5.1 | Model of automation.
3. Concatenation: Let there be two strings U = ab
5.2.1 Finite Automaton Model Characteristics and V = aab. Then concatenation of these two
languages is UV = abaab and VU = aabab.
The characteristics of finite automaton are described below: Concatenations of two strings satisfy associative
property but not commutative. So, UV ≠ VU.
1. Input: Input values from input alphabet Σ con- 4. Length of a string: Let U, V and W be three
tains I1, I2, I3, … In at discrete time. are the input strings. The length of a string tells the number of
values from the input alphabet Σ at discrete time. alphabets in that string.
2. Output: Output O1, O2, O3, …; On is the output
set for this computation model are different out- If U = 011, length of |U | = 3.
puts of this model. If V = ababa, length of |V | = 5.
3. States: At any instance of time, the finite auto If W = abababb, length of |W | = 7.
maton will be in one of the states q1, q2, q3, …, qn, 5. Reversal of a string: Let there be a string W = aab,
are the set of states on which the finite automa- then the reverse of W is
ton will be at some instance of time. W r = baa
4. States relation: State relation describes that how to If W = W r, then the string is a palindrome. WW r is
reach on next state using present input and output. called an even palindrome, and W × W r is called an
At any instance of time, the next state of automaton odd palindrome, where x is an alphabet. Let there
is determined by the present input and present state. be two strings U and V, then the reverse of UV is
5. Output relation: Either one state or number of
states can be obtained as output. The next state is |UV |r = V rU r
achieved using state relation. Either only state or 6. Power of a string: Let there be a string W, then
both the input and state are obtained as output. W0 = e
When an automaton reads an input symbol, it moves W1 = W
to the next state, which is given by the state relation. W2 = W ⋅ W
W 3 = W ⋅ W 2, so W n + 1= W ⋅ W n = W n ⋅ W
5.2.2 Technical Terms Let there be a string U = {010}, then the power of U:
Technical terms that are required in finite automaton U0 = e
are given as following: U 1 = {010}
U 2 = {010010}
1. Alphabets: A finite non-empty set of symbols are
the alphabets of a language. 7. Substring: Any set of conjugative symbols is
called a substring.
Example 5.1
Example 5.2
Σ = {a, b}
Let W = {abc} is a string. Substrings of W are (e, a, b,
Π = {0, 1} c, ab, bc, abc), where e is a substring of every string. If
the length of a string is n, then the number of substrings
Σ and Π are the sets which hold two alphabets. They
possible is
are called binary alphabets for language. n(n + 1)
+1
(a + b)* = Σ* = {e , a, b, aa, bb, ab, ba, ¼} 2
Example 5.5
14. Language subtractions: Let L1 = {ab, ba, baa}
and L2 = {aab, ba} be two languages. S → aSb/l
A → aA/aB/Cb
Then L1 − L2 = {ab, baa} and L2 − L1 = {aab}.
Ba → a/Da
15. Language XOR: Let L1 = −ab, ba, baa} and L2
= −aab, ba} be two languages.
2. Type 1: This is context-sensitive grammar (CSG)
L1 ⊕ L2 = (L1 − L2) ∪ (L2 − L1) = (L1 ∪ L2) − which is used to generate context-sensitive lan-
(L1 ∩ L2) guages. CSG (G) = u → v, where u, v ∈ (V ∪ T)*
L1 ⊕ L2 = {ab, baa, aab} and “u” must have at least one variable. There
is one additional rule also, u ≤ v , which means
16. L1⋅L2: Let L1 = {ab, ba, aab} and L2 = {ba, ab}, length of “u” is less than or equal to “v”.
then
L1⋅L2 = {abba, abab, baba, baab, aabba, aabab} Example 5.6
S → aSb/l
+
17. L* and L :
L*= {L ∪ L ∪ L ∪ L …}
0 1 2 3 Aa → aA/aB/Cb
L+= {L* − l} B → a/Da
B → a/Da
CFL
B → a/Da
a q1 {q1, q2} q1
a a q2 q2
q0 q1 q3 q2
b
q0 — Starting state
b b
q2 — Final state (accepting state)
q2
a, b
5.3.3 Comparison of DFA and NFA
The DFA can be represented in a transition table
shown below, which can be determined by the transition The comparison between DFA and NFA is given in
function (d ). Table 5.2.
2. Permanent reject state (trap state): The given Example 5.11
state is permanent reject state because there is self-
DFA which accepts string starting with “a”
loop to some non-final state.
a, b
q0 a q1
q3
a, b
Example 5.12
3. Temporary reject state: The given state is tem- DFA which accepts string ending with “a”
porary reject state because for input `b’, it has a loop
on the non-final state. However, if after input `b’ b a
a
there is another input `a’, then it will halt on final q0 q1
state. b
Regular expression: (a + b)*a
b
a Example 5.13
DFA which accepts string starting with “a” and ending
with “b”
a
b
a b
4. Temporary accepting state: The given state is q0 q1 q3
a
temporary accepting state, as it reaches the final
state if it has input symbol `a’, but if it gets another b
input symbol `b’ on that final state it will halt on
non-final state. q2
a, b
a
Regular expression:
1. a(a + b)* ∩ (a + b)*b (from statement)
b 2. aa*b(b + aa*b)*
Table 5.3 | Some standard regular languages and their regular expression, grammar and machine
∅ {} S→A
a, b
a, b
Λ {l} S→e a, b
a, b
A {a} S→a a a, b
b
a, b
B {b} S→b b a, b
(Continued)
b
(a + b)* {l, a, b, ab, ba, aab, S → aS/bS a, b
aaab, baa, bab, bbb,
baba, …} S → a/b
2. Regular grammar: Type 3 grammar is also called 5.3.7 Pumping Lemma
regular grammar and it generates regular language.
Regular grammar is divided into two types: Pumping lemma is very useful to prove that a certain
language is not a regular language. We know that the
Left linear grammar: V → VT * + T *
loop in finite automata (FA) makes it able to accept
Right linear grammar: V → T *V + T * those strings which have length equal to and greater
A grammar is a regular grammar iff ∃ a left linear than its total number of states.
or right linear grammar for it.
Let there be a string w which has a bigger length (more There are two types of FSMs:
than its states), then we can break this string into three 1. Mealy machines
parts x, y (y should not be null) and z. Let FA has loop 2. Moore machines
for y and w = xyz ∈ L accepted by FA, so w = xyiz for
i = 0, 1, … is also accepted by FA.
5.3.9.1 Mealy Machines
5.3.8 Myhill—Nerode Theorem
In a Mealy machine, the output of an FSM is dependent
Myhill—Nerode theorem tells that the given language on the present state and present input. A Mealy machine
might be regular or not regular. A given language L can be described by a six-tuple set (Q, Σ, Δ, d, l, S ),
is a regular language if and only if the set of equiva- where
lence classes of L is finite, or it satisfies following three 1. Q is a finite and non-empty set of states.
conditions: 2. Σ is an input alphabet.
1. The language L divides the set of all possible strings 3. Δ is an output alphabet.
into mutually separate classes. 4. d is a transition function which maps the pres-
2. If L is a regular language, then the number of ent state and input symbol on to the next state
classes created is finite. Q × Σ → Q.
3. If the number of classes that L creates is finite, 5. l is the output function which maps Q × Σ → Δ
then L is a regular language. ((present state + present symbol) → output).
6. S ∈ Q is the starting state or initial state.
Problem 5.1: Consider a language L consists all
strings over {a, b} ending in b. Prove that L is a Problem 5.2: Consider the Mealy machine shown in
regular language. the figure. Construct the transition table and find the
Solution: Let DFA M = {Q, S, d, q0, F} recognizes output for input aabba.
L = {b, bb, ab, aab, abb, …}.
a/0
We have two classes C0 and C1 defined as follows: a/0 b/1
q0 q1
C0 = {All strings that end in a}
C1 = {All strings that end in b, i.e., accepted
strings (b, ab, abb, bbb, …)}
b/1
Transition diagram for the DFA M is shown below. a/1
a
b
b q2
q0 q1
a b/1
In Moore machine, the output of an FSM is dependent To convert NFA to DFA, following points should be
on the present state only. A Moore machine can be considered:
described by a six-tuple set (Q, Σ, Δ, d, l, S), where 1. In NFA and DFA there is strictly one initial or
1. Q is a finite and non-empty set of states. starting state.
2. Σ is an input alphabet. 2. If there are n states in NFA, then equivalent DFA
3. Δ is an output alphabet. contains ≤ 2n states.
4. d is a transition function which maps the pres- 3. DFA can simulate the behaviour of NFA by
ent state and input symbol on to the next state increasing the number of states.
Q × Σ → Q.
5. l is the output function which maps Q → Δ (pres- Problem 5.4: Construct a DFA equivalent of given NFA.
ent state → output)
a
6. S ∈ Q is the starting state or initial state. b
a
q0 q1
Problem 5.3: Consider the Moore machine shown in
the figure. Construct the transition table and find the a
output for input aabba.
b b b b
a b
q0 q1
Output = 0 Output = 1 q2
a
Solution:
Step 1: Construct a transition table of the given NFA
q2
Output = 0
a, b δ a b
{q1q2} q0 {q0q1}
φ φ φ Present State Next State on Input
D E DE
1 0, 1
0 0, 1
5.3.10.3 Mealy to Moore Machine Now we will make a transition table of the Moore
machine.
In a Mealy machine, the output depends upon both the
present state and the input, and the Moore machine Present Next State Output
output depends only on the present state. While con- State
verting Mealy to Moore we develop a procedure so that
x=0 x=1
all the states of Mealy machines, which are associated
with different outputs, find them. After that, split those →q1 q1 q21 0
states into n parts if the states have n outputs. q20 q21 q20 0
q21 q21 q20 1
qi0 State qi associated
with output 0 Moore machine of 2’s complement:
ε
a/1
q0 a/1 b/1
q1
ollow above steps to remove e move
Step 3: F
between q0 and q2.
b/0
b/1 0 1 0
q0 1 q1 0 q2
q2
a/0 0
Figure 5.4 | Mealy machine. After removing all e moves, this is the final NFA.
Note: m states, n outputs Moore machine ≡ Mealy Note: If NFA with e-moves has n states then NFA
machine contains ≤ m states. without e-moves will have exactly n states.
5.3.12 Some Important DFA 6. L = {w | na(w) mod 2 = 0 and nb(w) mod 2 = 1}:
b
b b b b
a b
a
a
2. DFA having exactly two a’s:
a
b b b b
7. DFA which accepts exactly two a’s and one b:
a a a
a a
3. L = {w | na(w) mod 2 = 0 and nb(w) mod 2 = 0}:
a
a
b b a, b
b
a
a a, b
a
b b b b
b b
a
Pushdown automata (PDA) are devices that recognize
5. L = {w | na(w) mod 2 = 0 or nb(w) mod 2 = 1}: context-free languages. PDA has finite memory in terms
of a stack. A pushdown automaton is a non-determinis-
a tic device. The deterministic version of automata accepts
only a subset of CFL known as deterministic context-
a free language (DCFL).
3. Γ is a finite non-empty set of stack alphabets. leads to some final state of PDA or cause PDA to empty
4. d is the transition function which maps Q × (Σ ∪ its stack. A non-deterministic automaton is equivalent
{l}) × Γ → Q × Γ* to a CFG and more powerful than a deterministic PDA.
5. q0 is the initial state (start state).
6. Z0 ∈ Γ is the starting (top most) stack symbol. Example 5.16
7. F is the set of final states and F ⊆ Q.
Figure 5.5 shows a model of PDA.
a/0,10 a/1,11
q0 q1 δ {(q0,a,0)={(q1,10),(q3,λ)}
$ Ψ I/P Tape δ {(q0,λ,0)={(q3,λ)}
δ {(q1,a,1)={(q1,11)}
λ/0,λ a/0,λ
b/1,λ δ {(q1,b,1)={(q2,λ)}
δ {(q2,b,1)={(q2,λ)}
Reading
z0
Top of
λ/0,λ
b/1,λ δ {(q2,λ,0)={(q3,λ)}
head the stack q4 q3
a
Finite state control This NPDA will recognize the string “aaabbb” by the
following moves:
Three types of operations are performed under the tran- Input head start from left and starting stack symbol is 0.
sition function (d ) of PDA. 1. (q0, a, 0) . (q1, 10)
1. Push element into stack: Let the current stack 2. (q1, a, 10) . (q1, 110)
symbol be z0, current state be q0 and current input 3. (q1, a, 110) . (q1, 1110)
symbol be “a”. So after reading “a”, z2 is to be 4. (q1, b, 1110) . (q2, 110)
pushed into stack and next state may or may not 5. (q2, b, 110) . (q2, 10)
be changed according to the transition function 6. (q2, b, 10) . (q2, 0)
written as 7. (q2, l, 0) . (q3, l)
d (current state, current input symbol, top of stack
As q3 ∈ F, the string is accepted.
symbol) → (next state, new top of stack)
d (q0, a, z0) = (q1, az0)
2. Pop element from stack: If “a” is input symbol 5.4.4 Deterministic PDA
on state q0 with stack top z1 and z1 is popped up
after processing “a” and next state is q1. Then A deterministic pushdown automata (DPDA) is one for
transition function d is written as which every input string has a unique way through the
machine. A PDA M = {Q, Σ, Γ, d, q0, Z0, F} is deter-
d (current state, current input symbol, top of stack
ministic if it satisfies the following two conditions:
symbol) → (next state, top of stack)
d (q0, a, z1) = (q1, l) 1. d(q, a, z) is either empty or only single move.
2. d(q, l, z) ≠ ∅ means d(q, a, z) = ∅ for each a ∈ Σ.
3. No change in state element: If on an input
symbol “a” at state q0 on stack top z1 nothing is The language accepted by DPDA is known as determin-
pushed or popped, then transition function can be istic context-free language (DCFL), and it is noticeable
written as that not all CFLs are DCFLs.
d (q0, a, z1) = (q1, z1)
Example 5.17
5.4.3 Non-deterministic PDA Language L = {an × bn: n ≥ 0}, it can be accepted by the
DPDA because all `a’ characters are inserted into stack
Non-deterministic pushdown automata (NPDA) have till character `x’ come and then symbol `a’ is removed as
more than one choice for a single input such as NFA. many times as many symbol `b’ occur. In the last stack
Transition function of NPDA is Q × (Σ ∪ {l}) × Γ → will be empty.
Q × Γ*. An NPDA accepts an input string if a sequence
In leftmost derivation, we start with initial and replace Problem 5.8: Consider a grammar G = (Vn, T, S, P)
the leftmost variable in each step and finally get the having production {S → aSa |bSb |x}. Check the pro-
string. This approach is known as top-down parsing. duction and find the language generated.
In the parse, start symbol is root, terminals are leaves
(input symbols) and other nodes are variables. We start Solution: Let P1: S → aSa
from the root and replacing the intermediate nodes one P2: S → bSb
by one from left to right reach the leaves. This approach P3: S → x
is also known as recursive descent.
(a, b, x) are terminals and S is a variable. As all the pro-
Here, we have constructed the leftmost derivation ductions are of the form A → a, where a ∈ (Vn ∪ S)*
looking ahead one symbol in the given string. A gram- and A ∈ Vn, hence G is a CFG, and it will produce
mar having this property is known as LL(1). In general, context-free language.
if a leftmost derivation is constructed by looking at the
k symbol ahead in the given string, then the grammar is Language generated: L(G) = {w x wR: w ∈ (a + b)*}
known as LL(k) grammar, where k ≥ 1.
Steps of construction of top-down parser:
5.5.1 Context-Free Grammar
Step 1: E
liminate left recursion (if any) from the given
grammar. A grammar G = (Vn, T, S, P) is said to be a CFG if
production P {u → v} follows these conditions:
Step 2: E
liminate left factoring (if any) from the given
grammar. 1. u → v, where v ∈ (V ∪ T)*
Step 3: If resulting grammar is LL(k) for some k ≥ 1, 2. u ≤ v (length of u is less than v)
then construct a parsing table. 3. Only single variable is allowed in the left side
(means u has single variable only)
5.4.5.2 Bottom-Up Parsing
Example 5.18
In bottom-up parsing, we start with the input string S → aSb/l
and replace the terminals by respective variables such
that these replacements lead to the starting symbol of A → aA/aB/Cb
the grammar. So every step takes input string close to B → a/Da
the starting symbol. This approach is the reverse of top-
down approach. It reads the input from left and uses
the rightmost derivation in reverse order. Bottom-up
parser is also known as shift-reduce (SR) parser. There 5.5.2 Standard Context-Free Language
are three actions in bottom-up parsing:
There are some standard languages under CFL such as:
Step 1: S
hift the current input (token) on the stack and 1. anbn, anb2n, a2nbn, anb2n + 3, a2n + 2b3n + 5, etc.
read the next token. 2. na(w) = nb(w) (means number of “a” equals to
Step 2: R
educe by some suitable LHS of production number of “b” in a language), ambn, where (m = n,
rule. m > n, m < n, etc.)
Step 3: A
ccept, final reduction which leads to starting 3. Palindrome such as [wwR (even palindrome), wawR
symbol. (odd palindrome), etc.]
LMD:
5.5.3 Derivation Tree or Parse Tree
S → AB by production 1)
The string generated by CFG G = (Vn, T, S, P) is S → aaAB (by production 2)
S → aaB (by production 3)
represented by a hierarchical structure called tree.
A derivation tree or parse tree for a CFG is a tree that
satisfies the following conditions: S → aabB (by production 4)
S → aab (by production 5)
1. If A → a1, a2, a3, …, an is a production in G, then
A becomes the father of nodes labelled as a1, a2,
a3, …, an. 5.5.3.2 Rightmost Derivation Tree
2. The root has label S (starting symbol).
3. Every vertex (or node) has a label. A derivation tree is called rightmost derivation (RMD)
4. Internal nodes should be labelled with variables tree if the ordering of decomposed variable is from right
only. to left. Thus, for generating a string w = aab from the
5. The leaf nodes are labelled with e or terminal above grammar:
symbols.
6. The collection of leaves from left to right yields the RMD:
string w.
S → AB (by production 1)
S → AbB (by production 4)
Problem 5.9: Consider the grammar S → S + S → Ab (by production 5)
S |S*S |a|b. Construct derivation (or parse) tree for the
string w = a + b*b. S → aaAb (by production 2)
S → aab (by production 3)
Solution:
S S
A B
S + S
a a A b B
a S * S
∈ ∈
b b
Left to right
Left recursion and left factoring are the major cause A → aA||g1|g2|g3|…|gm
for a grammar to be ambiguous. But presence of these in A → b1|b2|b3|…|bm
a grammar does not mean that grammar is ambiguous,
and similarly absence of these does not mean that gram-
mar is unambiguous. Problem 5.11: Let grammar A → abc |abd |abe remove
left factoring.
5.5.5 Removal of Ambiguity Solution: After removing left factoring, the produc-
tions are replaced by
Ambiguities are left recursion and left factoring. In this A → abA|
A → c |d |e
subsection, removal of various ambiguities is discussed
step by step.
= P − (unit production)
Step 1: Find P Step 1: Identify the nullable variable.
Step 2: Find P production without null.
Step 2: F
ind unit production and draw a unit produc-
tion dependency graph.
Step 3: Add new rules to the grammar. Problem 5.13: Remove null (l) production from the
given CFG grammar.
Problem 5.12: Remove the unit production from the
S → ABaC
given CFG grammar.
A → BC
S → Aa |B
B → b | l
B → A|bb
C → D | l
A → a |bc |B
D→d
Solution:
) = P − (unit production
Step 1: Find a non-unit production (P
Solution:
)
) = P − (unit production)
(P Step 1: Identify nullable variables.
S → Aa Variables B and C directly produce null N = {B, C}.
B → bb But variable A is related to B and C, so A will also
produce a null production.
A → a |bc
Step 2: Find a unit production and draw a unit pro- N = {A, B, C}
duction dependency graph. production without null.
Step 2: Find P
S→B
S → ABaC
B→A
A → BC
A→B
B→b
S A B C→D
D→d
Generate grammar without null production (G |) by
Step 3: Add new rules to the grammar. Check that putting null value to nullable variables one by one in
from each variable through other variables production without null. For example, put A = l in
on which terminals we can reach. S → ABaC, we will get S → BaC. Do this process for
all nullable variables.
S → a|bc|bb
S → ABaC |BaC |AaC |ABa|aC |aB|Aa|a
A → bb
A → BC |B|C
B → a|bc
B→b
Final grammar after removing a unit pro-
duction is as follows: C→D
S → Aa|a|bc|bb D→d
A → a|bc|bb
B → bb|a|bc 5.5.6.3 Removal of Useless Production
Note: Removal of a unit production has made B and
Let G = (Vn, T, S, P) be a CFG. A variable A ∈ V is
the associated production useless.
said to be useless if there is not even a single derivation
which uses that production.
computer. A TM is a generalization of a PDA, which The transition function (d) is defined as d(p, a) = (q, b,
uses a tape instead of tape and stack. The length of the D), where p is the present state, q is the next state a, b ∈ Γ
tape is assumed to be infinite. A tape is divided into and D is the movement {(left (L) or right (R)}.
cells, and one cell can hold one input symbol. The head After reading an input a ∈ Σ, TM does one of the
of the TM is capable to read and write on the tape and following:
can move left or right or remain static.
1. Replaces a by b and moves right (R) as d(p, a) =
TMs accept the languages defined by type 1 gram- (q, b, R).
mar, and these languages are called recursively enumer- 2. Without write, anything moves right (R) as d(p, a) =
able or type 1 languages. (q, a, R).
3. Replace a by b and move left (L) as d (p, a) = (q, b, L).
5.6.1 Model of a Turing Machine 4. Without write, anything moves left (L) as d(p, a) =
(q, a, L).
A TM consists of a tape which is finite at the left end The change of state in the above transition is optional
and infinite at the right end and has a finite control and (means may or may not change).
read/write head (Fig. 5.6).
5.6.1.2 Language Reorganization by a
Turing Machine
Figure 5.6 | Model of a turing machine. Problem 5.17: Design a TM for L = {w: w ∈ (a + b)*
ending in substring abb}
5.6.1.1 Mathematical Description of a Solution: Let TM M = {(q0, q1, q2, q3, h), (a, b), (a, b, #),
Turing Machine d, q0, #, h} accepts language L, where d is defined as
follows:
A TM can be described by a seven-tuple set (Q, Σ, Γ, d,
d(q0, b) = (q0, b, R)
q0, #, F), where
d(q0, a) = (q1, a, R)
1. Q is the finite set of states (not including the halt
state (h)). d(q1, a) = (q1, a, R)
2. Σ is the input alphabet which is a subset of the d(q1, b) = (q2, b, R)
tape alphabet (not including the blank symbol d(q2, a) = (q1, a, R)
d(q2, b) = (q3, b, R)
#).
3. Γ is the finite set of symbols called the tape
alphabet. d(q3, a) = (q1, a, R)
4. d is the transition function which maps from Q × d(q3, b) = (q0, b, R)
Γ → Q × Γ × [L, R].
d(q3, #) = (h, #, S)
5. # ∈ Γ is a special symbol called blank.
6. q0 ∈ Q is the initial state. The transition diagram is shown in the following figure.
7. F ⊆ Q is the set of final states.
A TM can be deterministic or non-deterministic depend- b,b,R b,b,R
ing on the number of moves in a transition. If a TM has
at the most one move in a transition, then it is a deter- a,a,R q b,b,R b,b,R a,a,R
ministic Turing machine (DTM). If there is more than q0 1 q2 q3 h
one move, then it is a non-deterministic Turing machine a,a,R
(NTM). A standard TM reads one cell of the tape and
changes its state (optional) and moves left or right by a,a,R
one cell.
q3 qf
, ,R There are different closure and decidability properties
y,y,R for various languages. Tables 5.6 and 5.7 describe these
properties for different languages.
Operation Languages
Regular DCFL CFL CSL REC RE
∪
∩
Lc
-
*
L1 ∪ R
L1 ∩ R
R
L — — —
h(L) — — —
h−1(L) — — —
L1/L2 — — — —
{Surely exists}
{May or may not exist}
— {Not known}
Table 5.6 is interpreted as follows: second language. For example, if one language is
1. {∪} means that if there are two languages L1 and regular and the other is DCFL, then their union
L2 which are regular then their union will be reg- will be a DCFL.
ular, whereas if languages are DCFL then their 3. LR represents reverse of a language; if there are
union will not be DCFL. two CFLs L1 and L2 then their union will be a CFL
2. {L1 ∪ R} means that from two languages if one is and their intersection will not be a CFL.
regular (R) and the other is different, then their 4. (.) shows concatenation operation.
union will be result in a language similar to the 5. (*) denotes multiplication operation.
IMPORTANT FORMULAS
1. Number of states in a machine which accept m a’ s 12. If there is a comparison between m and n then it
and n b’ s: will be a CFL. Example: Let L = {ambn, (m ≥ n)}.
[(m + 1)*(n + 1) + 1] 13. Let language L = {ambn} and if m or n is non-
2. Mod n machines have n states. linear then it will not be accepted by PDA, so it
will fall in the category of CSL.
3. Mod machine have no trap states.
14. If there are more than one comparison then it will
4. If a machine accepts string length exactly n, then be a CSL. Example: Language L = {ambnco, m ≥ n
it has (n + 2) states. and o ≥ n }.
5. If a machine accepts string length ≤ n, then it has 15. All modular machines are regular language.
(n + 2) states.
16. All palindrome languages are CFL language.
6. If a machine accepts string length ≥ n, then it has
(n + 1) states. Variation of NFA/DFA
7. m States, n outputs Mealy machine ≡ Moore 17. DFA/NFA + (left ↔ right) move ≡ (2 − DFA)/
machine containing ≤ mn + 1 states. (2 − NFA) means whatever a 2 − DFA can do,
that can be done by simple DFA with left — right
8. m States, n outputs Moore machine ≡ Mealy move.
machine containing ≤ m states.
18. DFA/NFA + (left ↔ right) move + (read/write)
9. Output length of Mealy machine is equal to input head ≡ (2 − DFA)/(2 − NFA).
length.
19. DFA + 1 stack ≡ DPDA).
10. Output length of Moore machine is one greater 20. NFA + 1 stack ≡ NPDA).
than the input length because first output symbol
is additional without reading any symbol from the 21. DFA + 2 stack ≡ TM.
input. 22. DFA/NFA + 2 counter ≡ TM.
11. If language is finite then it will surely be regular. 23. DFA/NFA + 2 stack ≡ DFA/NFA + 2 counter.
Example: Let L = {ambn, m + n =10} as m and n
are finite so it is a regular language. But it does not 24. Power of {(DFA/NFA) < (DFA/NFA + 1 counter)
mean that if a language is infinite then it cannot < (DFA/NFA + 1 stack)}
be regular such as (a*b*) is an infinite and regular 25. {(DFA/NFA + 1 counter) < (DFA/NFA + 2
language. counter)}.
SOLVED EXAMPLES
1. Which of the following is a regular expression? In option (c), the given expression will generate
(a) L = {0 1 | m, n ≥ 0}
m n any string “x ” twice, which will be accepted by
(b) L = {0n1n | n ≥ 0}
PDA. So it is not a regular expression.
(c) L = {xx | x ∈ {0, 1}*} In option (d), the given expression will generate
(d) L = {1 0 | n ≥ 0}
n2 n twice the n number of 1’s followed by n number of
0’s, which cannot be accepted by FA. So, it is not
Solution: In option (a), the given expression a regular expression.
will generate any number of 0’s followed by any Ans. (a)
number of 1’s, which can be accepted by FA. So, it
2. The class of context-free language is not closed
is a regular expression.
under
In option (b), the given expression will gener-
ate number of 0’s followed by number of 1’s (a) union
(both should be same in number), which will (b) star
be accepted by PDA. So, it is not a regular (c) repeated concatenation
expression. (d) intersection
Solution: Context-free language is not close under Let S denote the seven bits binary strings in which
intersection. Please refer table 5.6 (closure prop- the first, fourth and last bits are 1. The number of
erty of languages) in text. strings in S that are accepted by M is
Ans. (d)
(a) 6 (b) 5
3. Which of the following is true for regular sets A = (c) 4 (d) 7
(1101 + 1)* and ((1101)*1*)*?
(a) A ⊂ B (b) B ⊂ A Solution: Strings which will be accepted by
(c) A = B (d) A and B are incomparable machine M can be found by fixing 1 at places first,
fourth and last. We will have following four strings:
Solution: Both the regular expressions will gener- 1001001 1001011 1001111 1111001
ate the same set of strings, so both the regular sets Ans. (c)
are equal.
Ans. (c) 7. Which of the following CFG will generate the lan-
guage L = {ambncpdq | m + n = p + q}
4. The language generated by the following grammar
is S → aSa |bSb |e (a) S → aSd |A|B (b) S → aSd |A|B
A → aAc |C A → aAc |D
(a) a b n ≥ 0, m ≥ 0
m n
B → bBd |D B → bBd |C
(b) anbm n ≥ 1, m ≥ 1
C → bCc | e C → CBc | e
(c) S → aSd |A|B (d) S → aSd |A|B
(c) Odd length palindrome
A → aAB |C A → aAc |C
(d) Even length palindrome
17. Let L be the set of all binary strings whose last two (c) All the three languages are context-free.
symbols are the same. The number of states in the (d) Turing machine can be used to recognize all the
minimum state deterministic finite state automa- three languages.
tion accepting L is
Solution: Language L1 is regular because it can
(a) 2 (b) 5 be accept by NFA. Language L2 and L3 are CFL
(c) 8 (d) 3 because they have comparison in between p and q.
Ans. (c)
Solution: Minimum number of states in DFA
which accept all binary strings whose last two sym- 19. Which of the following language over {a, b, c} is
bols are the same is 5. accepted by a deterministic pushdown automata?
(a) wcwR | w ∈ {a, b}*}
(b) wwR | w ∈ {a, b, c}*}
a
(c) {anbncn | n ≥ 0}
(d) {w | w is a palindrome over {a, b, c}
a
B C
a Solution: The language L = wcwR | w ∈ {a, b}*}
b is accepted by DPDA because it is the only one
b which is DCFL. For the above language everything
A a is pushed into the stack until “c” appears. When
a “c” appears all elements are removed from the
b stack if they match every symbol in wR.
D E Ans. (a)
b
20. Which one of the following is the strongest correct
b statement about a finite language over some finite
Ans. (b) alphabet ∑?
18. Consider the languages L1, L2, L3 as given below: (a) It could be undecidable.
(b) It is Turing machine recognizable.
L1 = {1p0q | p, q ∈ N } (c) It is a regular language.
L2 = {1p0q | p, q ∈ N and p = q} (d) It is a context-sensitive language.
L3 = {1p0q | p, q, r ∈ N and p = q = r} Solution: Every finite language is regular and
Which of the following statements is NOT TRUE? also CFL, CSL, RE (Turing machine recognizable)
because regular language ⊂ CFL ⊂ CSL ⊂ RE. So,
(a) Pushdown Automata (PDA) can be used to the strongest correct statement is option (c).
recognize L1 and L2.
(b) L1 is a regular language. Ans. (c)
1. Ram and Shyam have been asked to show that Solution: Reduction from the 3-SAT problem
a certain problem Π is NP-complete. Ram to Π is NP hard and reduction from Π to 3-SAT
shows a polynomial time reduction from the shows that Π is in NP.
3-SAT problem to Π, and Shyam shows a poly-
As Π is in NP and it is NP hard, so it can be
nomial time reduction from Π to 3-SAT. Which
inferred that Π is NP complete.
of the following can be inferred from these
Ans. (c)
reductions?
(a) Π is NP-hard but not NP-complete. 2. If strings of a language L can be effectively enumer-
(b) Π is in NP, but is not NP-complete. ated in lexicographic (i.e., alphabetic) order, which
(c) Π is NP-complete. of the following statements is true?
(d) Π is neither NP-hard nor in NP. (a) L is necessarily finite.
(GATE 2003: 1 Mark) (b) L is regular but not necessarily finite.
(c) L is context-free but not necessarily regular. Solution: Option (a) is False. G is ambiguous due
(d) L is recursive but not necessarily context-free. to the possibility of two parsing trees for the same
string.
(GATE 2003: 1 Mark)
Option (b) is True. Let x = ab and y = ba, so abba
Solution: L is recursive because recursive lan- does not belong to L(G).
guages can be effectively enumerated in lexico- Ans. (b)
graphic order. 6. A single tape Turing machine M has two states
Ans. (d) q0 and q1, of which q0 is the starting state. The
3. Consider the set ∑* of all strings over the alphabet tape alphabet of M is {0, 1, B} and its input
∑ = {0, 1}. ∑* with the concatenation operator for alphabet is {0, 1}. The symbol B is the blank
strings symbol used to indicate end of an input string.
(a) does not form a group The transition function of M is described in the
(b) forms a non-commutative group following table
(c) does not have a right identity element 0 1 B
(d) forms a group if the empty string is removed
from ∑* q0 q1, 1, R q1, 1, R Halt
(GATE 2003: 1 Mark) q1 q1, 1, R q0, 1, L q0, B, L
Solution: Fact
The table is interpreted as illustrated below.
Ans. (a)
The entry (q1, 1, R) in row q0 and column 1 signi-
4. Consider the following deterministic finite state
fies that if M is in state q0 and reads 1 on the cur-
automaton M.
rent tape square, then it writes 1 on the same tape
1 0 0, 1 square, moves its tape head one position to the
right and transitions to state q1.
0 0
1 Which of the following statements is true about M?
(a) M does not halt on any string in (0 + 1)+.
1 (b) M does not halt on any string in (00 + 1)*.
(c) M halts on all string ending in a 0.
Let S denote the set of seven bit binary strings in
(d) M halts on all string ending in a 1.
which the first, the fourth, and the last bits are 1.
The number of strings in S that are accepted by (GATE 2003: 2 Marks)
M is
(a) 1 (b) 5 Solution: M does not halt on any string in (0+1)+.
(c) 7 (d) 8 Ans. (a)
S1: L1 is recursive implies L2 is recursive The output computes 2’s complement of input
S2: L2 is recursive implies L1 is recursive number.
Ans. (b)
Which of the following statements is true?
16. Consider the machine M:
(a) Both S1 and S2 are true.
(b) S1 is true but S2 is not necessarily true. b a
(c) S2 is true but S1 is not necessarily true.
(d) Neither is necessarily true. a b bc b
(GATE 2004: 2 Marks)
a a
Solution: For L1, the membership algorithm can
be constructed as follows. For an arbitrary wi # wj, a, b
Solution: L1 and L2 both are context-free 22. If s is a string over (0 + 1)*, then let n0(s) denote
languages. Their intersection may or may not be the number of 0’s in s and n1(s) the number of 1’s
context-free. So, option (a) is false. in s. Which one of the following languages is not
Ans. (a) regular?
19. Let L1 be a recursive language, and let L2 be a (a) L = [s ∈ (0 + 1)* | n0(s) is a 3-digit prime}
recursively enumerable but not a recursive lan- (b) L = {s ∈ (0 + 1)* | for every prefix s’ of s, |n0(s’)
guage. Which one of the following is TRUE? — n1(s’)| ≤ 2}
(c) L = {s ∈ (0 + 1)*| |n0(s) - n1(s) ≤ 4}
(a) L1 is recursive and L2 is recursively enumerable. (d) L = {s ∈ (0 + 1)*|n0(s) mod 7 = n1(s) mod 5 = 0}
(b) L1 is recursive and L2 is not recursively
(GATE 2006: 2 Marks)
enumerable.
(c) L1 and L2 are recursively enumerable. Solution: Option (a) is finite, can be accepted by
(d) L1 is recursively enumerable and L2 is recursive. DFA.
Option (b), only prefix comparison is possible by
(GATE 2005: 2 Marks) DFA, so it is regular.
Option (c), whole string is compared, which is not
Solution: Recursive languages are closed under possible by DFA because it does not have memory.
complementation but recursive enumerable lan- Option (d), mod machines can be made using DFA.
guages are not. Ans. (c)
Ans. (b)
Linked Answer Questions 23 and 24:
20. Consider the languages 23. Which one of the following grammars generates the
L1 = {wwR | w ∈ {0, 1}*} language L = {ai bi | i ≠ j)?
B → Bb | ∈ B → Bb | b
(b) L2 is a deterministic CFL.
(c) L3 is a CFL, but not a deterministic CFL.
(d) L3 is a deterministic CFL. (GATE 2006: 2 Marks)
(GATE 2005: 2 Marks) Solution: Both options (a) and (b) can produce equal
number of a’s and b’s, which should not be produced
Solution: L2 can be accepted by DPDA, so it is according to grammar. Option (d) cannot produce all
a deterministic context free language. L1 is non- the strings. So, option (c) is the correct answer.
deterministic context free and L3 in not context Ans. (c)
free language. 24. In the correct grammar above, what is the length
Ans. (b) of the derivation (number of steps starring from S)
21. Let L1 = {0n+m1n0m |n, m ≥ 0}, to generate the string al bm with l ≠ m?
L2 = {0n+m1n+m0m |n, m ≥ 0} and (a) max(l, m) + 2 (b) l + m + 2
L3 = {0n+m1n+m0n+m |n, m ≥ 0} (c) l + m + 3 (d) max(l, m) + 3
Which of these languages are NOT context-free? (GATE 2006: 2 Marks)
(a) L1 only (b) L3 only Solution: This can be checked by producing vari-
(c) L1 and L2 (d) L2 and L3 ous strings of different length.
25. For s ∈ (0 + 1)*, let d(s) denote the decimal value 28. Consider the regular language L = (111 + 11111)*.
of s (e.g. d(101) = 5). The minimum number of states in any DFA accept-
Let L = {s ∈ (0 + 1)*l d(s) mod 5 = 2 and d(s)
ing this languages is
mod 7 ≠ 4} (a) 3 (b) 5
Which one of the following statements is true? (c) 8 (d) 9
(a) L is recursively enumerable, but not recursive. (GATE 2006: 2 Marks)
(b) L is recursive, but not context-free. Solution: Nine states are required. The length of
(c) L is context-free, but not regular. string will be 8. Total states required are 8 + 1 =9.
(d) L is regular. Ans. (d)
(GATE 2006: 2 Marks)
29. Which of the following problems is undecidable?
Solution: DFA is possible for L. If for any
(a) Membership problem for CFGs
language, we can make DFA then that language
(b) Ambiguity problem for CFGs
will be regular language. So, L is regular.
(c) Finiteness problem for FSAs
Ans. (d)
(d) Equivalence problem for FSAs
26. Consider the following statements about the con-
(GATE 2007: 1 Mark)
text-free grammar:
G = {S → SS, S → ab, S → ba, S → e } Solution: No algorithm exists to check the ambi-
guity of grammar.
I. G is ambiguous. Ans. (b)
II. G produces all strings with equal number of a’s
and b’s. 30. Which of the following is TRUE?
III. G can be accepted by a deterministic PDA. (a) Every subset of a regular set is regular.
Which combination below expresses all the true (b) Every finite subset of a non-regular set is regular.
statements about G? (c) The union of two non-regular sets is not regular.
(d) Infinite union of finite sets is regular.
(a) I only (b) I and III only
(c) II and III only (d) I, II, and III (GATE 2007: 1 Mark)
(GATE 2006: 2 Marks) Solution: In option (a), anbn is subset of a* b*;
Solution: but it is not regular.
I. G is ambiguous. Null can be produced using Option (b) is correct.
multiple rules [S → e and S → SS]. Two parse Option (c) is union of two context-free languages,
trees will be formed. and thus is regular.
II. It cannot produce all the combinations of equal Option (d) is infinite union of finite sets, and thus
a’s and b’s such as string aabb is not possible. is not regular.
III. Language accepted is (ab + ba)*, which is Ans. (b)
regular. Regular languages are also accepted 31. A minimum state deterministic finite automaton
by DPDA. accepting the language
L = {w | w ∈ {0, 1}*, number of 0’s and 1’s in w are
Ans. (b)
27. Let L1 be a regular language, L2 be a deterministic divisible by 3 and 5, respectively} has
context-free language and L3 a recursively enumer-
able, but not recursive, language. Which one of the (a) 15 states (b) 11 states
following statements is false? (c) 10 states (d) 9 states
(GATE 2007: 2 Marks)
(a) L1 ∩ L2 is a deterministic CFL.
(b) L3 ∩ L1 is recursive. Solution: Divisible by 3 and 5 = 3 × 5 = 15 states
(c) L1 ∪ L2 is context-free. are required.
(d) L1 ∩ L2 ∩ L3 is recursively enumerable. Ans. (a)
(GATE 2006: 2 Marks) 32. The language L = {0i 21i i ≥ 0} over the alphabet
Solution: {0, 1, 2} is
Option (a) is true. DCFL is closed under regular (a) not recursive
intersection. (b) recursive and is a deterministic CFL
Option (b) is false. Intersection is recursively (c) a regular language
enumerable. (d) not a deterministic CFL but a CFL
Options (c) and (d) are true.
Ans. (b) (GATE 2007: 2 Marks)
Solution: L is accepted by DPDA. So, it is deter- (c) It is context-free but not regular.
ministic CFL. (d) It is neither regular nor context-free, but
Ans. (b) accepted by a Turing machine.
33. Which of the following languages is regular? (GATE 2008: 1 Mark)
(a) {wwR ⁄ w ∈ {0, 1}+} Solution: The options are checked by using
(b) {wwRx ⁄ x, w ∈ {0, 1}+} pumping lemma for regular and context-free lan-
(c) {wxwR ⁄ x, w ∈ {0, 1}+} guages. It is found that the language is not regular,
(d) {xwwR ⁄ x, w ∈ {0, 1}+} and hence not context free; but is accepted by the
(GATE 2007: 2 Marks) Turing machine.
Ans. (d)
Solution: Finite automata do not have memory
37. Which of the following are decidable?
element. So, they cannot remember the symbols.
Hence, options (a) and (d) are incorrect. I. Whether the intersection of two regular
Ans. (c) languages is infinite.
Common Data Questions 34 and 35: Consider the II. Whether a given context-free language is
following finite state automaton regular.
III. Whether two pushdown automata accept the
same language.
q3 IV. Whether a given grammar is context-free.
(a) I and II (b) I and IV
b b a b (c) II and III (d) II and IV
(GATE 2008: 1 Mark)
q0 q1 a q2 b
a Solution: Options II and III are undecidable.
a The intersection of two regular languages is deter-
34. The language accepted by this automaton is given mined by an algorithm, and it can be determined
by the regular expression whether the given grammar is context-free or not.
So, options I and II are decidable.
(a) b*ab*ab*ab* (b) (a + b)* Ans. (b)
(c) b*a (a + b)* (d) b*ab*ab*
38. If L and L are recursively enumerable, then L is
(GATE 2007: 2 Marks)
(a) Regular (b) Context-free
Solution: One a is compulsory. Minimum string is (c) Context-sensitive (d) Recursive
`a’ that is accepted by the automaton. b*a (a + b)*
is regular and accepted by the automaton. (GATE 2008: 1 Mark)
Ans. (c)
Solution: A theorem states that when L and L
35. The minimum state automaton equivalent to the both are RE, then L is recursive.
above FSA has the following number of states Ans. (d)
(a) 1 (b) 2 39. Which of the following statements is false?
(c) 3 (d) 4
(a) E very NFA can be converted to an equivalent
(GATE 2007: 2 Marks) DFA.
Solution: Minimized DFA is: (b) Every non-deterministic Turing machine can
be converted to an equivalent deterministic
b Turing machine.
a, b
(c) Every regular language is also a context-free
q0 q1 language.
a
(d) Every subset of a recursively enumerable set is
Ans. (b) recursive.
(GATE 2008: 2 Marks)
36. Which of the following is true for the language
{aP | P is a prime}?
Solution: Option (d) is not necessarily true.
(a) It is not accepted by a Turing Machine. Every recursive set is recursively enumerable, but
(b) It is regular but not context-free. the reverse is not necessarily true.
40. Given below are two finite state automata (→ indi- 42. Match the following.
cates the start state and F indicates the final state) List I List II
2(F) 2 1 2(f) 1 1
(F) Number of formal (Q) X → XbX |XcX |dXf |g
parameters in the
declaration of a function
Which of the following represents the product
automaton Z × Y?
agrees with the number
of actual parameters in
(a) (b) use of that function
a b a b
(G) Arithmetic (R) L = {wcw | w ∈ (a|b)*}
→P S R →P Q S expressions with matched
pairs of parantheses
Q R S Q R S
(H) Palindromes (S) X → bXb|cXc|e
R(F) Q P R(F) Q P
S Q P S Q P Codes:
(a) E — P, F — R, G — Q, H — S
(c) (d) (b) E — R, F — P, G — S, H — Q
a b a b (c) E — R, F — P, G — Q, H — S
→P S Q →P S Q (d) E — P, F — R, G — S, H — Q
(GATE 2008: 2 Marks)
Q R S Q S R
Solution:
R(F) Q P R(F) Q P E — R: In R, first w is checking the declaration of
S P Q A Q P an identifier.
F — P: Actual parameters anbm and formal param-
eters cndm
(GATE 2008: 2 Marks) G — Q: Arithmetic expressions with matched pair
Solution: Option (a) represents the product of parenthesis
correctly. H — S: S is generating palindrome strings.
Ans. (a) Ans. (c)
43. Match the following NFAs with the regular expres-
41. Which of the following statements are true? sions they correspond to
I. Every left-recursive grammar can be converted
to a right-recursive grammar and vice versa. (P) (Q)
1 1 0
II. All e productions can be removed from any con- 0 1
text-free grammar by suitable transformations. 1 0
0 0 0 0
III. The language generated by a context-free
grammar all of whose productions are of the
form X → w or X → wY (where w is a string
of terminals and Y is a non-terminal) is always 1
(R) (S) 1
regular.
0 0
IV. The derivation trees of strings generated by 1 1
a context-free grammar in Chomsky normal 0 1
form are always binary trees. 0 1
Codes: (a) T
he set of all strings containing the substring 00.
(a) P — 2, Q — 1, R — 3, S — 4 (b) The set of all strings containing at most two 0’s.
(b) P — 1, Q — 3, R — 2, S — 4 (c) The set of all strings containing at least two 0’s.
(c) P — 1, Q — 2, R — 3, S — 4 (d) The set of all strings that begin and end with
(d) P — 3, Q — 2, R — 1, S — 4 either 0 or 1.
(GATE 2009, 1 Mark)
(GATE 2008: 2 Marks)
Solution: P → e + 0(01*1 + 00)*01*
Solution: Given regular expression must have
Q → e + 0(10*1 + 00)*0
atleast two zeros 0(0 + 1)*0….
R → e + 0(10*1 + 10)*1
Ans. (c)
S → e + 0(10*1 + 10)*10* 47. Which one of the following is FALSE?
Ans. (c) (a) T here is a unique minimal DFA for every regu-
44. Which of the following are regular sets? lar language.
(b) Every NFA can be converted to an equivalent
I. {anb2m |n ≥ 0, m ≥ 0} PDA.
II. {anbm | n = 2m} (c) Complement of every context-free language is
III. {anbm| n ≠ m} recursive.
IV. {xcy | x, y ∈ (a, b}*} (d) Every non-deterministic PDA can be converted
to an equivalent deterministic PDA.
(a) I and IV only (b) I and III only
(c) I only (d) IV only (GATE 2009: 1 Mark)
If the initial state is A = 0, B = 0, what is the minimum length of an input string which will take the machine to
the state A = 0, B = 1 with output = 1?
(a) 3 (b) 4 (c) 5 (d) 6
(GATE 2009: 2 Marks)
Solution:
Present State Output
(0, 0) → 1
(0, 1) → 0
(1, 0) → 0
(0, 1) → 1
50. Let L = L1 ∩ L2, where L1 and L2 are languages as Solution: The given DFA accepts all the strings
defined below: ending with 00.
Ans. (c)
L1 = {ambmcanbn | m, n3 ≥ 0}
52. Let L1 be a recursive language. Let L2 and L3 be
L2 = {aibick | i, j, k ≥ 0} languages that are recursively enumerable but not
Then L is recursive. Which of the following statements is not
necessarily true?
(a) not recursive.
(b) regular. (a) L2 − L1 is recursively enumerable.
(c) context-free but not regular. (b) L1 − L3 is recursively enumerable.
(d) recursively enumerable but not context-free. (c) L2 ∩ L3 is recursively enumerable.
(d) L2 ∪ L3 is recursively enumerable.
(GATE 2009: 2 Marks)
(GATE 2010: 1 Mark)
Solution: Intersection of two regular languages is
Solution:
regular, but L1 is not regular.
Subtraction of recursive language and recursive
Ans. (c)
enumerable is not necessarily recursive enumerable.
51. In the following figure, DFA accepts the set of all Ans. (b)
strings over {0, 1} that
53. Let L = {w ∈ (0 + 1)*|w has even number of 1’s},
1 1 0 i.e., L is the set of all bit strings with even number
of 1’s. Which one of the regular expressions below
0 0 represents L?
(a) (0*10*1)* (b) 0*(10*10*)*
1 (c) 0*(10*1)*0* (d) 0*1(10*1)*10*
54. Consider the languages 58. Which of the following pairs have DIFFERENT
expressive power?
L1 = {0i1j | i ≠ j}, L2 = {0i1j | i = j}, L3 = {0i1j | i = 2 j + 1}, L4 = {0i1j | i ≠ 2 j}
(a) Deterministic finite automata (DFA) and non-
}, L2 = {0i1j | i = j}, L3 = {0i1j | i = 2 j + 1}, L4 = {0i1j | i ≠ 2 j} deterministic finite automata (NFA)
Which one of the following statements is true? (b) Deterministic pushdown automata (DPDA) and
non-deterministic pushdown automata (NPDA)
(a) Only L2 is context-free (c) Deterministic single-tape Turing machine and
(b) Only L2 and L3 are context-free Non-deterministic single-tape Turing machine
(c) Only L1 and L2 are context-free (d) Single-tape Turing machine and multi-tape
(d) All are context-free Turing machine
(GATE 2010: 2 Marks) (GATE 2011: 1 Mark)
69. Consider the DFA A given below. 3. M is a Turing machine. Is L(M) regular?
1 4. A is a DFA and N is an NFA. Is L(A) = L(N)?
1 (a) 3 only (b) 3 and 4 only
(c) 2 and 3 only (d) 2 and 3 only
0 0 (GATE 2013: 2 Marks)
{{ }}
2. L(A) = L((11*0 + 0)(0 + 1)*0*1*)
L = 0 p1q 0r p, q, r ≥ 0
3. For the language accepted by A, A is the L11 = 0 p1q 0r p, q, r ≥ 0
PRACTICE EXERCISES
Set 1
1. What is the meaning of the regular expression Σ* 4. The string 1101 does not refer to the set repre-
0101 Σ*? sented by
(a) Any string containing “1” as substring (a) 110*(0 + 1) (b) 1(0 + 1) * 101
(b) Any string containing “01” as substring (c) (10)*(01)*(00 + 11) (d) (11 + 00) + 01
5. The grammar S → 0S1, S → 0S, S → S1, S → 0
(c) Any string containing “0101” as substring
(d) All strings containing “0101” as substring
will generate a
2. Which of the following is called a regular operation? (a) Context-free language
(a) Union, star (b) Regular language
(b) Concatenation (c) Context-sensitive language
(c) Star, concatenation (d) Push-Down Automata
(d) Concatenation, star, union 6. Which of the string is accepted by a given NFA?
3. In Mealy machine, if we enter the string of length a, b
5, then what will be the output string length?
c
(a) 4 (b) 5
(c) 6 (d) 1
(a) c . (a ∪ b)* (b) c . a*b* 15. Consider the following context-free languages
(c) c . (ab)* (d) c . (ab) L = {0m1n|m ≠ n; m, n ≥ 0}
7. Let L denotes the language generated by the gram- L1 = {0m1n|m > n ≥ 0} and L2 = {0m1n|n >
mar S → 0S0 | 00. Which of the following is true? m ≥ 0} then
B → aabb |e B → BBBA|e
number of 0’s and 1’s in w are divisible by 3 and 4,
(c) S → AB (d) S → BA
respectively} has
A → aaaA|e A → BA|e (a) 12 states (b) 11 states
B → ab |aabb |e B → AAAB|e (c) 10 states (d) 8 states
22. A = {<M>|Turing machine that accepts only one Which should be the final state(s) of the DFA if it
string}. The language A is should accept strings starting with “a” and ending
with “b”?
(a) context-free
(b) recursively enumerable (a) {q0} (b) {q1}
(c) non-recursively enumerable (c) {q0, q1} (d) {q3}
(d) regular
29. Which of the following strings will be accepted in
23. A push-down automata recognizing anbn has mini- the above case if q0 and q1 are accepting states?
mum of n states where n is
1. ababab
(a) 3 (b) 0 2. babaaa
(c) 1 (d) 2 3. aaaba
24. Given the following statements: (a) 1, 2 (b) 2, 3
S1: Every context-sensitive language L is recursive. (c) 1, 3 (d) None of these
S2: There exists a recursive language that is not
30. Which of the following represents the set of accept-
context-sensitive.
ing states if the language to be accepted con-
Which statement is correct? tains string having the same starting and ending
(a) Only S1 is correct. symbols?
(a) {q0} (b) {q0, q3}
(b) Only S2 is correct.
(c) {q3} (d) {q3, q1}
(c) Both S1 and S2 are not correct.
(d) Both S1 and S2 are correct.
31. If L and ~L are recursive enumerable then L is
25. The Greibach normal form grammar for the lan-
guage L = {anbn + 1 | n ≥ 0} is (a) regular and context-free
(b) only context-free
(a) S → aSB, B → bSB |e
(c) regular and context-sensitive
(b) S → aSB, B → bSB |b
(d) recursive
(c) S → aSB |b, B → b
(d) S → aSb |b, B → a 32. The following grammar
26. Recursive enumerable languages are not closed under G = (N, T, P, S)
N = {S, A, B, C, D, E}
(a) Union (b) Intersection T = (a, b, c}
(c) Reversal (d) Complement P: S → ABCD, BCD → DE, D → aD, D → a,
E → bE, E → c is
27. Consider the following statements:
(a) Type 1
I. Recursive languages are closed under com- (b) Type 1 but not Type 3
plementation. (c) Type 2 but not Type 3
II. Recursively enumerable languages are closed (d) Type 0 but not Type 1
under complementation.
III. Recursively enumerable languages are closed 33. Context-free languages are closed under
under union. (a) Concatenation and union
Which of the above statements are true? (b) Kleene closure and union
(c) Concatenation and union
(a) II only (b) I and III
(d) Concatenation, union, Kleene closure
(c) I and II (d) I, II and III
28. Consider the transition diagram of a DFA as given 34. The number of DFAs with the set of states {1,2,
below …n} (n ≥ 1) over the alphabet {0, 1}, with 1 as
a the initial state is
b
Start a b (a) 2n*2 (b) n2
(c) n22n/2 (d) n2n2n
a
b 35. The problem whether the given CFG is finite or
infinite is
a
a
b (a) Undecidable
b (b) NP hard and decidable
18. Which of the following statement is false? (a) generate an inherently ambiguous language
(b) generate an ambiguous language but not inher-
(a) Every finite subset of a non-regular set is ently so
regular. (c) are unambiguous
(b) Every subset of a regular set is regular. (d) can generate all possible fixed length valid
(c) Every finite subset of a regular set is regular. computation for carrying out addition, subtrac-
(d) The intersection of two regular sets is regular. tion, multiplication and division, which can be
expressed in one expression
19. If two finite state machines are equivalent, they
should have the same number of 26. In which of the cases stated below is the following
statement true?
(a) states (b) edges “For every non-deterministic machine M1 there
(c) states and edges (d) none of these exists an equivalent deterministic machine M2 rec-
ognizing the same language”
20. Context-free languages are closed under
I. M1 is non-deterministic finite automation
(a) union, intersection II. M1 is a non-deterministic PDA
(b) union, Kleene closure III. M1 is a deterministic Turing machine
(c) intersection, complement IV. there is no machine M1 for which the given
(d) eomplement, Kleene closure statement is true
21. Let LD be the set of all languages accepted by a (a) I and II (b) I and III
PDA by final state and LE the set of all languages (c) I, II and III (d) II and IV
accepted by empty stack. Which of the following is 27. Which of the following conversion is not possible
true? (algorithmically)?
(a) LD = LE (b) LD ⊃ LE (a) Regular grammar to context-free grammar
(c) LD ⊂ LE (d) None of these (b) Non-deterministic FSA to deterministic FSA
(c) Non-deterministic PDA to deterministic PDA
22. If L1 is a context-free language and L2 is regular, (d) Non-deterministic Turing machine to determin-
which of the following is false? istic Turing machine
(a) L1 — L2 is not context-free. 28. Which one of the following is not decidable?
(b) L1 ∩ L2 is context-free.
(a) G ive a Turing machine M, a string s and an
(c) ~L1 is not context-free.
integer k, M accepts s within k steps
(d) ~L2 is regular.
(b) Equivalence of two Turing machines
(c) Languages accepted by a given finite state
23. The language accepted by pushdown automation
machine is non-empty
in which the stack is limited to 10 items is best
(d) Languages accepted by a context-free grammar
described as
is non-empty
(a) context-free
29. Which of the following is true?
(b) regular
(c) deterministic context-free (a) T
he complement of a recursive language is
(d) recursive recursive.
(b) The complement of a recursively enumerable (III) Whether a finite state automation halts on all
language is recursively enumerable. inputs
(c) The complement of a recursive language is (IV) Membership problem for type 0 languages
either recursive or recursively enumerable.
(a) II and IV (b) I and II
(d) The complement of a context-free language is
(c) III and IV (d) I and IV
context-free.
33. For two regular languages
30. The C language is
(a) A context-free language L1 = (a + b)*a and L2 = b(a + b)*
(b) A context-sensitive language
(c) A regular language The intersection of L1 and L2 is given by
(d) Parsable fully only by a Turing machine
(a) (a + b)*ab (b) ab(a + b)*
31. Any string of terminals that can be generated by (c) a(a + b)*b (d) b(a + b)*a
the following CFG is
34. Consider the following problem X.
S → XY
X → aX |bX |a “Given a Turing machine M over the input alpha-
bet ∑, any state q of M and a word ∑*, does the
Y → Ya|Yb|a
computation of M on w visit the state q”
(a) Has at least one “b”
Which of the following statements about X is correct?
(b) Should end in “a”
(c) Has no consecutive a’s or b’s (a) X is decidable.
(d) Has at least two a’s (b) X is undecidable but partially decidable.
32. Which of the following problems are undecidable? (c) X is undecidable and not even partially
(I) Membership problem in context-free languages decidable.
(II) Whether a given context-free language is (d) X is not a decision problem.
regular
Set 1
1. (d) 9. (c) 17. (c) 25. (c) 33. (d) 41. (a)
2. (d) 10. (d) 18. (d) 26. (c) 34. (d) 42. (d)
3. (b) 11. (c) 19. (b) 27. (b) 35. (a) 43. (b)
4. (c) 12. (b) 20. (c) 28. (b) 36. (b) 44. (a)
5. (b) 13. (c) 21. (a) 29. (c) 37. (d) 45. (a)
6. (a) 14. (c) 22. (b) 30. (b) 38. (b)
7. (b) 15. (c) 23. (a) 31. (d) 39. (c)
8. (b) 16. (c) 24. (d) 32. (d) 40. (c)
Set 2
1. (c) Regular languages are closed under union, The given language L produces all strings having
interaction, Kleene closure and complementation. equal number of x and y, where y follows symbol x.
So option (c) is correct and remaining options (a), Only expression ⇒ E → Ey | xy can produce equal
(b) and (d) are false. number of x and y, where y follows x.
2. (a) L = {xnyn |n ≥ 1}
25. (b) Given grammar will generate ambiguous gram- 30. (b) The C language is a context-sensitive language.
mar because for a single string there can be two 31. (d) Has at least 2 numbers of a’s
parse tree according to a given grammar.
32. (a) Only options (II) and (IV) are undecidable
because there exists no algorithm to check that a
26. (b)
CFL is regular and also for membership problem
(a) P
ower of NFA and DFA is same because there for RE language.
exists an algorithm to convert every NFA to 33. (d) Language L1 = {(a + b)*a} is a set of strings
DFA. possible with “a” and “b” and which ends with
(b) Recognition power of NPDA and DPDA are terminal “a”.
Language L2 = {b(a + b)*} is a set of all strings
not same because there is no algorithm to con-
vert NPDA to DPDA.
possible with “a” and “b” and which starts with
(c) Power of NTM and DTM is same because there
terminal “b”.
exists an algorithm to convert every NTM to
So their intersection will give set of strings
DTM.
which starts with “a” and ends with “b”, that is,
(d) Since for finite automaton and Turing machines
b(a + b)*a.
the above statements are true, so this option is
false. 34. (b) There is algorithm to reduce halting problem
of Turing machine to state entry problem. The
27. (c) Since recognition power of NPDA (non-deter- given problem X is a state-entry problem. As halt-
ministic PDA) and DPDA (deterministic PDA) ing problem of Turing machine is undecidable,
are not same because there is no algorithm to con- so problem X also must be undecidable. The lan-
vert NPDA to DPDA. Means there exists some guage corresponding to state-entry problem is RE
languages such as L = {wwR| w ∈ (a, b*} which are but not REC. So X is undecidable, but partially
accepted by NPDA but not by DPDA. decidable.
4
Number of questions
3
Marks 1
2 Marks 2
Total number of questions
0
2015 2014 2013 2012 2011 2010
Year Concepts
2015 Control flow graph, Compiler phases, Parsers, LL(1) and
LR(1) Grammar, Shift reducing parser
2014 Parsing, Static and dynamic memory allocation
2013 Parser, Languages
2012 Parsing, Grammar, Decidability
2011 Decidability, Languages
2010 Grammar, Compiler keywords
COMPILER DESIGN
Syllabus: Compiler design: Lexical analysis, Parsing, Syntax-directed translation, Runtime environments, Intermediate
and target code generation, Basics of code optimization.
6.2.3.1 Lexical Analyzer
High level language (HLL)
Lexical analyzer reads the source program character by
Compiler character at a time and unites them into a stream of tokens.
Token is a group of character which represents keywords,
operators and identifiers. Character sequence formed by
Input Low level language Run Output tokens is called “lexeme”. Lexical analyzer is also known
Example 6.1
6.2.3 Phases of a Compiler
Statement written in a source program is
A compiler takes a source program written in high-level A = B + C *25;
language as input and produces an equivalent set of Tokens generated by lexical analyzer
machine instruction as output. The compiler process is
A → Identifier
complex, so it is divided into six sub-processes which are
also known as phases of a compiler. The following are = → Assignment operator
different phases of a compiler (Fig. 6.3): B → Identifier
1. Lexical analyzer + → Add operator
2. Syntax analyzer C → Identifier
3. Semantic analyzer * → Multiplication operator
4. Intermediate code generator 25 → Constant
5. Code optimization
6. Target code
Input 6.2.3.2 Syntax Analyzer or Parser
Example 6.2 So, here in our example, implicit conversion took place.
Implicit-type casting is also known as coercion.
Parse tree of a given problem A = B + C *25.
6.2.3.4 Intermediate Code Generator
S
The intermediate code generator phase takes a tree as an
input produced by a semantic analyzer and produces an
(A)id = P
intermediate code. The intermediate code thus generated
has mainly two properties: it should be easy to produce
P + T and easy to translate into target program. An intermedi-
ate code can be represented in variety of forms. One of
the forms is the three-address form, which is very similar
T T ∗ Q to the assembly language in which every memory loca-
tion acts like a register. The intermediate code of our
Q Q 25 example is
Source code:
(B)id (C)id A = B + C * 25
T1 = C * 25
6.2.3.3 Semantic Analyzer T2 = B + T1
A = T2
When the parse tree is generated by a syntax analyzer
and passed as an input to the semantic analyzer, then
6.2.3.5 Code Optimization
the semantic analyzer computes the additional informa-
tion related to the recognized tokens, such as operator,
The code optimization phase is to reduce the size of the
operand, expression or statement, and inserts that infor-
code and improve the performance of the code gener-
mation into the symbol table. The information stored in
ated by an intermediate code phase. The most impor-
the symbol table is frequently used by the other phases
tant part of optimized code is to minimize the amount
of the compiler. During the semantic analysis, the type
of time taken by the code to execute and less common
of identifier is checked. In our example, let all identifiers
is to minimize the amount of memory used by the code.
be float, and 25 be treated as an integer constant. If
Optimized code of our example is
required, the semantic analyzer will perform an implicit-
type conversion, and if it is not possible, then it will Intermediate code: Optimized code:
throw an error. This can be easily understood by the T1 = C * 25 T1 = C * 25
example given in Fig. 6.4.
T2 = B + T1 A = B + T1
A = T2
Semantic analyzer
6.2.3.6 Target Code
S
The target code is the final phase of the compiler which
Float id = P Float normally converts the input obtained from the code opti-
mization phase into the target code (machine code or
Float P + T Float assembly code). The target code of our example is as
Type
mismatch, follows:
Float T T ∗ Q Integer implicit
Float conversion MOV R1, C
takes place
MUL R1, 25
Float Q Q Float 25 Integer MOV R2, B
ADD R2, R1
Float id id Float
STORE A, R2
Figure 6.4 | Semantic analyzer of A = B + C *25. This is the final output of the compiler.
5. Formal language: A set of all possible strings a lexical analyzer when called by the parser to get
which can be generated from given alphabets, and query for the next token. The positions are shown by
represented by Σ*. the circles called states which are connected by edges.
6. Regular expression: The rules that define Here is a finite automaton which recognizes an integer
the set of words that are valid tokens in a (Fig. 6.7).
formal language. These rules are made by three
operators:
••Alternation x|y (x or y) Digit
••Repetition x* (x is repeated 0 or more times)
••Concatenation xy Start Digit
Not Not
6.3.2.2 Finite Automata Review digit digit
Once we have all type of tokens defined by regular Figure 6.7 | Finite automata for recognizing an integer.
expression, we can create a finite automaton for recog-
nizing them. A finite automaton has the following:
1. A finite set of states, one of which is the start state Problem 6.1: Consider the following C program, find
or initial state, and some (maybe none) of which the number of tokens.
are final states.
float average(int a, int b)
2. An alphabet Σ of possible input symbols. {
3. A finite set of transitions that specifies for each float c;
state and for each symbol of the input alphabet, c = (a + b)/2;
which defines that for an input symbol which will return c;
be the next state to go. }
6.3.2.3 Recognition of Tokens
6.4 PARSER
In this section, we will explain how a token is rec-
ognized by a lexical analyzer. A lexical analyzer uses
finite automaton to recognize a token. Transition dia- A parser is a part of a compiler. It takes sequence of
gram is shown below which consists of stages and arcs. tokens from the lexical analyzer as an input and then
Arcs show the transition from one state to another builds a data structure in the form of a parse tree
state. Transition diagram describes the working of (Fig. 6.8). A parser’s main purpose is to determine if
the input data may be derived from the start symbol 6.4.2 Derivation Tree or Parse Tree
of the grammar. Depending upon the method how
the parse tree is derived, we have two types of parsers— The string generated by a CFG G = (Vn, T, S, P) is rep-
top-down parser and bottom-up parser (discussed resented by a hierarchical structure called tree. A deriva-
shortly). tion tree or parse tree for a CFG is a tree that satisfies
the following condition:
1. If A → a1, a2, a3, …, an is a production in G,
Grammar then A becomes the father of nodes, labelled a1, a2,
a3, …, an.
V→T
Source
program 2. The root has label S (starting symbol).
3. Every vertex (or node) has a label.
4. Internal nodes should be labels with variables only.
Parser Parse 5. The leaves nodes are labelled with e or terminal symbol.
tree 6. The collection of leaves from left to right yields the
string w.
Error
handler Problem 6.4: Consider the grammar S → S +
S |S *S |a|b. Construct a derivation (or parse) tree for
Figure 6.8 | Parser. the string w = a + b *b.
Solution:
6.4.1 Context-Free Grammar S
A grammar G = (Vn, T, S, P) is said to be a context-free
grammar (CFG) if the production P = {u → v} of G are S + S
of the form u → v and satisfy the following conditions:
1. u → v, where v ∈ (V ∪ T ) * , and V stands for vari-
a S * S
able and T for terminal
2. u → v, where u ∈ Vn
3. u ≤ v (length of u is less than v) b b
4. Only single variable is allowed in left side (means u
has single variable only)
As we know that a CFG has no context either left or right, 6.4.2.1 Leftmost Derivation Tree
this is the reason why it is also known as context-free.
A derivation tree is called a leftmost derivation (LMD)
tree if the ordering of decomposed variable is from left to
Problem 6.3: Consider a grammar G = (Vn, T, S, P) right. Thus, for generating string w = aab from grammar:
having production S → aSa|bSb|x. Check the produc-
tion and find the language generated. S → AB (production 1)
A → aaA (production 2)
Solution: A → e (production 3)
Let P1: S → aSa B → bB (production 4)
P2: S → bSb B → e (production 5)
P3: S → x LMD:
(a, b, x) are terminals and S is a variable. As all the pro- S → AB (by production 1)
duction are of the form A → a, where a ∈ (Vn ∪ S ) * S → aaAB (by production 2)
and A ∈ Vn, hence G is a CFG. And it will produce
S → aaB (by production 3)
context-free language.
S → aabB (by production 4)
Language generated: L(G) = {wxwR : w ∈ (a + b)*}
S → aab (by production 5)
A derivation tree is called rightmost derivation tree 1. Removal of left recursion: A production of
(RMD) if the ordering of decomposed variable is from grammar G = (Vn, T, S, P) is said to be left recur-
right to left. Thus, for generating string w = aab from sive grammar if it has one of the productions in the
the above grammar: given form:
RMD: A → Aa, where A is a variable and a ∈ (Vn ∪ S)*
S → AB (by production 1) Elimination of left recursion: Let the variable A
S → AbB (by production 4) have left recursive problem as following:
S → aaAb (by production 2) where b1, b2, …, bm do not begin with A. Then we
replace A production by:
S → aab (by production 3)
{A → b1A′|b2A′|b3A′|…|bnA′|}
where A′ → a1A′|a2A′|a3A′|…|anA′|e
S
Example 6.4
A B Let grammar S → S + S |S *S |a|b|c
To eliminate left recursion, the grammar S is replaced by
a a A b B S ′ → +SS ′|*SS ′|e
S → aS ′|bS ′|cS ′
e e
Left to right 2. Removal of left factoring: In grammar G, two
or more productions of variable A are said to have
left factoring if the productions are in the form:
6.4.3 Ambiguous Grammar A → ab1|ab2|ab3…|abm
A grammar G is called ambiguous if for some string where {b1|b2|b3…|bm} ∈ (Vn ∪ S)* and does not
w ∈ L(G), there exist two or more derivation tree (two start with a. All these production have common
or more LMD or two or more RMD tree). Let us consider left factor a.
a CFG grammar having production: Elimination of left factoring: Let variable A have
S → S + S |S *S |a|b, for string w = a + a *a have more (left factoring) production as follows:
than one LMD tree. A → ab1|ab2|ab3…|abm|γ1|γ2|γ3|…|γm
where γ1, γ2, γ3, …, γm and {b1, b2, b3…bm} do not
S
S contain a as a prefix, then we replace this produc-
tion by
S + S S * S A → aA′|γ1|γ2|γ3|…|γm
A′ → b1|b2|b3…|bm
S + S a
a S * S
Example 6.5
a a
a a Let grammar A → abc|abd|abe
To remove left factoring, we have
A → abA′
Note: A language (L) is called ambiguous if and only
if every grammar which generates it is ambiguous. The
only known ambiguous language is {anbmcn} ∪ {anbmcm}. A′ → c|d|e
LMD for constructing the parse tree. When a variable Problem with Recursive Descent Parser
contains more than one choice, choosing the correct
1. Recursion is used to generate parse tree.
production is always going to be difficult. In top-down
2. More time is wasted in backtracking.
parsing, no left recursion and no left factoring exist.
3. Recursive descent parser can enter into an infinite
loop if the given grammar contains left recursion.
Problem 6.5: String w = abbcde, grammar G is given 4. Time complexity of RDP is O(2n).
below: 6.4.4.2 Top-Down Parser without
S→aABe Backtracking
A→Abc/b
A predictive parser is a special class of recursive decent
B→D
parser. The goal of predictive parsing is to construct
Draw a parse tree for the production of a string w a top-down parser that does not require backtracking.
with the help of grammar G. Predictive parsing technique can only be used for class
of LL(k) grammars, where k is some integer. In LL(k)
Solution: grammar, by seeing k tokens a recursive decent parser
decides which production should be examined so that
S the LL(k) grammars are able to exclude all grammars
that are ambiguous and having left recursion (Fig. 6.9).
a A B e
Input
A b c d
LL() Parser Output
b $
Stack
Parse tree for string w = abbcde.
LL() Action LL() Goto
table table
Top-down parsing can be performed by the following Parsing table
One of the most straightforward forms of parsing is recur- 1. If (x = = a = = $), then successful complete.
sive descent parsing (RDP). This is a top-down process in 2. If (x = = a = = $), then pop the stack element and
which the parser attempts to verify that the syntax of the increment input pointer.
input stream is correct as it is read from left to right. The 3. If (x is a variable), then see the LL(1) action pars-
pseudocode of recursive descent parser is given as follows: ing table M.
If M[x1, a] = x → uvw, then replace x by uvw in the
RDP(S) reverse order.
{ 4. If M[x1, a] = blank, then parsing error.
Choose a production S →x1,x2,x3,x4
for(i=1 to n) Problem 6.6: Draw a top-down predictive parser
{ using the following parser table.
if(xi is variable)
RDP(i)
Grammar of given language
else if(xi==lookahead) S → (L)/a
increment i/p pointer
L → SL′
else
error(choose another production) L′ → e/,SL′
}
String w = (a, a, a)
}
Problem 6.7: Find the first and the follow sets for the
S
given grammar:
E → TE ′
C L J
E ′ → e/ + TE ′
T → FT ′
S L T ′ → e/*FT ′
F → id/(E)
,
Solution:
a S L
Variable First( ) Follow( )
a , S L′ E id, ( $, )
E ′ +, e $, )
a ¨ T id, ( +, $, )
T ′ *, e +, $, )
Parse tree for the string w = (a, a, a). F id, ( *, +, $, )
Problem 6.8: Find the first and the follow sets for the Number of cells in parsing table = {V *(T + 1)}
given grammar: So, table entries = {5*(5 + 1)} = 30
S → (L)/a Parsing table constructed with the help of the first
L → SL′ and follow sets of given grammar:
L′ → e/,SL′
Id + * ( ) $
Solution:
E E → TE ′ E → TE ′
Variable First( ) Follow( ) E ′ E ′ → +TE ′ E ′ → e E ′ → e
S (, a ,, ), $ T T → FT ′ T → FT ′
L (, a )
T ′ T ′ → e T ′ → *FT ′ T′ → e T ′ → e
L′ ,, e ) F F → id F → (E)
6.4.4.5 Algorithm for Constructing a How to Check a Given Grammar (G) Is LL(1) or Not?
Parsing Table
The following are two ways to identify that a given
If a grammar G is given, the steps to construct a parsing grammar is LL(1) or not:
table is as follows:
1. By constructing parsing table, if parsing table does
For each production A → a, do the following not have more than one production entry in any of
1. Add A → a under M[A, b], where b ∈ First(a). the cell then only it is LL(1).
2. If First(a) contains e, then add A → a under 2. By checking the following two conditions:
M[A, c], where c ∈ Follow(A). ••If a given grammar G does not contain null(e)
production:
Number of parsing table entries = {V *(T + 1)}, where Let grammar G be
V is the number of variables in the given grammar and T
is the number of terminals in the given grammar. A → a1/a2/a3
Then a1, a2, a3 should be pair wise disjoint.
That is,
Problem 6.9: Construct the parsing table from the
following grammar: First(a1) ∩ First(a2) = ∅
E → TE ′ First(a2) ∩ First(a3) = ∅
E′ → e/ + TE ′ First(a3) ∩ First(a1) = ∅
T → FT ′ ••If grammar G contains null(e) production:
T′ → e/*FT ′ Let grammar G be
F → id/(E) A → a1/a2/e
Solution: Find First(a1), First(a2) and Follow(A), they
The first and the follow sets for the given grammar should be pair wise disjoint.
are as follows:
First(a1) ∩ Follow(A) = ∅
Variable First( ) Follow( ) First(a2) ∩ Follow(A) = ∅
E id, ( $, )
E ′ +, e $, ) Problem 6.10: Check whether the following grammar
T id, ( +, $, ) is LL(1) or not.
T ′ *, e +, $, ) S → E/a
F id, ( *, +, $, )
E→a
Solution: In the given grammar there is no null pro- Draw a parse tree for reduction of a string w with the
duction, so help of the grammar G.
First(S) = a Solution: The parse tree for reduction of the string
w = abbcde.
First(E) = a
a A B e
Problem 6.11: Check the following grammar is LL(1)
or not.
a A d e
S → aABb
A → a/e
a A b c e
B → d/e
d
Important Points
LR Parser Operator precedence
1. Every regular grammar need not be LL(1) because Unambigious parser
that grammar may contain left factoring. grammar Operator grammar
2. Any ambiguous grammar cannot be LL(1).
3. If any grammar contains left factoring, then it
cannot be LL(1) grammar. LR(0) SLR(1) CLR(1) LALR(1)
4. If a given grammar contains left recursion, then it
cannot be LL(1) grammar. Figure 6.10 | Classification of bottom-up parser.
LR Parser
6.4.5 Bottom-Up Parser
In computer science, an LR parser is a type of bottom-
In bottom-up parser, parse tree construction starts from up parser that efficiently handles context-free languages in
children and proceeds to root. Bottom-up parser uses guaranteed linear time. An LR parser reads input from left
RMD for constructing the parse tree. A substring which to right and produces an RMD. The canonical LR (CLR),
will give reduction of string is called handle. look-ahead LR (LALR) and simple LR (SLR) parsers are
common variants of LR parsers. For all types of LR parsers,
The difficulty with bottom-up parser is identifying the
parsing algorithm is same but parsing tables are different.
right handle which will give one required variable so that
we will go to start symbol. LR Parsing Algorithm
Let S be the state on top of the stack and `a’ be the look-
Problem 6.12: String w = abbcde, grammar G is ahead symbol, then
given below:
1. If action [S, a] = Si, then shift `a’ and `i’, and also
S → aABe increment the i/p pointer.
A → Abc/b 2. If action [S, a] = rj and rj is a → b, then pop 2|b|
symbols and replace by a. If Sm−1 is the state below
B→D
a, then push Goto [Sm−1, a].
LR(0) Parser A → aA
A→b
1. LR(0) parsing table construction: For con-
structing LR(0) parsing table we have to follow the Step 2: Find closure.
given steps: I1
••Construct augmented grammar. S′ → S.
Let given grammar G be S
S → AA
I2
S′ → .S S → A.A I5
A → aA/b
A
S → .A
A A → aA S → AA.
Augmented grammar G′ will be A → .aA a A → .b
S ′ → S A → .b I3 a
A → a.A
b I6
S → AA A → aA
I0 aA
A →aA.
A → .b
A → aA/b
I4
S ′ → S is called the augmented production. A → b. b
b
••Find closure {I0 = closure (S ′ → S )}. ·
LR(0) item. a a
This can be understood clearly by the given exam- b b A
ple; it uses the above grammar to find closure. I3 I6
Closure (S ′ → S ) = {S ′ → S |S → AA|A → aA|
· · · ·
A → b} · I b
a
Closure (S → AA) = {S → AA|A → aA|A → b}
· · · ·
Important Points
Problem 6.14: Check whether the following grammar
is LR(0) or not. 1. If there are two reductions on any state of DFA,
then in table entry we have to put both in that cell.
S → (L) (production number 1)
In that situation parser will not be able to decide
S → a (production number 2) for which reduction it has to parse, so this problem
L → L, S (production number 3) is called reduce-reduce (R-R) problem.
L → S (production number 4) 2. If in any state one production is reduced and
another is shifted then also parser is not able to
Solution: parse it. This problem is called shift-reduce (S-R)
Step 1: Construct the augmented grammar. problem.
S ′ → S 3. Due to DFA no shift-shift (S-S) problem occurred.
4. For conflict, there should be two productions and
S → (L) at least one of them should be reduced.
S→a 5. Goto section does not participate in conflict.
L → L, S
L→S
Step 2: Find closure (I0 = S′ → S). Problem 6.15: Check if the following grammar is
I1 LR(0) or not.
S S→ S.
I0
E → T + E …….(production number 1)
I4 I6
I2 )
S′→ .S L S→ (L.) S→ (L).
S→ .(L) ( S→ (.L)
L→ .L,S
L→ L.,S E → T …….........(production number 2)
S→ .a I7 I8
L → .S
(
T → id ……........(production number 3)
,
L→ L,.S
I 5
a S→ .(L) S L→ L,S.
L → S. S→ .(L)
I3 S→ .a S→ .a
S→ a. a
( Solution:
a Step 1: Construct an augmented grammar.
Step 3: Construct DFA. E′ → E
I1 E→T+E
I6
E→T
S )
T → id
I
I0 ( L ,
I2 ( I7
Step 2: Find closure.
a S I
a S I1
I3 a I8 I0 E E′ → E.
I4
E′ → .E I2
E → T+.E I5
E → .T+E E → T.+E → .T+E E → T+E.
T + E E
Step 4: Construct LR parsing table.
E → .T E → T. E → .T
T → .id
T T → .id
Action Goto I7
id
State A ( ) , $ S L T → id. id
I0 S3 S2 1
I1 Accepted Step 3: Construct DFA.
I2 S3 S2 5 4
I3 r2 r2 r2 r2 r2 I1
I4 S6 S7 E
I5 r4 r4 r4 r4 r4
I6 r1 r1 r1 r1 r1 T + E
I0 I2 I4 I5
I7 S3 S2 8 T
I8 r3 r3 r3 r3 r3
id
The given grammar is LR(0) because there are no I3 id
multiple entries in the same cell.
SLR(1) parser is one of the variants of LR parser. An SLR Step 1: Construct an augmented grammar.
parser is efficient at finding the single correct bottom- S′ → S
up parser in a single scan without backtracking. SLR(1) S → AA
parser table has only one difference from LR(0) pars- A → aA
ing table, reduction entries are made only in the specific
location. Let production E → T be reduced, then entry A→b
of the reduced production will be in those column which Step 2: Find closure.
comes in Follow(T). If there are multiple entries in the I1
S′→ S. ,$
I5
same cell, then the given grammar will not be SLR(1).
S S′→ AA.,$
From Problem 6.15, we will construct parse table I2 A
I1
I7
only for SLR(1) (Table 6.1). State 2 has shift and reduce A S′→ A. A,$ b
S′→ S,$ A→ a.A,$ A→ b. ,$
entry, but we have to find that where we have to put the S→ .AA,$ A→ .b,$
reduce entry. The production which is reduced is E → T, a b
A→ .aA,a/b, I6 I9
so we will put r2 at places which come in Follow(T). A→ .b,a/b, a
I3
A→ a.A,$
A A→ aA.,$
A→ a.A,a/b A→ .aA,$ a
Follow (T) = {+, $} b A→ .aA,a/b
a
A→ .b,$
A→ .b, a/b A
Table 6.1 | Parsing table for SLR(1)
I8
I4 b A→ aA.,a/b
Action Goto A′→ b., a/b
+ Id $ E T
Step 3: Construct DFA.
I0 S3 1 2
I1 Accepted I1
I2 S4/r2 r2 S I5
A
I3 r3 r3 r3 a
I4 S3 5 2 A a A
I2 I6 I9
I5 r1 r1 r1 I0
a b b
I7
There are two entries in the same cell, so this is not b I3 A
SLR(1) grammar. b I8
Canonical LR Parser I4
Canonical LR parser is a simplified version of an LR parser
and is also known as CLR parser. CLR parser is the most Step 4: Construct parsing table.
powerful parser of the LR parser family. Closure and Goto
function are processed differently than in SLR parser. Action Goto
1. Closure function of CLR: State A B $ S A
Closure(I) I0 S3 S4 1 2
••If I is A → B CD, $ and C → EF is in G, then
· ·
I1 Accepted
add C → EF, {First(D), $}.
·
(Continued)
Solution: Operator
precedence
(a) Operator grammar parser
Figure 6.12 | Relation between CLR(1), LALR(1),
(b) Not operator grammar
(c) Not operator grammar
SLR(1), LR(0) and operator precedence parser.
An SDT is basically divided into two types: S-attribute Parse tree for given arithmetic expression
definition and L-attribute definition (Table 6.2). 2 + 3*4.
Problem 6.20: Consider the following SDT: Problem 6.22: Construct an SDT to convert infix
S → TR
expression to prefix expression.
R→e
Output: + a*b c
For input string (9 + 5 + 2), what will be the output? The SDT is constructed as follows:
E → {print (+)} E + T
Solution:
E→T
We first draw the parse tree and then determine the
output. T → {print (*)} T*F
S T→F
F → id {print (id)}
T R
Num(9) +
6.6 RUNTIME ENVIRONMENT
T Print(+) R
Problem 6.21: Construct an SDT to convert infix Runtime memory needs to be subdivided as follows to
expression to postfix expression. hold the different components of an executing program
(Fig. 6.13):
Input: a + b*c
Output: abc* +
Program code
Solution:
Static global data
The SDT is constructed as follows:
E → E + T {print (+)} Stack
E→T
T → T *F {print (*)}
T→F
F → id {print (id)}
E
Heap
There are two different approaches for runtime storage Solution: The activation takes place as follows:
allocation, as given in Table 6.3.
System starts main
Table 6.3 | Static and dynamic allocation enter f(5)
Static Allocation Dynamic Allocation enter f(4)
enter f(3)
Allocates all needed space Allocates space when it is enter f(2)
when program starts. needed. exit f(2)
Deallocates all space Deallocates space when it enter f(1)
when program terminates. is no longer needed. exit f(1)
exit f(3)
enter f(2)
6.6.2 Activation Record and Activation Trees exit f(2)
exit f(4)
enter f(3)
Activation is the function in execution mode. The func-
enter f(2)
tion code is the static part whereas execution is the exit f(2)
counterpart. The storage associated with an activation enter f(1)
of a procedure is called activation record. exit f(1)
1. Activation record content: exit f(3)
exit f(5)
••Temporary values: Values generated as a result
of the expression evaluations which cannot be The activation tree for f (5) is as follows:
put in registers.
••Local data: Local data to belong to the procedure.
main ( )
••Saved machine: Keeps the context or machine
f(5)
status (register, PC, etc.).
••Access link: Points to non-local data in other AR.
••Control link: Points to the caller’s activation f(4) f(3)
record the return value space of the called func-
tion, if any.
••Actual parameters: Used by the calling proce- f(3) f(2) f(2) f(1)
dure to pass parameters to called procedures;
registers are used to pass these information. f(1) f(1)
For a recursive procedure or function, several activations Activation tree of f (5).
may be alive simultaneously. Activation tree shows the
path through which control enters and leaves activa- The following observations are made based upon the
tions for single run of a program. Each node represents activation tree:
an activation of a function, if an arrow is facing a child
node from the parent node that means the child func- 1. Order of activation corresponds to the pre-order
tion is called by the parent function. Sibling `a’ is left traversal of the tree.
to `b’ means that function `a’ is called before function b. 2. Order of deactivation corresponds to the post-order
traversal of the tree.
3. If an activation of p calls q, then p will not termi-
nate before q.
Problem 6.23: Draw an activation tree of the given
example.
6.6.3 Procedure Call Return Model
int a[10];
int main(){ Every machine’s architecture and every language are
int i; slightly different from each other. The basic steps fol-
for (i=0; i<10; i++){ lowed by a function call are as follows:
a[i] = f(i);
} 1. Before a function call, the calling routine:
} ••Saves any necessary registers
int f (int n) { ••Pushes the arguments onto the stack for the tar-
if (n<3) return 1; get call
return f(n-1)+f(n-2); ••Sets up the static link (if appropriate)
main ends ••Pushes the return address onto the stack
} ••Jumps to the target
2. During a function call, the target routine: Symbol table can be implemented by:
••Saves any necessary registers
1. Ordered list
••Sets up the new frame pointer
2. Unordered list
••Makes space for any local variables
3. Hash table
••Does its work
4. Tree
••Tears down frame pointer and static link
••Restores any saved registers Possible entries in a symbol table are as follows:
••Jumps to saved return address 1. Name
3. After a function call, the calling routine: 2. Data type
••Removes return address and parameters from 3. Size
the stack 4. ID
••Restores any saved registers 5. Scope information
••Continues executing 6. Storage allocation
Intermediate
Parser
It is a data structure used by compiler to store all the 6.7.1 Intermediate Representations
information of tokens generated by lexical analyzer. After
the lexical analysis phase, semantic analyzer performs The commonly used representations of intermediate
type checking on the input code. During type check- code are as follows:
ing, semantic analyzer checks whether the use of names 1. Syntax tree
(such as type names, variables, functions) is consistent 2. Postfix notation
with their definition in the source code. Consequently, 3. Three-address code
if there are any inconsistencies or misuses found during
type checking then it is shown as an error. This is the
task of a symbol table. 6.7.1.1 Syntax Tree
Operations that can be performed on a symbol table A syntax tree is the tree representation of the abstract
are as follows: syntactic structure of source code. Due to its abstract
1. Insert nature it is also called as abstract syntax tree. Each of
2. Delete the tree node denotes a construct occurring in source
3. Search code.
a +
Example 6.10
The quadruples for the assignment a = b*c + d *e gener-
ates the following three-address code:
* *
Op arg1 arg2 Result
(0) = C t1
b c d c (1) * B t1 t2
(2) = D t3
Syntax tree of a = b*c + d*c. (3) * E t3 t4
(4) + t2 t4 t5
(5) = t5 A
6.7.1.2 Postfix Notation
Postfix notation is also known as `reverse polish’ nota- 2. Triples: Temporary variables or memory names
tion. Any expression can be written unambiguously. in the symbol table can be avoided by the position
Interpreters can be built easily for postfix notation by number of the statement that computes it. If we
using the stack data structure. In the postfix notation, used this method then three address statements can
an operator follows the operand. be represented by records with three columns oper-
ation, arg1 and arg2. The column arg1 and arg2,
for the arguments of operation, are either point-
Example 6.9 ers to the symbol table or pointers into the triple
structure. As numbers of used fields are three, so
The source language statement: this format is known as triples.
a = b*c + d*c
Can be rewritten in postfix notation as: Example 6.11
abc*dc* + =
Operation Arg1 Arg2
(0) = c
(1) * b (0)
6.7.1.3 Three-Address Statement
(2) = c
Three-address statement is a linearized representation (3) * b (2)
of an abstract syntax, in which names of the temporar-
(4) + (1) (3)
ies correspond to the nodes. The intermediate values
name allows three-address code to be easily rearranged, (5) = a (4)
which is convenient for optimization technique. The
reason to call `three-address code’ is that each state-
ment generally contains three addresses, two for the 3. Indirect triples: Triples are very difficult to opti-
operands and one for the result. Representations of the mize because for optimization it required moving
three-address statement are quadruples, triples and of intermediate code and other triples connected
indirect triples. to it also have to be updated. So, a new variant of
triples is called indirect triples is being used which 6.8.2 Primary Source of Optimization
is easier to optimized. Indirect triples perform list-
ing pointers to triples, rather than listing the tri- Optimization can be done by using different ways.
ples themselves. Some of the primary source of optimization is discussed
below:
Example 6.12
6.8.2.1 Dead-Code Elimination
# Stmt # Op Arg1 Arg2
Dead-code is a section of program code which is executed
(0) (14) → (14) C
but the result produced by that section is never used.
→
(1) (15) (15) * B (14) So, it can be removed from the program code because it
does not have any effect on the functionality of program.
(2) (16) → (16) C
Let we have temp1 = temp2 − temp3, and temp1 is
→
(3) (17) (17) * B (16) never used further in the program then we can eliminate
this whole instruction.
(4) (18) → (18) + (15) (17)
(5) (19) → (19) = A (18) 6.8.2.2 Constant Propagation
Program Triple container Constant propagation is the process of substituting a
constant value by the subsequent uses of a variable while
there is no intervening changes in the value of that vari-
able. Consider the given example below:
6.8 CODE OPTIMIZATION
int a = 10;
int b = 12 – a / 2;
return b = b * (35/a +2);
Code optimization phase is to reduce the size of the
code and improve the performance of the code gener- Variable `a' propagating the constant value 10 in the
ated by intermediate code phase. The most important given code.
part of optimized code is to minimize the amount of
time taken by the code to execute and less common is 6.8.2.3 Constant Folding
to minimize the amount of memory used by the code.
The basic requirement optimization methods should Constant folding is the process of evaluating the
comply with is that an optimized program must have constant expression at compile time. Constant fold-
the same output and side effects as its non-optimized ing process improves run-time performance and also
version. reduces code size by evaluating constant at compile
time. In program code below, the expression (4−2) can
be evaluated at compile time and replaced with the
6.8.1 Types and Levels of Optimization constant 2.
IMPORTANT FORMULAS
1. Time complexity of RDP is O(2n). 7. Every LL(1) grammar will surely be LALR(1), but
if any grammar is LALR(1) then it may or may not
2. Every regular grammar need not be LL(1), because
be LL(1).
that grammar may contain left factoring.
8. If any grammar is LALR(1), then it will surely be
3. Any ambiguous grammar cannot be LL(1).
CLR(1).
4. If any grammar contains left factoring, then it
9. CLR(1) is also known as LR(1).
can’t be LL(1) grammar.
10. Every LL(1) grammar will surely be CLR(1).
5. If given grammar contains left recursion, then it
can’t be LL(1) grammar. 11. All LL(k) parsers are subset of LR(k) parser.
6. If grammar G is LL(1), then 12. If the number of states in LR(0), SLR(1), CLR(1)
•• It may be LR(0). and LALR(1) is n1, n2, n3, n4, respectively, then
•• It may be SLR(1). relation between them is
•• It will surely be LALR(1). n 1 = n2 = n4 ≤ n3
SOLVED EXAMPLES
1. Which of the following derivations does a top-down Solution: The tokens in C tokens include identi-
parser use while parsing an input string? The input fiers, keywords, constants, operators, string literals
is assumed to be scanned in left to right order. and other separators. Thus, there are 10 tokens in
the given printf statement.
(a) Leftmost derivation
Ans. (c)
(b) Topmost derivation
(c) Rightmost derivation 6. The identifications of common sub-expression and
(d) Leftmost derivation traced out in reverse replacement of run-time computations by compile-
time computations is
Solution: Top—Down parser uses leftmost deriva-
tion for constructing parse tree. (a) local optimization
Ans. (a) (b) global optimization
(c) constant folding
2. The process of assigning load addresses to vari-
(d) common fielding
ous parts of the program and adjusting the code
and data in the program to reflect the assigned Solution: Constant folding is the process of evalu-
addresses is called ating the constant expression at compile time.
Ans. (c)
(a) address assembly (b) parsing
(c) relocation (d) indexing 7. In a compiler the module that checks every charac-
ter of the source text is called
Solution: The process adjusting the code and data
in the program to reflect the assigned addresses is (a) the machine dependant optimizer
called relocation. (b) the code checker
Ans. (c) (c) the lexical analyzer
(d) the syntax analyzer
3. Which of the following statements is true?
Solution: Lexical analyzer reads the source program
(a) An unambiguous grammar need not have same
character by character at a time and unites them
leftmost and rightmost derivation.
into a stream of tokens.
(b) An LL(1) parser is not a top-down parser.
Ans. (c)
(c) LALR is less powerful than SLR.
(d) An ambiguous grammar can be LR(k) for any k. 8. Match the following.
Solution: Only option (a) is true. LL(1) parser is Group 1 Group 2
a top—down parser; LALR is more powerful.
A. Lexical analysis P. Directed acyclic graphs
Ans. (a)
B. Code optimization Q. Syntax trees
4. Given the following expression grammar: C. Code generation R. PDA
E → E *F |F + E |F
D. Abelian groups S. Finite automaton
Solution: Begin and Goto cannot be properly (a) i only (b) i and ii
grouped as keywords without considering the (c) i and iii (d) i and iv
delimiter. But <> can be considered as Boolean
Solution: Operator grammar has two properties:
operator, not matter what follows the symbol.
First, there should not be any null production. Second,
Ans. (c)
there should not be any production which has two con-
10. Let us suppose, in a programming language an secutive variables in the right-hand side of production.
identifier is permitted to be a letter followed Ans. (d)
by any number of letters or digits. If L and D 13. Consider the following grammar:
denote the sets of letters and digits, respec-
S → (L)/a
tively, which of the following expressions defines
L → SL′
an identifier?
L′ → e/+SL′
(a) (L.D)* (b) L(L + D) In the predictive parser table M of the grammar,
(c) L + (L.D)* (d) L(L.D)* the entries M[S, ( ] and M[L′,)], respectively,
Solution: An identifier expression cannot have (a) S → (L) and L′ →,SL′
character such as *. (b) S → a and L′ → e
Ans. (b) (c) S → (L) and L′ → e
(d) S → a and L′ →,SL′
11. Consider LR(1) collections of grammar (G), in
state I 0 (i.e. first state) what is the follow set of Solution:
LR(1) item, T → .TF? Parsing table
(a) $| + (b) $| + |a|b A , ( ) $
(c) $| + |a (d) + |a|b
S S→a S → (L)
Solution: L L → SL′ L → SL′
I0: L′ L′ →,SL′ L′ → e
E′→ .E, $
E → .E + T, $| +
Ans. (c)
E → .T, $| + 14. Which is equivalent CFG without useless symbol
T → .TF, $| + |a|b for the given grammar:
T → .F, $| + |a|b S → PQ|p, P → p
F → .F *, $| + |a|b|*
F → .a, $| + |a|b|* (a) S → PQ (b) S → p
F → .b, $| + |a|b|* (c) S → P, P → p (d) S → P|p, P → p
Ans. (b) Solution: P and Q are the useless symbol because
12. Which grammar rules violate the requirement of Q does not produce anything and P produces p
the operator grammar? A, B, C are variables and terminal through P → p, which can directly be
a, b, c are terminals. generated by the starting symbol. So, option (b)
is correct, because the given grammar generates
i. A → BC ii. A → CcBb terminal p only, which can be generated by S → p.
iii. A → BaC iv. A → e Ans. (b)
1. Which of the following suffices to convert an arbi- 2. Assume that the SLR parser for a grammar G has
trary CFG to an LL(1) grammar? n1 states and the LALR parser for G has n2 states.
The relationship between nl and n2 is
(a) Removing left recursion alone
(b) Factoring the grammar alone (a) n1 is necessarily less than n2.
(c) Removing left recursion and factoring the grammar (b) n1 is necessarily equal to n2.
(d) None of the above (c) n1 is necessarily greater than n2.
(GATE 2003: 1 Mark) (d) None of the above.
(GATE 2003: 1 Mark)
Solution: To convert a CFG to an LL(1) grammar,
left recursion and left factoring must be removed. Solution: States for SLR parser and LALR parser
Otherwise, the grammar becomes ambiguous. are equal.
Ans. (c) Ans. (b)
Here, gen is a function that generates the output operation requires the first operand to be in the
code, and newtemp is a function that returns the register. If E1 and E2 do not have any common sub-
name of a new temporary variable on every call. expression, in order to get the shortest possible code
Assume that ti’s are the temporary variable names
(a) E1 should be evaluated first.
generated by newtemp. For the statement `X: =
(b) E2 should be evaluated first.
Y + Z’, the 3-address code sequence generated by
(c) evaluation of E1 and E2 should necessarily be
this definition is
interleaved.
(a) X = Y + Z (d) order to evaluation of E1 and E2 is of no
(b) t1 = Y + Z; X = t1 consequence.
(c) t1 = Y; t2 = t1 + Z; X = t2 (GATE 2004: 1 Mark)
(d) t1 = Y; t2 = Z; t3 = t1 + t2; X = t3
Solution: If we evaluate E2 first then we can save
(GATE 2003: 2 Marks) this to memory location and evaluation of E1 is
kept in registers only. But if E1 is solved first, it
Solution: Three-address code for X = Y + Z is should be saved to memory. Then evaluate E2 and
t1 = Y save to memory. Both need to be fetched again. So,
t2 = Z option (b) is the optimized answer.
t3 = t1 + t2 Ans. (b)
X = t3 12. Consider the grammar with the following transla-
Ans. (d) tion rules and E as the start symbol.
9. Consider a program P that consists of two source E → E1 #T {E.value = E1.value * T.value}
modules M1 and M2 contained in two different files. |T {E.value = T.value}
If M1 contains a reference to a function defined in T → T1 & F {T.value = T1.value + F.value}
M2, the reference will be resolved at |F {T.value = F.value}
F → num {F.value = num.value}
(a) edit time. (b) compile time.
(c) link time. (d) load time. Compute E.value for the root of the parse tree for
the expression: 2 # 3 & 5 # 6 & 4.
(GATE 2004: 1 Mark)
(a) 200 (b) 180
Solution: Modules can be compiled separately, but (c) 160 (d) 40
they are linked to resolve references at link time. (GATE 2004: 2 Marks)
Ans. (c)
Solution: The parse tree is
10. Which of the following grammar rules violate the E = 160
requirements of an operator grammar? P, Q, R are
non-terminals, and r, s, t are terminals. # T1 = 10
E1 = 16
(i) P → QR
(ii) P → QsR E2 = 2 T2 = 8 T5 = 6 & F = 4
(iii) P → e #
(iv) P → QtRr T3 = 2 T4 = 3
&
T2 = 5 F=6
in the sentential form where the right-hand implemented with a stack-based storage alloca-
side of the production may be found. tion scheme for activation records.
(GATE 2008: 1 Mark) (a) II and V only (b) I, III and IV only
(c) I, II and V only (d) II, III and V only
Solution: Fact (GATE 2008: 2 Marks)
Ans. (d)
Solution:
25. Some code optimizations are carried out on the
intermediate code because I: Multi-level access link (or display) arrangement
is needed to arrange activation records only if the
(a) They enhance the portability of the compiler to programming language being implemented has
other target processors. nesting of procedures/functions is true.
(b) Program analysis is more accurate on interme- V: Programming languages which permit a func-
diate code than on machine code. tion to return a function as its result cannot be
(c) The information from dataflow analysis cannot implemented with a stack-based storage allocation
otherwise be used for optimization. scheme for activation records is true.
(d) The information from the front end cannot oth- Ans. (a)
erwise be used for optimization.
28. Match all items in Group 1 with correct options
(GATE 2008: 1 Mark) from those given in Group 2.
I. A programming language which does not A. There exist parsing algorithms for some pro-
permit global variables of any kind and has no gramming languages whose complexities are less
nesting of procedures/functions, but permits than Q(n3).
recursion can be implemented with static stor- B. A programming language which allows recur-
age allocation. sion can be implemented with static storage
II. Multi-level access link (or display) arrangement allocation.
is needed to arrange activation records only if C. No L-attributed definition can be evaluated in
the programming language being implemented the framework of bottom-up parsing.
has nesting of procedures/functions. D. Code improving transformations can be per-
III. R ecursion in programming languages cannot be formed at both source language and intermedi-
implemented with dynamic storage allocation. ate code level.
IV. Nesting procedures/functions and recursion (a) A and B (b) A and D
require a dynamic heap allocation scheme and (c) C and D (d) A, C and D
cannot be implemented with a stack-based (GATE 2009: 2 Marks)
allocation scheme for activation records.
V. Programming languages which permit a func- Solution: Statements A and D are true.
tion to return a function as its result cannot be Ans. (b)
PRACTICE EXERCISES
Set 1
22. Which one of the following is FALSE? 27. The compiler type that converts all operands to
the type of the largest operand is called
(a) A simple basic block enters the sequence at the
beginning and exits at the end. (a) Type promotion (b) Type evaluation
(b) Expression analysis can be used for common (c) Type bootstrap (d) Type operand
sub-expression elimination. testing
(c) Dead-code elimination can be done by live vari-
able analysis. 28. High-level knowledge which relates to the use of
(d) x = 4 ∗ 5 ⇒ x = 20 is an example of common sentences in different contexts and how the context
sub-expression elimination. affect the meaning of the sentences is called
23. One of the purposes of using intermediate code in (a) Pedagogy (b) Syntactic
compilers is to (c) Morphological (d) Pragmatic
(a) Improve memory and stack management. 29. The process adjusting the code and data in the
(b) Improve detection of syntax and semantic program to reflect the assigned addresses is
errors. called ________.
(c) Improve the reusing of the machine-independent (a) Assembly (b) Linking
code optimizer. (c) Absolute addressing (d) Relocation
(d) Improve the register allocation and cache
management. 30. A top-down parser while parsing an input string
from left to right makes use of:
24. Which of the following statement(s) is/are true
about LALR(1) parsers? (a) Leftmost derivation
(b) Topmost derivation
S1: LALR(1) parsers have same number of states
(c) Rightmost derivation traced out in reverse
as SLR(1) parsers (core LR(0) items are the same).
(d) Bottom-up derivation traced in reverse
S2: LALR(1) is derived from LR(1) with no shift-
reduce conflict will also have no shift-reduce 31. Which of the following statement(s) is/are true?
conflict.
S3: LALR(1) may create reduce-reduce conflict S1: If G is unambiguous, then every right-sentential
which was not in LR(1) from which LALR(1) is form has a unique handle.
derived. S2: Shift-reduce parsers use a stack and input
buffer.
(a) None of the statement is true
(b) S1 and S3 (a) Neither S1 nor S2 (b) S1 only
(c) S1 and S2 (c) Both S1 and S2 (d) S2 only
(d) S1, S2 and S3
32. Which of the following is/are type(s) of tools used
25. Which of the following suffices to convert an arbi- in various phases of compiler?
trary CFG to an LL(1) grammar? (a) Control flow graph, binary sort, cyclic graphs
(a) Removing left recursion alone and right asso- (b) Three-address code, bootstrap loader, cyclic
ciative productions graphs
(b) Removing the grammar and the productions (c) Directed acyclic graph, assembler, binary linker
but not the start symbol (d) Control flow graph, three-address code, directed
(c) Factoring the grammar and removing the left acyclic graph
associative productions
33. Which of the following is true?
(d) None of the above
(a) If a grammar is LL(k), it is also ~(LR(k − 1)).
26. The major limitation of a shift reduce parser is to (b) If a grammar is not LR(k), it is ~(SLR(k)).
overcome (c) If a grammar is CLR(k), it is also SLR(k).
(a) Representation of productions in top-down (d) If a grammar is not LALR(k), it is not SLR(k).
parser
34. Consider the following grammar:
(b) Reduce-reduce conflict only
(c) Both shift reduce conflict and reduce-reduce E → AB
conflict A → +BA/e
(d) Shift conflicts only B → B * A/id
A → AA|a|e
45. Consider the following grammar:
String `a aaa’ has
S → aB|aAb
A → bAb|a
(a) A unique derivation tree in the grammar
B → aB|e
(b) Four leftmost derivations in the grammar
(c) Four rightmost derivations in the grammar
(d) An infinite number of leftmost and rightmost How many back tracks are required to generate the
derivations string `aab’ from the above grammar?
40. Consider the grammar R: (a) Nil (b) 2 (c) 1 (d) 3
E → E + E |E *E|(E)|id 46. Which of the following statements are TRUE?
Consider the other grammar S: (a) T here does not exist any parsing algorithm
E → E + T |T which can parse in parallel.
T → T *F |F (b) A programming language which does not use
F(E)|id loops makes use of static storage allocation.
(c) No L-attributed definition can be evaluated in
Choose the incorrect statement.
the framework of bottom-up parsing.
(a) S generates the same language as R. (d) Code improving transformations can be per-
(b) R is ambiguous but not LL(1) as it is left formed at both the source language as well as
recursive. the intermediate code level.
49. Which statement is false about parsers? 5. The grammar E → EE, E → (E), E → ( ), E → t is
(a) Top-down parsing algorithms use a leftmost (a) Not LL(1) as it is left recursive
derivation. (b) Not LL(1) as it is ambiguous
(b) Bottom-up parsing algorithms use the reverse (c) Not LR(1) as it is left recursive
of a rightmost derivation. (d) None of the above
(c) An LR(1) parser may sometimes exist for some 6. Which one of the following string can definitely be said
ambiguous grammars. to be a token without looking at the next input char?
(d) An LL(1) parser is a top-down parser.
(a) < (b) > (c) + (d) *
50. Consider the following grammar:
7. Consider the grammar:
L → eLsL|sLeL|e
E → TE ′
Choose the correct statement: E ′ → e/+TE ′
(a) The string `eses’ has two parse trees T → FT ′
(b) The grammar is unambiguous. T ′ → e/*FT ′
(c) The grammar is LL(1) and LR(1). F → a/[E]
(d) None of the above. 7a. Find out the First( ) of variable T ?
9. Assume that the LALR parser for a grammar G1 Which of the following strings is generated by the
has n1 states and the CLR parser for G2 has n2 grammar?
states. The relationship between n1 and n2 is
(a) aabbab (b) abbbab
(a) n1 > n2 (b) n1 ≥ n2 (c) bbaaa (d) aabbb
(c) n1 < n2 (d) n1 ≤ n2
14. For the correct answer string to the above question, how
10. Which of the following grammar (G) is an operator many derivation trees are possible?
grammar? (a) 1 (b) 2
(a) {S → SS|Sa|Sb|a} (c) 3 (d) 4
(b) {S → AB|e, A → a, B → b}
(c) {S → aA, A → a|b|e}
15. To manage and store information about variables
(d) {S → AaS|b, A → a}
and their attributes, compiler uses
(a) Parse table (b) Semantic stack
11. Consider the following grammar (G): (c) Symbol table (d) Tree
E → E + T |T
T → TF |F
16. Consider the grammar
F → F *|a|b S → AbBaCc|e
A → aAb|ba
B → bBc|cb
The above given grammar is
(a) Not LR(0) due to S-R problem C → cCa|ac
(b) Not LR(0) due to R-R problem
(c) LR(0) 16a. Find the First( ) of S ?
(d) None of the above (a) {e} (b) {a, e}
(c) {a, b, e} (d) {a, b, c, e}
12. Consider the above grammar. If a grammar is
LL(1) then 16b. Find the Follow( ) of B?
(a) It will be LR(0). (b) It will be SLR(1). (a) {a} (b) {b}
(c) It will be LALR(1). (d) None of the above. (c) {a, b} (d) {a, c}
13. Consider the CFG given below with {S, X, Y} 16c. Consider the grammar (G) = {A → A + A|a|e},
non-terminal and {a, b} terminals. S is the starting which one is the correct after removing left recursion?
(a) {A′ → AA′, A′ → |a|e}
symbol and set of production rules are
S → bX |aB (b) {A′ → A + A′|e, A′ → |a}
A → a|aS |bAA (c) {A′ → A + A′|a|e}
B → b|bS |aBB (d) {A′ → +AA′|e, A → aA′}
Set 1
1. (a) 10. (b) 19. (d) 28. (c) 37. (c) 46. (c)
2. (c) 11. (d) 20. (c) 29. (d) 38. (b) 47. (b)
3. (a) 12. (d) 21. (a) 30. (a) 39. (b) 48. (a)
4. (c) 13. (a) 22. (d) 31. (c) 40. (d) 49. (c)
5. (d) 14. (a) 23. (d) 32. (a) 41. (a) 50. (c)
6. (b) 15. (b) 24. (a) 33. (b) 42. (d)
7. (c) 16. (d) 25. (c) 34. (d) 43. (a)
8. (d) 17. (b) 26. (d) 35. (b) 44. (12338)
9. (b) 18. (d) 27. (d) 36. (b) 45. (a)
Set 2
6
Number of questions
4 Marks 1
Marks 2
3
Total number of questions
2
0
2015 2014 2013 2012 2011 2010
Year Concept
2015 Shared variable, SCAN algorithm, Page replacement, Processes, Process synchronization,
Deadlock, Process scheduling, TLB, Best fit algorithm, Page table
2014 Page Frames, Deadlock, Translation Lookaside Buffer (TLB), Disk Scheduling Algorithm
2013 Scheduling, Disk Management, Semaphores, Concurrent Processes, Memory Management
2012 System Calls (Fork), Scheduling Algorithms, Concurrent Processes
2011 DMA Concept, Interrupts, Lightweight Processes, User–Kernel Mode, Scheduling Algorithms
2010 Deadlock, Critical Section, Page table, Concurrent Process Concept
OPERATING SYSTEM
Syllabus: Operating system: Processes, Threads, Inter-process communication, Concurrency, Synchronization, Deadlock,
CPU scheduling, Memory management and virtual memory, File systems, I/O systems, Protection and security
System Call is a request made by a user program to 7.2.2 Multiprogramming Operating System
get the service of an operating system.
To consider the “inside view” of the system, the OS is A single user cannot keep either the CPU or the I/O
written as a collection of procedures, each of which can devices busy at all times. Multiprogramming increases
call any other, whenever required. Each parameter has a CPU utilization by organizing jobs so that the CPU is
well-defined interface with input parameters and output not idle at any time (Fig. 7.4). Jobs to be executed are
results. A simplified layered structure of Dijkstra’s maintained in the memory simultaneously and the OS
“THE” operating system is shown in Fig. 7.2: switches among these jobs for their execution. When
one job waits for some input (WAIT state), the CPU
switches to another job. This process is followed for all
LAYER FUNCTION jobs in memory. When the wait for the first job is over,
the CPU is back to serve it. The CPU will be busy till all
5 The operator (user) the jobs have been executed. Thus, increasing the CPU
utilization and throughput.
4 User programs
3 Input/output management
Job 2 Job 1
2 Operator-process communication
CPU
1 Memory management Job 4 Job 3 JOB 3
Figure 7.3 | A batch operating system. Figure 7.5 | A multiprocessor operating system.
Table 7.1 | Comparison between Batch Processing processor is called a process. A process has various attri-
and Timesharing OS butes, states and operation.
Batch Timesharing
Processing 7.3.1.1 Attributes of a Process
AIM Maximize Minimize A process is distinct from another based upon the follow-
processor response time ing attributes:
utilization
1. Process id: It is a unique identification number
COMMANDS Job Control As per the which is assigned by the OS at the time of process
Language (JCL) commands creation.
provided along provided at 2. Process state: It contains the state of the process
with the job the terminal where the process is currently residing. The state
may be new, ready, running, waiting, halted and
7.2.5 Real-Time Operating System so on.
3. Program counter: It is having the address of the
A real-time operating system (RTOS) is the one which next instruction to be executed.
deals with real-time application requests (Fig. 7.6). 4. Priority: It is also a parameter assigned by the
The input data must be processed without any delays. OS at the time of process creation.
The processing time is shorter and the waiting time is 5. General purpose register
minimum. If an RTOS meets a deadline approximately, 6. List of open files
it is known as a soft RTOS, but if deadline has to be met 7. List of closed files
deterministically (hard deadline), it is a hard RTOS. All the attributes of a process are stored in the process
The events that an RTOS may have to respond to can be control block (PCB) (Fig. 7.7).
either periodic (occurring at regular intervals) or aperiodic
(occurring at irregular intervals). Further, if there are n peri- Process Id Process State
odic events and an event i occurs with period Pi and requires Process counter Priority
Ti seconds of CPU time, then the load can be handled if
List of open files List of close files
n
∑ (Ti / Pi ) ≤ 1 General purpose register
information
Accounting information
i =1
An RTOS that meets this criteria is said to be schedulable. I/O status information CPU scheduling
information
Memory management Context data memory
Real time OS
information pointers
Figure 7.7 | A process control block.
from CPU because the CPU is busy in executing state and scheduling that process into the run-
some other task. ning state.
5. Terminated: A process is in terminated state 3. Mid-term scheduler: It is responsible for sus-
when the processor has finished the execution of a pending and resuming the processor.
particular task.
6. Suspended ready: If processes are more than 7.3.3.1 Dispatcher
the capacity of the memory, then they will be sus-
pended and go to suspended ready state, which is It is responsible for saving and loading the context of a
in the secondary memory. process. The context switching is done by a dispatcher.
7. Suspended wait: If processes are more than the
capacity of memory, then they will be suspended 7.3.3.2 Degree of Multiprogramming
and go to suspended waiting state, which is in the
secondary memory. The number of jobs present in the memory is called as
the degree of multiprogramming. Long-term scheduler
controls the degree of multiprogramming.
7.3.2 Schedulers Note: The best data structure to implement ready
queue is double linked list.
An operating system has three types of scheduler
(Fig. 7.8): 7.3.3.3 Time Periods
1. Long-term scheduler: It is responsible for
A process has various time periods as follows:
bringing a newly created process into the system.
2. Short-term scheduler: It is responsible for 1. Arrival time (AT): When a process arrives into
selecting one of the processes from the ready ready state, it is called as arrival time.
Schedule/Dispatch Terminated
leted
Created Comp
d Priority
e
nd
e
e
um
sp
Su
es
I/O Operation
R
I/O
Suspended Block Suspend Suspended
Complete
ready wait wait
Resume
2. Burst time (BT): The time required by a pro- Example 7.1
cess for its execution is called as burst time.
3. Completion time (CT): The time when a pro- Consider the following workload table of three processes:
cess completes its execution is called as completion
time. Process Burst Time (ms)
4. Turn-around time (TAT): The time difference P1 15
between completion time and arrival time is called
as turnaround time. P2 4
TAT = CT − AT P3 2
5. Waiting time (WT): Waiting time for a pro- Let us suppose that the process arrives at time = 0 in
cess is the time duration which is spent in waiting the order P1, P2, P3. By applying the FCFS scheduling
queue by that process. algorithm, we get the result shown in the following
WT = TAT − BT Gantt chart:
6. Response time: The first scheduled time for a P1 P2 P3
process is called response time.
0 15 19 21
Waiting Time:
7.4 CPU SCHEDULING Process Waiting Time (ms)
WT = (TAT − BT)
CPU scheduling is the technique to put a process from P1 0
ready queue to running state. The objective of CPU
scheduling is to minimize the turnaround time and aver- P2 15
age waiting time of a process. The short-term scheduler
P3 19
is used to apply the CPU scheduling in the ready state
to select the process to schedule on to the processor (run-
Average waiting time = (Total waiting time)/3
ning state).
= (0 + 15 + 19)/3 = 34/3 = 11.33 ms
Turnaround Time:
7.4.1 Scheduling Algorithms Process Turnaround Time (ms)
TAT = (CT − AT)
There are many CPU scheduling algorithms as follows:
P1 15
1. First come, first serve (FCFS)
2. Shortest job first (SJF) P2 19
3. Shortest remaining time first (SRTF)
P3 21
4. Round robin
5. Priority based Average turnaround time = (15 + 19 + 21)/3
6. Highest response ratio next (HRRN) = 55/3 = 18.33 ms.
7. Multilevel queue
8. Multilevel feedback queue
7.4.1.2 Shortest-Job-First Scheduling
7.4.1.1 First-Come, First-Serve Scheduling In SJF scheduling, the job that requires CPU atten-
tion for lesser time is chosen first for CPU attention.
In this simple scheme, the allocation of CPU is on the The data structure used is a queue in which the jobs
basis of first come, first serve (FCFS). A queue data are arranged on the basis of CPU time required by the
structure is maintained for FCFS scheduling. When process (in ascending order). If there are two processes
the wait of the process is over and it enters into the that have the same requirement of CPU time, then the
ready state, its PCB is linked onto the tail of the queue. order in which they arrived (FCFS scheduling) is used to
The process at the head of the queue is allocated to the break the tie. This algorithm can be either pre-emptive
processor and the running process is removed from the or non-preemptive. Pre-emptive SJF scheduling is some-
queue. The FCFS scheduling is non-preemptive. times called shortest-time-first scheduling (STF).
Continued
Example 7.2
Consider the same Example 7.1: Process Arrival Time Burst Time
(ms) (ms)
Process Burst Time (ms) P3 2 8
P1 15 P4 3 3
P2 4
P3 2 1. Now, P1 is executed first, because it is the only
process available at that time (t = 0).
Assuming that all these processes arrive at the same 2. At t = 1, we have a new process P2 which requires
time, using SJF scheduling, we would schedule these only 2 ms whereas the already executing process P1
processes according to the following Gantt chart: still requires 4 ms to complete. Therefore, the CPU
will start executing P2 because it has the shortest run-
P3 P2 P1
ning time out of the two available processes P1 and P2.
0 2 6 21 3. At t = 2, we have three processes, namely P1, P2
Waiting Time: and P3, P2 has the shortest remaining time, there-
fore it will continue to execute.
Process Waiting Time (ms) 4. At t = 3, process P2 terminates and a new process
WT = (TAT − BT) P4 enters the ready queue. Now, we have three
P1 6 processes P1, P3 and P4 waiting for CPU attention.
P4 has the shortest remaining time (3 ms) as com-
P2 2 pared to P1 (4 ms) and P3 (8 ms).
P3 0 The Gantt chart is shown below:
Average waiting time = (6 + 2 + 0)/3 = 8/3 = 2.66 ms
P1 P2 P4 P1 P3
Turnaround Time:
0 1 3 6 10 18
Process Turnaround Time (ms) Turnaround Time:
TAT = (CT − AT)
P1 21 Process Turnaround Time (ms)
TAT = (CT − AT)
P2 6
P3 2 P1 10 − 0 = 10
P2 3 Example 7.5
P3 3 Consider the following example. We have the workload
table of five processes.
P4 5
Process Burst Time (ms) Priority
Using RR scheduling, we would schedule these processes
according to the following Gantt chart: P1 15 3
P2 1 1
P1 P2 P3 P4 P1 P4 P1 P1 P1 P1 P3 5 4
0 4 7 10 14 18 19 23 27 31 35 P4 1 5
P5 10 2
Waiting Time:
Using priority scheduling, we would schedule these pro-
Process Waiting Time (ms) cesses according to the following Gantt chart:
WT = (TAT − BT)
P2 P5 P1 P3 P4
P1 35 − 24 = 11
0 1 11 26 31 32
P2 7−3=4
Waiting Time:
P3 10 − 3 = 7
Process Waiting Time (ms)
P4 19 − 5 = 14 P1 11
P2 0
Average waiting time = (11 + 4 + 7 + 14)/4 = 9 ms
P3 26
Turnaround Time:
P4 31
Process Turnaround Time (ms) P5 1
TAT = (CT − AT)
Average waiting time = (11 + 0 + 26 + 31 + 1)/5
P1 35 = 69/5 = 13.8 ms
P2 7 Turnaround Time:
P3 10 Process Turnaround Time (ms)
P4 19 P1 26
P2 1
Average turnaround time = (35 + 7 + 10 + 19)/4
= 17.75 ms P3 31
(Continued)
P2 6 2 P1 3
P3 4 4 P2 7
P4 5 6 P3 9
P5 2 8 P4 14
P5 7
Using HRRN scheduling, we would schedule these pro-
cesses according to the following Gantt chart: Average turnaround time = (3 + 7 + 9 + 14 + 7)/5
= 8 ms
P1 P2 P3 P5 P4
0 3 9 13 15 20
7.4.1.7 Multilevel Queue Scheduling
1. Now P1 is executed first, because it is the only
process available at that time (t = 0). In multilevel queue scheduling, the ready queue is par-
2. At t = 2, we have a new process P2 which requires titioned into several separate queues such that each
only 6 ms whereas the already executing process process belongs to some queue. Various properties for
P1 still requires 1 ms to complete. Therefore, the allocating jobs to different queues may depend upon
CPU will continue to execute P1 because it is non- memory size, process priority or process type, or any
preemptive scheduling. other parameter(s). Each queue may follow different
3. At t = 3, we have only one process P2, so P2 will scheduling algorithms. Figure 7.9 depicts the multilevel
get executed. queue scheduling.
7.5.2 Interprocess Communication data from file or some want to insert or change the con-
tents in the file. According to Bernstein constraint, many
Inter-process communication is a technique to exchange readers may access the file at the same time, but when
data among multiple processes or threads. IPC allows the writer is accessing the file, no one should be allowed
programmers to schedule activities to different processes. to access it. This can be ensured by following methods:
One process controls the whole communication. There
are different IPC techniques available: 1. Assign readers more priority than writers. If a
writer has no permission to access the file, any
• Message passing reader can get access to it. But this may cause
• Shared memory infinite waiting for the writer.
2. Another method can be used to assign writers large
• Remote procedure calls
priority over readers. When writer requests for file,
• Synchronization all the current readers finish their access and new
readers have to wait until the writer finishes its
7.5.3 Classical Synchronization Problems task. This may cause infinite waiting for reader.
as “race condition”. The producer–consumer example For example the process is checking repeatedly whether
discussed in the previous section is one such example. a lock is available or not. This technique can be used
The part of the program where the shared memory is to generate the random time delays. This consumes so
accessed is known as critical section. The following are many CPU cycles.
the four conditions that are used to provide a good solu-
do
tion to this problem:
Pi Pj
while(flag[j]
and turn
and =turn
j); = j);
{flag[j]
flag[j]
= true;
turn =turn
i; = i;
while(flag[i]
= true;
while(flag[i]
and turn
and =turn
i); = i);
Critical
Critical
Section
Section Critical
Critical
Section
Section
flag[i]
flag[i]
= false
= false flag[j]
flag[j]
= false
= false
Remainder
Remainder
Section
Section Remainder
Remainder
Section
Section
}while(1);
}while(1); }while(1);
}while(1);
1. Wait: It reduces the value of semaphore by 1 unit. The value of binary semaphores can range between 0
It can be denoted by P(S). and 1 only. Binary semaphores are also known as mutex
Wait(S): locks as they ensure mutual exclusion.
P (mutex)
Critical Section
V (mutex)
The code for P10 and P11 is identical except it uses V in the place of P and P in the place of V.
How many processes will be in critical section?
Solution:
Let S = 1
P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 P11
P P P P P P P P P P V
V V V V V V V V V V P
Note: 1. With change in one sequence, three processes will enter in the critical section.
2. With change in two sequences, five processes will enter into the critical section and so on.
Table 7.2 summarizes the common concurrency In the above code, let S = 1, Q = 1
mechanisms
Process P0 will make S → 0
Table 7.2 | Common concurrency mechanisms
Process P1 will make Q → 0
1. Semaphore An integer value is used for
Both will not be able to move further. This is the dead-
signalling between different
lock state.
processes. Only three operations
are performed — initialize,
increment and decrement. Also 7.6 DEADLOCKS
known as counting semaphore.
2. Binary Only two values 0 and 1 are used. A deadlock is a situation where computer programs sharing
Semaphore the same resources are permanently blocked. As a result
3. Mutex Similar to binary semaphore (lock of which, each program prevents the other from acquiring
and unlock values are used) but the the resources required for its completion. Let us consider
major difference is that the process an example, there are two friends (processes) and they
which has locked the mutex is the intend to write a letter. To accomplish this task they need
only one that can unlock it. two pens and two pages (resources). Now, one of them has
4. Monitor It is a line of code that contains pages (papers) and the other a pen. A possible solution
access procedures, initialization would have been that one person can give his resource to
code and variables. The access another so that the other person completes the job (write
procedures are critical sections. the letter) and after his job is finished this person can also
complete the job. But none of them is willing to part the
(Continued) resources s/he has. This is a simple case of deadlock.
There are two types of resources, namely, pre-emptable A resource allocation graph is a graph which has two dif-
and non-preemptable resources. ferent types of nodes, the process nodes (represented by
circle) and resource nodes (represented by rectangles).
1. Pre-emptable: These resources can be taken
For different instances of a resource, there is a dot in
away from the process with no ill effects, for exam-
the resources nodes (rectangle). For example, if there
ple, CPU, memory.
are two identical drives, we will depict it with two dots.
2. Non-preemptable: These resources can be
taken away from the process (without causing The directed edges among these nodes represent
any ill effect). For example, CD, USB drive, plot- resource allocation and release. An edge from the resource
ter, printer, etc. We cannot give such resources to to the process node denotes that it has been acquired by
other processes in the middle of their jobs. the process, whereas an edge from the process node to
the resource node denotes that the process is request-
ing for the resource. For example, consider the resource
7.6.2 Characteristics of a Deadlock allocation graph in Fig. 7.13 and we can easily conclude
that there is a deadlock.
The following four conditions must exist simultaneously
for a deadlock to occur:
R1
P2
Figure 7.12 | A traffic deadlock.
P1
The following conditions are depicted in the resource 1. Mutual exclusion condition: Let us take the
allocation graph shown above: example of tossing a coin, getting a head or tail is
1. There are three sets of process (P), resource mutually exclusive, which means that we cannot
(R) and edges (E): have both head and tail. The mutual exclusion
P = {P1, P2} condition means that there are some resources
(such as CD Drive, Printer, etc.) which once allo-
R = {R1, R2} cated to a process cannot be taken back unless and
E = {R1 → P1, P1 → R2, R2 → P2, P2 → R1} until the process releases it (i.e., non-preemptable
2. Resource instances: resources). So, such resources should be allocated
Two instances of R1 to only one process at a time. They cannot be
shared. Although, this does not mean that none of
One instance of R2 the resources can be shared. Certain pre-emptable
3. Process states: Process P1 is holding an instance resources (such as memory, CPU) can be shared,
of resource type R1 and is waiting for an instance because they will never result in a deadlock. So,
of resource type R2. Further, process P2 is holding those resources which cannot be shared are mutu-
an instance of resource type R2 and is waiting for ally exclusive and this condition needs to be taken
an instance of resource type R1. care by OS.
2. Hold and wait condition: In order to ensure
The following tips help us to check the resource alloca-
this condition never occurs in the system, we have
tion graph easily and predict the presence of cycles.
to devise a strategy such that a process requests
1. If there is no cycle in the resource allocation graph, all its required resources at one time and blocks
there is no deadlock. the process until all requests can be granted. This
2. If there is a cycle in the graph and each resource approach has its drawback that a process cannot
has only one instance, then there is a deadlock. In know what resources would be required before it
such a case, the presence of a cycle is the necessary actually starts execution, and sometimes it may
condition in order for deadlock to occur (as there is have to wait for a long time in order to get all
only one instance of the resource). its required resources; whereas it could have
3. If there is a cycle in the graph, and each resource used some of the already allocated resources and
has more than one instance, there may or may not released them back to the pool to be used by other
be a deadlock. Therefore, we conclude that a cycle processes. For example, a program may require five
in the resource allocation graph is a necessary but CDs, there should be five free drives before the
not a sufficient condition for a deadlock in the case execution begins, while the program might be using
of multiple instances of a resource. only one drive to begin with. Thus, this approach
leads to serious waste of resources.
The following table summarizes the above discussion 3. Non-Preemption condition: The non-
preemption condition can be overcome by the
Possibility of Dead- Existence of Dead- following protocol. If a process is holding some
lock (03 conditions) lock (04 conditions) resources and requests another resource that cannot
be immediately allocated to it (which means, the
Mutual Exclusion, No Mutual Exclusion, No process must wait), then all resources currently
Preemption & Hold and Preemption, Hold and being held are pre-empted (released). These pre-
wait wait & Circular Wait empted resources are added to the list of resources.
For example, when a process releases resources,
A deadlock can be dealt by following any of the three the process may lose all its work to that point.
strategies: The major disadvantage of this strategy is the pos-
sibility of indefinite postponement (starvation).
1. Deadlock prevention
However, this protocol is often applied to resource
2. Deadlock avoidance
whose state can be easily saved and restored later,
3. Deadlock detection and recovery
such as CPU registers and memory space. It cannot
generally be applied to printers and drives.
7.6.4 Deadlock Prevention 4. Circular wait condition: A linear ordering
of different resources can be used to allocate the
To prevent a deadlock, we must ensure that any one of resources to a process, which means that if a pro-
the following four conditions listed below (Section 7.5.2) cess has been allocated a resource, then it can
should not hold. Let us consider them one by one: only be allocated a resource in the linear order.
Another way to implement it could be use some 1. Basic principle: It is similar to a banking system,
kind of indexing or a subscript. Such a strategy is that is, to ensure that the bank never allocates its
helpful in avoiding cycle(s) in resource allocation available cash such that it can no longer satisfy the
graph and results in overcoming the circular wait needs of all its customers.
condition. 2. Working: When a new process enters the system,
it must declare the maximum number of instances
of each resources type it may need. This number
7.6.5 Deadlock Avoidance should not exceed the total number of resources in
the system.
The side effects of preventing deadlocks are similar to
the side effects of walking carefully so that you donot Now, when a user requests a set of resources, the system
fall as compared to walking freely. These are as follows: must determine whether the allocation of these resources
1. Low device utilization will leave the system in a safe or in unsafe state. If in
2. Reduced system throughput safe state, then the resources requested are allocated,
otherwise the process must wait until some other process
Another approach is to anticipate a deadlock before it releases the requested resource.
actually occurs. This method differs from deadlock pre-
We need the following data structures:
vention, which guarantees that deadlock cannot occur
by denying any of the four necessary conditions of the 1. Available: A vector of length m indicates the
deadlock, whereas a `Deadlock Avoidance’ algorithm number of available resources of each type. If
dynamically examines the resource-allocation state to Available[j] = k, there are k instances available of
ensure that there can never be a circular wait condition. resource type Rj.
There are three basic pillars to attain this objective: 2. Max: An n × m matrix defines the maximum
1. Safe state algorithm demand of each process, if Max[i, j] = k, then pro-
2. Resource allocation graph algorithm cess Pi may request at most `k’ instances of resource
3. Banker’s algorithm type Rj.
3. Allocation: An n × m matrix defines the number
of resources of each type currently allocated to
7.6.5.1 Safe State each process.
•• If Allocation[i, j] = k
A safe state can be defined as a state in which there is •• Then, process Pi is currently allocated `k’
no deadlock. This can be achieved if instances of resource type Rj.
1. All the requested resources are allocated to the 4. Need: An n × m matrix indicates the remaining
process. resources needed for each process.
2. If a process needs an unavailable resource, it may •• If Need[i,j] = k
wait until the same has been released by a process •• Then, process Pi may need `k’ more instances of
to which it has already been allocated. If such a resource type Rj to complete its task.
sequence does not exist, it is known as unsafe state. •• Further, Need[i, j] = Max[i, j] − Allocation[i, j]
The Banker’s algorithm consists of the following two
Taking care of these two rules will never result in a
parts:
deadlock.
1. Safety algorithm: The algorithm for finding out
7.6.5.2 Resource Allocation Graph whether a system is in a safe state can be defined
as follows:
Algorithm
•• Let `Work’ and `Finish’ be vectors of lengths m
The resource allocation graphs can also be used for dead- and n, respectively.
lock avoidance. The major limitation of this strategy is Work: = Available
that it is not applicable to a resource allocation system Finish [i]: = False, for i = 1,2,3, …, n
with multiple instance of each resource type. •• Find an `i’, such that
Finish[i]: = False
Need i ≤ work, if no such i exists, go to step 4.
7.6.5.3 Banker’s Algorithm (Proposed by •• Work: = Work + Allocation
Dijkstra’s) Finish[i]: = true
Go to step 2.
Banker’s algorithm is used to avoid deadlocks, is dis- •• If Finish[i]: = true for all i, then the system is
cussed in the following text: in a safe state.
2. Resource request algorithm does, the request is granted; otherwise, it is postponed
for some time later. This algorithm has a time complex-
If Requesti[j] = k, then process Pi wants k instances
ity of O(n2), where n is the number of processes.
of resources type Rj. When a request for resources is
made by process Pi, the following actions are taken: Limitations of the Banker’s Algorithm
•• If Requesti ≤ Needi, go to step 2. The following are some limitations of the banker’s
Otherwise, raise an error condition because the algorithms:
process has exceeded its maximum claim.
•• If Requesti ≤ Available, go to step 3. 1. A fixed number of processes and resources are
Otherwise, Pi must wait, as the resources are required. Further, all the processes should know
not available. their requirement of resources before starting.
•• Let the system pretend to have allocated the 2. No other process can start until the algorithm has
requested resources to process Pi by modifying been completely executed.
the state as follows: 3. It relies on the input information, the resources
Available: = Available − Requesti; may get underutilized if inaccurate information is
Allocationi: = Allocationi + Requesti; provided.
Needi = Needi − Requesti 4. This algorithm itself takes good amount of time
to execute, more information about processes and
Now, if the resulting transactions are safe, process Pi is resources will result in more time to execute.
allocated its resources. 5. The requests granted should be in a finite time.
6. If a resource becomes unavailable (e.g., a CD drive
Time Complexity
stops functioning), it can result in an unsafe state.
Banker’s algorithm considers each request as it occurs, 7. It is difficult to have a priori information about the
and checks whether it leads to a safe state or not. If it need of resources.
Problem 7.2: Consider a system having five processes and three resources types A, B and C. Resource type A has
nine instances, B has four instances and C has seven instances. Initially, the snapshot of the given system is as
follows:
Process Need
ABC
P0 7,4,3
P1 1,2,2
P2 6,0,0
P3 0,1,1
P4 4,3,1
Step 1.2: Now, we will decide which process should execute first, that is, what are the processes that need resources
which are less than available.
So, we will start with P1 (because its resource need is less than the available resource) and after its completion all
the resources will be free; hence, after completion of P1 the available matrix contents will be
Available
222
+ 2 0 0 (resources held by P1)
422
Step 1.3: Again let us see the status
So, P4 executes and similarly we can say that P2 and then P0 will be executed, and as a result, the available matrix
after execution of each process would be:
Available 6,3,3
P4 executes 0,0,2 (+)
6,3,5
After the execution of P2
Available 6,3,5
P2 executes 3,0,2
9,3,7
Therefore, the proposed sequence of execution is [P1, P3, P4, P2, P0]. Now, we will find whether this sequence of
execution is a `safe sequence’. We will now consider the resource request algorithm (the second portion) of the
Banker’s algorithm. In the beginning, we have:
Step 2.1:
Work = [2,2,2] (i.e., avail at the start)
Finish = [F, F, F, F, F] (the finish value of all processes is F meaning False)
So, P1 is executed first, and we observe that
Needi < Work: [1,2,2] < [2,2,2] holds good, and as a result we have
Work = [4,2,2]
Finish = [F,T,F,F,F] (the finish value of P1 is T meaning True)
Step 2.2:
Next, we have P3 executed and in this case the condition Needi < Work holds good, and as a result we have,
Work = [6,3,3]
Finish = [F,T,F,T,F]
After the execution of P4, we have
Step 2.3:
Work = [6,3,5]
Finish = [F,T,F,T,T]
After the execution of P2, we have
Step 2.4:
Work = [9,3,7]
Finish = [F,T,T,T,T]
After the execution of P0, we have
Step 2.5:
Work = [9,4,7]
Finish = [T,T,T,T,T]
In the whole process, we have never encountered a situation where the condition Needi < Work does not hold good.
Therefore, we can execute our processes in the order [P1, P3, P4, P2, P0] without encountering the deadlock.
7.6.6.1 Deadlock Detection
• Available: A vector of length m indicates the
To deal with this concept further, we can discuss the number of available resources of each type.
different types of resources available, namely, reusable • Allocation: An n × m matrix defines the num-
resources and consumable resources (Table 7.3). ber of resources of each type currently allocated to
to the newly created child process. The fork() returns is allocated and when the process is complete it
a positive integer (process id of child process) to the releases the particular partition to be re-used by
parent process. The fork() returns a negative value if a another process.
child process creation is unsuccessful.
Note: If a program contains n fork() calls, then the
number of created child processes are (2n − 1). 0 70 K
1 40 K
2 110 K
Problem 7.3: Consider the following code and find
the total number of processes. 3 105 K
main() 4 250 K
{
fork (); 5 30 K
fork (); 6 80 K
fork ();
printf (“hello”); 7 120 K
}
Figure 7.17 | A fixed memory partition scheme.
Solution:
Number of child process = (2n − 1) = 23 − 1 = 7 = 7 2. Variable partition scheme: Variable-sized partition
is also known as dynamic partitioning (Fig. 7.18).
Total number of process = Parent process + Child
In this scheme, initially the memory will be full
process = (1 + 7) = 8
contiguous free block. Whenever it gets a request
from a process, accordingly partition is made.
The processes are accommodated in the memory
7.8 MEMORY MANAGEMENT according to their requirements and thus internal
fragmentation is overcome. But external fragmen-
tation exists.
The OS manages both the primary memory (RAM)
and the secondary memory (hard disk). In the follow- P1
ing section, we discuss various primary management P2
algorithms followed by secondary memory management
algorithms (hard disk scheduling algorithms). The OS P3
manages the memory in an efficient and orderly manner. P4
The OS has the following main responsibilities related to
the memory–Isolation of a process (non-interference), P5
Allocation and management (based upon an algorithm,
the user does not decide but knows where the content P6
is), Protection and access control (security) and reliable P7
long-term storage.
P8
of the process. The search can start from either end 4. Worst Fit: It is the exact opposite of best fit.
(up or down). The resource is allocated a partition that results in
2. Next Fit: A variation of first fit, the allocation higher free space. The idea behind this strategy is
is started from the previous partition that was that, a small process when allocated with a bigger
allocated. partition will be able to make it free in a lesser
3. Best Fit: The entire list of free partitions is time, this free partition may be used at a later
scanned and the partition that fits best, that is, stage by a process which might require a large par-
which results in a lesser free space after allocation tition; so that larger process does not have to wait
is chosen. for resource allocation.
Problem 7.4: Request from the processes is 300K, 25K, 125K and 50K, respectively.
150 K 350 K
The above request could be satisfied with? (Assume variable partition scheme)
(a) Best fit but not first fit
(b) First fit but not best fit
(c) Both best and first fit
(d) Neither best nor first fit
Solution:
We have to check for only two algorithms best fit and first fit (as given in question). Let us discuss by considering
one algorithm at a time, as follows:
(a) In Best Fit:
1. First of all, the 300K jobs will use 350K slot. Free space in the slot will be = 350 − 300 = 50K.
2. Next process of 25K will use remaining 50K slot (available as free from the operation above), as per the best
fit algorithm. So, the free space left = 50 − 25 = 25K.
3. Third process of 125K will be accommodated by 150K space, so we are left with 150 − 125 = 25K.
4. Fourth process requires 50K. Now, we have two slots of 25K left but our last process is of 50K size.
Therefore, the need of all the processes is not satisfied.
(b) In First Fit:
1. First process of 300K will use 350K slot. Remaining free space will be = 350 − 300 = 50K.
2. Next process of 25K will use remaining 150K slot (as per first fit algorithm). So, free space left = 150 − 25
= 125K slot.
3. Third process of 125K will be accommodating by 125K slot which is left free after the allocation of the
memory to second process.
4. Now, we have only one slot of 50K left and our process requires 50K memory.
Observing, the execution of both the algorithms, we can say that by using the first fit algorithm, all processes can
be executed. Therefore, option (b) is the correct answer.
A program may consist of different modules which are Loading is copying a program from secondary storage
to be `linked’ and then `loaded’ in the Main Memory for into main memory so it is ready to run. In some cases,
execution. An analogy to understand it could be that a loading just involves copying the data from disk to
program in C is first linked (as it makes use of different memory, in others it involves allocating storage, setting
header files) and then loaded in memory for execution. protection bits or arranging for virtual memory to map
It is hereby worth-mentioning that the image of the pro- virtual addresses to disk pages. Loading can be done
gram gets loaded into memory. through the following two ways:
1. Static Loading: It is the technique in which the 1. Compiler Time: If the compiler takes responsibility
entire program loads into memory before the start of associating a program and data to the actual physi-
of execution of the program. Some points of static cal memory location, it is called compile-time address
loading are as follows: binding. The compiler needs to interact with the
•• Inefficient utilization of memory operating system memory manager to perform com-
•• Faster program execution pile-time address binding. This type of address bind-
•• Use of static linking if static loading is used ing is done before loading the program into memory.
2. Load-time Address Binding: This type of
2. Dynamic Loading: It is the technique in which
address binding is performed after loading the pro-
the program is loaded into memory on demand.
gram into memory. The load-time address binding
Some points of dynamic loading are as follows:
•• Efficient utilization of memory
is performed by a loader.
•• Slower program execution
3. Run-Time (dynamic) Address Binding: In the
•• Use of dynamic linking if dynamic loading is used
dynamic address binding, address binding is post-
poned till the time of execution. Dynamic binding is
The majority of OS is used dynamic loading and dynamic performed by the processor. The majority of OS uses
linking. dynamic binding.
Association of program instruction and data to the actual Paging is one of the memory-management schemes
physical memory location is called as address binding. where a computer can store and retrieve data from sec-
ondary storage for use in the main memory (Fig. 7.19).
Instruction Address This technique maps processor-generated logical address
I1 20 to physical address. As we know,
I2 30 Logical address > Physical address
I3 40 There are few terms used in paging:
I4 50 1. Logical Address Space (LAS): Defines the size
of logical memory.
Address binding track to increment program counter. 2. Physical Address Space (PAS): Defines the
There are three types of address binding: size of physical memory.
13 bit 12 bit
13LA
bit
CPU LA 2 12 bit10
3 10
CPU 2 10
3P 10d g d
P d g d
00
0 00
01 11 (F)
01
011 12 11 (F)
10 (F) 01
011 23
10 P3
34 10 (F)
01 (F) 10 P3 10
45
10
56 01 (F) 11
67 00 (F)
11
7 00 (F)
Page table PAS
Page Page table PAS
numbers
Page
numbers
P= Page number bits
fP = Framenumber
Page numberbits
bits
d =
f = Frame number bitssize
Page size or frame
Figure 7.19 | A memory paging scheme.
d= Page size or frame size
CPU p d f d
P
LA TLB 0 PA
miss 1
2
P7
3
4 P3
5 P2
TLB 6 P1
Page 7
numbers Physical
TLB Page table address space(PAS)
HIT
1. If page table is kept in the main memory and the 7.8.5 Multi-Level Paging
main memory access time is M, then formula for
effective memory access time for page table is Paging technique faces problem of large memory require-
EMAT = 2M ment due to the large logical address space. Multi-level
paging technique provides the solution to the problem
2. Translation look-aside buffer (TLB) is added to that occurred in paging technique (Fig. 7.21).
improve the performance of page table. TLB con-
tains the frequently referenced page number and Example:
corresponding frame number. If the TLB access Assume that processor-generated logical address (LA) is
time is C and TLB hit ratio is X, then effective 35-bit long and physical address (PA) is 26-bit long, then
memory access time for page table is Logical address space = 235 words
= 25 × 230 words
EMAT = X(C + M ) + (1 − X )(C + 2M )
= 32G words
Physical address space = 226 words
= 26 × 220 words
Problem 7.7: Consider a system with TLB access = 64M words
time 20 ns and main memory access time 100 ns.
Calculate the effective memory access time if TLB The logical address is divided into equal size pages. In
hit ratio is 95%. our case, page size is given as 4K words. So,
LAS 32G words
Solution: Number of pages = = = 8M pages
Page size 4K words
Given that C = 20 ns, M = 90 ns, X = 95%. Without
TLB effective memory access time LAS 32G words
Number of pages = = = 8M pages
Page size 4K words
EMAT = 2M = 2 × 100 = 200 ns
To represent 8M pages, we require 23 bits. So, the logi-
With TLB, effective memory access time cal address is divided into two parts P (page number or
number of bits required to represent the pages of LAS)
EEMAT =X
MAT = M))++((11−−X
X((CC++M X)()(CC++22M
M))
and d (page size or number of bits required to represent
==00.95
.95((20 100))++((11−−00.95
20++100 20++22××100
.95)()(20 100)) the page size of LAS). This means LA is divided into
==11
1144++11 11==125
125 nsns P = 23 and d = 12 bits. And P is further divided into
two parts P1 and P2.
CPU LA
P p d
(x) (f)
Physical address
space
n n
{ P1
P2
Number of bits required to represent pages of page table
Number of bits required to represent size of page of page table
{
Figure 7.21 | Multi-level paging scheme.
1. P1 is the page number or number of bits required table for every page, inverted paging is used (Fig. 7.22).
to represent the pages of page table. In inverted paging, only one table is maintained for all
2. P2 is the page size or number of bits required to
represent the page size of page table.
The physical address is divided into equal-size frames. Problem 7.8: Consider a system with logical address
Generally, frame size = page size, given as 4K words. So, = 34 bits, physical address = 29 bits and page size
= 16 KB. The memory is byte addressable and the
PAS 64M words page table entry size is 8 bytes.
Number of frames = = = 214 = 16K frames
Frame size 4K words (a) What is the conventional page table size?
PAS 64M words (b) What is the inverted page table size?
es = = = 214 = 16K frames
Frame size 4K words Solution:
To represent 16K frames, we required 14 bits. So, physi- (a) Conventional page table size
cal address is divided into two parts f (frame number or
number of bits required to represent the frames of PAS) LAS 234
and d (frame size or number of bits required to represent Number of pages = = 14 = 220
Page size 2
the frame size of PAS). Means PA is divided into f = 14
and d = 12 bits. Page table size = Number of pages × Page table
Problems associated with multilevel paging: entry size = 220 × 8 bytes × 8 B = 8 MB
Memory requirements are low for multilevel paging but
(b) Inverted page table size
address translation time is more due to more number of
levels. This can be solved by using Translation lookaside
PAS 229
buffer. Number of frames = = 14 = 215
Frame size 2
7.8.5.1 Inverted Paging Page table size = Number of frames × Page table
entry size
In paging technique, every process has its own page table = 215 × 8 bytes × 8 B
and because of this there is a problem of huge page table
= 256 KB
size. To overcome overhead of maintaining the page
CPU P d F d
LA
the processes. The number of entries in the inverted page S is the segment number; it is equal to the number of
table is the same as number of frames in the PAS. bits required to represent the segment of LAS.
LA
CPU S D
D D
Virtual memory is a memory management technique The time taken to service a page fault is called as page
that is implemented using both hardware and software. fault service time. If the main memory access time is M,
It maps memory addresses used by a program, called page fault service time is S, which is much larger than M
virtual addresses, into physical addresses in computer (S >> M) and page fault rate is P, then
memory. Virtual memory gives an illusion to the pro- Effective memory access time,
grammer that programs which are larger in size than
actual memory can be executed. Virtual memory can be EMAT = P × S + (1 − P ) × M
implemented with demand paging.
7.8.7.1 Demand Paging Problem 7.9: Consider a system with page fault ser-
vice time (S) = 100 ns, main memory access time
Demand paging is a method of virtual memory manage- (M) = 20 ns, and page fault rate (P) = 65%. Calculate
ment (Fig. 7.24). In a system that uses demand paging, effective memory access time.
the operating system copies a disk page into physical Solution:
memory only if an attempt is made to access it and
that page is not already in memory (i.e., if a page fault EMAT = P × S + (1 − P ) × M
occurs).
Figure Description:
= 0.65 × 100 + (1 − 0.65) × 20
Step 1: Processor is looking for page number 4. But = 0.65 × 100 + 0.35 × 20
page 4 is not available in the memory, that is, page fault. = 65 + 7 = 72 ns
Without page 4 currently executing, program will stop.
Step 2: The signal is send to OS that processor is look- Table 7.4 summarizes the different memory management
ing for page 4, which is currently not available in the techniques.
main memory.
Step 3: OS will search for required page 4 in LAS.
Step 4: OS will replace the page from LAS to PAS by 7.8.8 Page Replacement Algorithms
using the page replacement algorithm.
Whenever there is a demand of any page which is not in
Step 5: OS updates the page table accordingly. memory then that must be loaded. But if there is no free
Step 6: The signal is sent to the processor, which says frame in memory, one page is selected for replacement.
the required page is brought into the memory and then Page replacement algorithm decides which page is to be
the processor continues the program execution. replaced at the time of page fault.
Step 3: Next page referred is 1, which will be replaced In the case of optimal page replacement algorithm, page
with 2 and so on. will be replaced that will not be referenced in future.
1 1 1 11 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1
2 2 22 2 2 2 4 4 4 2 2 2 2 3 3 3 4 4 4
0 3 3 3 3 3 3 0 0 0 2 2 2 0
Miss Miss Hit Hit Miss Miss Hit Hit Miss Hit Miss Miss Miss Hit Hit Miss Miss Hit Miss Miss Hit Miss
Working set:
{1,2,3,4}
2 6 1 5 7 7 7 7 5 1 6 2 3 4 1 2 3 4 4 4 3 4 3 4 4 4 1 3 2 3
D=10
7.9 FILE SYSTEM Such a type of file system is more popular in present day
OS. A path name is the path from the root, through all
the subdirectories, to a specified file. There are two types
7.9.1 Directory Structures of path names:
1. Absolute path name: It begins at the root and
The directory structures are used to manage and orga- follows a path down to the specified file, giving
nize data. This can be done in the following ways: directory names on the path. For example, root/
1. The disks can be split into one or more partitions. spell/mail/sys/units.
These partitions provide separate areas within one 2. Relative path name: It defines a path from the
disk and are treated as a separate storage devices. current directory. Considering the same example
2. Each partition contains information about files. as above, let us suppose that we are working in a
The device directory contains information such as current directory named root/spell/mail, then the
name, location, size and type. relative path can be sys/units, instead of specifying
the full path name.
7.9.1.1 Single-Level Directory
7.9.1.4 Acyclic Graph Directories
It is the simplest directory structure (Fig. 7.27), in which
there is one directory which contains all the files. Major An acyclic graph is a graph with no cycles and is a natural
limitation lies in the number of files (memory size) a generalization of the tree-structured directory scheme (Fig.
directory can hold and there is more than one user that 7.29). A tree structure prohibits the sharing of files or direc-
needs to access the files. tories, whereas an acyclic graph allows directories to have
shared subdirectories and file, which means the same file
7.9.1.2 Two-Level Directory or subdirectory may be in two different directories. With a
shared file, only one actual file exists, so any changes made
To overcome the above problems, a separate directory can
by one person are immediately visible to the other.
be created for each user, known as — User File Directory
(UFD). The major limitation is the sharing of files among
the users. This scheme is represented in Fig. 7.28. 7.9.2 Allocation Methods
7.9.1.3 Tree-Structured Directories The direct access nature of disks allows us flexibility in
the implementation of files and usually many files are
Directory structure is extended to a tree of arbitrary stored on the same disk. The main problem is how to
height. In this, users can create their own subdirectories allocate space to these files so that disk space is uti-
and manage their own files. There is a root directory too. lized effectively and files can be accessed quickly. Three
Dateien
mail spell
list a b c c zzz dd
list e3 w4
Figure 7.29 | Acyclic graph directory structures (the sub directory list is shared by zzz and b).
widely used methods of allocating disk space are dis- and external fragmentation. It supports sequential and
cussed in the following text. random access.
7.9.2.2 Linked Allocation
7.9.2.1 Contiguous Allocation
In linked allocation, each data block contains the address
In contiguous allocation method, each file occupies a
of the next block in the file (Fig. 7.31). In contrast to con-
set of contiguous blocks on the disk. All the blocks
tiguous allocation, the disk blocks are scattered on disk.
are continuously kept together. There is an index that
The directory entry contains an index, which points to the
specifies the starting address and the length (size)
starting block as well as the ending block. Each block in
indicating the blocks (Fig. 7.30). Different allocation
turn contains an address of the next block, similarly, all the
strategies are first-fit, best-fit or worst-fit (already dis-
blocks are linked together. The last block does not have a
cussed earlier). This technique suffers from internal
0
File Start Length
5 Cnt 0 3
10 List 9 4
.
.
.
.
.
.
.
Directory
0 File Start End
5 Cnt 7 31
10
30
35
next block address (just as linked list). The major advan- dynamic access without any external fragmentation and
tage being, no external fragmentation with a limitation of there is very less loss of information in case of breaking
slow access (because we have to traverse all the blocks in of links. Let us take an example of a loss of pointer, in
the list before the required block). Another major drawback this case only information about that block will be lost
is the breaking of the link (similar to dangling references). whereas in linked allocation information stored in the
rest of blocks will also be lost.
7.9.2.3 Indexed Allocation It is hotly debated how large the index block should
be. Every file must have an index block, so we want the
There is an index block that contains the address of index block to be as small as possible. If the index block
every block that contains the information associated with is too small, however, it will not be able to hold enough
that file (Fig. 7.32). This approach is better as provides pointers for a large file, and a mechanism will have to be
Directory
File index block
1 Cut 15
7 9
16
1
10 10
15 13
-1
13 15 -1
-1
16
Figure 7.32 | Indexed allocation of disk space (-1 represents not used).
available to deal with this issue. The following are the • Number of disk block possible in one disk block =
possible solutions: DBA size
1. Linked scheme: An index block is normally of DBA
one disk space. Thus, it can be read and written
directly by itself. •• If asked for maximum file size, then calculate for
2. Multilevel index: The first index block contains highest power only such as Problem 7.10.
the set of pointers to secondary index blocks, which
in turn contain pointers to the actual data blocks. Problem 7.10: Consider the UNIX i-node which uses
3. Combined scheme: It is used in the UNIX File 12 direct DBAs, 1 single indirect, 1 double indirect, 1
System (UFS) i-nodes (see Fig. 7.33, which rep- triple indirect. The disk block address requires 32 bits
resents the UFS i-node scheme), where first few and the disk block size is 1 KB. What is the maxi-
data block pointers of the index block are stored mum file size?
directly in the i-node and then single, double and (a) 8 GB (b) 16 GB
triple indirect pointers provide access to more data (c) 32 GB (d) 64 GB
blocks as needed.
DBA size
Solution: DBA size 2 DBA size 3
Total size of file system = Number of direct DBA + ×DB size
DBA DBA DBA DBA 3
+ + +
size
DBA size DBA sizeMaximum
2 DBA file 3 = DBA × DB size
size
Total size of file system = Number of direct DBA + + +
size
+ ×DB size
DBA DBA DBA
DBA size 1 KB 3 3 210 3
1 KB = 2 × 210 bytes
3
×
×size × DB size
DBA size
2
=size
DBA file
Maximum file size =Maximum DB =
DBA size DBA size 2 DBA size 3 4 bytes
DBA
f direct DBA + ×DB
DBA DBA DBA
+ + + size
1 KB 3 = 1610GB 3 3
21 KB 210 3
= × 1 KB = = 2 × 210×bytes × 210 bytes
1 KB = 2
where DBA is disk block address. 4 bytes 2
4 bytes 2
= 16 GB = 16 GB
Mode
Owners (2)
Timestamps (3)
data
Size block
data
Count data
Direct blocks
data
. data
Single indirect . data .
.
.
. data
data .
Double indirect
.
.
Triple indirect . data
.
.
data
7.10 I/O SYSTEMS Problem 7.11: Consider a disk which has 16 plotters,
each plotter is divided into two surfaces. Each surface
has 1K tracks and each track has 512 sectors. Each
There are different devices that are connected with a
sector can store 2 KB data.
computer. These devices are broadly classified as:
(a) Find the capacity of the disk.
1. Machine readable: Most of the electronic equip- (b) How many bits are required to identify a sector?
ments such as hard disk, floppy disk, CD, USB, bar
Solution:
code reader, actuators, etc.
2. Human readable: Devices such as printer, key- (a) Capacity of disk = (Plotters × Surface × Tracks
board and Video Display Unit (VDU), usually × Sector × Sector Size) bytes
called monitor screen (but is different from moni- = (16 × 2 × 1024 × 512 × 2048) bytes
= (32 × 210 × 29 × 211) bytes
tor, as used in OS). Such devices transfer the data
at a slower rate as compared to the data transferred
between the memory and CPU. To overcome such = 32 × 23 bytes
differences in the rate of transfer, the concept of = 32 GB
SPOOLing (Simultaneous Peripheral Operations
(b) Number of bits required to identify a sector
OnLine) is used.
3. Communication: Network Interface Cards (NIC) = (Bits for plotter + bits for surface + bits for
are used for network communication or modems, tracks + bits for sectors)
etc. = (4 + 1 + 10 + 9) bits
There are different techniques used for I/O, such as = 24 bits
Programmed I/O, Interrupt driven I/O and DMA I/O.
These are covered in detail in Chapter 2.
Spindle
Track
Read/write header
Upper surface
Lower surface
Plotter
Cylinders
Sector
Surface
Problem 7.12: Consider a disk which has average seek time of 32 ns and rotational rate of 360 rpm (round per
minute). Each track of the disk has 512 sectors, each of size 512 bytes.
(a) What is the time taken to read four continuous sectors?
(b) What is the data transfer rate?
Solution:
(a) Time to read four continuous sectors = (Seek time + rotational latency + transfer time)
I. Seek time = 32 ns = 32 × 10−9 s
II. Rotational latency
⇒360 rotations took 60 s
1 rotation → (60/360) s
1/2 rotation → (60/360) × (1/2) ⇒ 0.083 s
III. Transfer time
⇒1 Track can be read in one rotation, and 1 track has 512 sectors each having 512 bytes.
So, (512 × 512) bytes data can be read in one rotation time (1/6) s.
256 KB data read time = (1/6) s
1
1 KB data read time =
6 × 256
s
1
2 KB data read time = 2 ×
6 × 256
s
Problem 7.13: Consider an interleaving disk where the track is divided into eight sectors, each of size 2K. Seek time
is 30 ms and rotation rate of disk is 3600 rpm. If half rotation is required to place the read/write head at the start
of the sector, then
(a) How much time is required reading all eight sectors of the track using double-interleaving disk?
(b) What is the time difference to read all eight sectors if it is a non-interleaving disk?
(c) What is the data transfer rate using double-interleaving disk?
Solution:
(a) Time to read eight continuous sectors = (Seek time + rotational latency + transfer time)
I. Seek time = 30 ms
II. Rotational latency = (1/2) rotation
3600 rotations took 60 s
1 rotation → (60/3600) = 16.66 ms
1/2 rotation → (16.66) × (1/2) = 0.0083 = 8.33 ms
III. Transfer time
We have to read all of the eight sectors mean one whole track. 1 Track can be read in 2.75 rotations (double
interleaving).
So, transfer time of eight sectors = Transfer time of 1 track = 2.75 Rotation time
Time to read eight continuous sectors = (Seek time + rotational latency + transfer time)
= (Seek time + (1/2) rotation time + 2.75 rotation time)
= (Seek time + 3.25 rotation time)
={(30 + 3.25 × (16.66)} = 84.14 ms
(b) Time to read eight continuous sectors = (Seek time + rotational latency + transfer time)
I. Seek time = 30 ms
II. Rotational latency ⇒ 8.33 ms
III. Transfer time
We have to read all of the eight sectors means one whole track.
1 Track can be read in 1 rotation (non-interleaving).
So, transfer time of eight sectors = Transfer time of 1 track = 1 Rotation time
Time to read eight continuous sectors = (Seek time + rotational latency + transfer time)
= (Seek time + (1/2) rotation time + 1 rotation time)
= (Seek time + 1.5 rotation time)
= {(30 + 1.5 × (16.66)} = 55 ms
Time difference between double interleaving and non-interleaving = (84.14 − 55)
= 29.14 ms
(c) Transfer rate = Number of bytes transferred in one unit time (1 s)
2.75 Rotation time → 16 KB
60 s = 3600 rotations
1 s = 3600/60 = 60 rotations
2.75 rotation = 16 KB data
1 rotation = (16/2.75) KB
60 rotations = {(16/2.75) × 60} KB ≈ 349 KB
So, transfer rate ≈ 349 Kbps
One of the responsibilities of the operating system is to The simplest form of the disk scheduling is, of course,
use the hardware efficiently, which means that we should the first-come, first-served (FCFS) algorithm. This algo-
have fast disk access time and a broader disk bandwidth. rithm is intrinsically fair, but it generally does not pro-
Whenever a process needs I/O to or from the disk, it vide the fastest service.
issues a system call to the operating system. The request
specifies several pieces of information, such as:
7.10.2.2 SSTF Scheduling
1. Whether this operation is input or output?
2. What the disk address for the transfer is? A variation of shortest job first (SJF), applied in
3. What the memory address for the transfer is? CPU scheduling. It serves the request that has a
4. What the number of bytes to be transferred is? minimum seek time from the current head position.
If the desired disk drive and controller are available, To calculate the minimum seek time, we look for the
the request can be serviced immediately. If the driver or minimum number of cylinders to be traversed. The
controller is busy, any new requests for service will be major limitation is – starvation, as encountered in
placed on the queue of pending requests for that device. SJF algorithm.
We require disk-scheduling algorithms to service these
pending requests.
79 90
13
11
63
11
7
Figure 7.36 | FCFS disk-scheduling algorithm. Figure 7.37 | SSTF disk-scheduling algorithm.
Total head movement = (53~88) + (88~83) +
(83~7) + (13~7) + (99~13) + (79~90) + (11~79) Total head movement = (53~63) + (63~79) +
+ (11~63) (79~83) + (83~88) + (88~90) + (90~13) + (13~11)
= 35 + 5 + 76 + 6 + 86 + 11 + 68 + 52 + (11~7)
= 339 cylinders = 10 + 4 + 4 + 5 + 2 + 77 + 2 + 4 = 108 cylinders
7.10.2.3 SCAN Scheduling is that the requests are served in any one direction of
the motion of the head, not in both the directions as in
Also, called the elevator algorithm, here the disk arm SCAN. This can be compared by looking at the following
begins serving the requests at one end and moves to numerical problem.
the other end (it does not come back, as in earlier algo-
rithms) and then returns back serving the requests, simi-
lar to an elevator. This technique eventually results in Problem 7.17: Consider, the same problem, a disk
reduced variance as compared to SSTF. queue with requests for I/O to blocks on cylinders
88, 83, 7, 13, 90, 79, 11, 63
Problem 7.16: Consider, the same previous problem,
a disk queue with requests for I/O to block on The disk head is initially at cylinder 53 and the disk
cylinders arm is moving towards 0.
53 13
11
7
13
11
7 90
88
83
63
63 79
70
83
88 Figure 7.39 | C-SCAN disk-scheduling algorithm.
90
Total head movement = (53~13) + (13~11) +
Figure 7.38 | SCANdisk-scheduling algorithm. (11~7) + (7~0) + (0~99) + (99~90) + (90~88) +
(88~83) + (83~79) + (79~63)
Total head movement = (53~13) + (13~11) +
(11~7) + (7~0) + (0~63) + (63~79) + (79~83) + = 40 + 2 + 4 + 7 + 99 + 9 + 2 + 5 + 4 + 16
(83~88) + (88~90) = 188 cylinders
= 40 + 2 + 4 + 7 + 63 + 16 + 4 + 5 + 2
= 143 cylinders
7.10.2.5 LOOK and C-LOOK Scheduling
It should take into account the head movement from
cylinder 7 to 0 and 0 to 63 also. LOOK and C-LOOK are the variants of SCAN and
C-SCAN algorithm, respectively. Here the head does not
7.10.2.4 C-SCAN Scheduling move till the end of the cylinder, it only moves to the
farthest cylinder which requires to be served. After serv-
This algorithm treats the cylinders as a circular list, ing the farthest request, it changes the direction (in case
hence provides a uniform wait time as compared to the of LOOK) or jumps to another end (in case of C-LOOK).
SCAN algorithm discussed above. The major difference The same is illustrated below.
63
79 7.10.2.6 Selection of a Disk-Scheduling
83 Algorithm
88
90 Given so many disk-scheduling algorithms, how do we
that the system is free from unauthorised access or `adversaries’, and are capable of deploying active attacks
manipulation). as well as passive attacks.
3. Availability: It means the timely access to user
(for which he has the rights). 7.11.2.3 Accidental Data Loss
7.11.2 Security Environment Malicious intruders cause threats to the system. Hence,
valuable data can be lost by accident. The common
Security has many facets. Three of the most important causes of accidental data loss are as follows:
ones are the nature of the threats, the nature of intrud- 1. Hardware or software errors: CPU malfunc-
ers and accidental data loss. tions, unreadable disks or tapes, program bugs, etc.
There are two different kinds of attack. A `Passive 2. Human errors: Incorrect data entry, wrong tape
Attack’ does not affect the system resources but observes or disk mounted, wrong program run, lost disk or
the various acts of the system (such as — trying to know tape, etc.
the password by looking over the shoulders of a user), 3. Natural disaster: Fires, floods, earthquakes, wars,
whereas in an `Active Attack’ the system resources are riots, or rats gnawing tapes or floppy disks, etc.
affected (such as — an unauthorised person deleting a file).
7.11.3 Cryptography
7.11.2.1 Threats
First we will discuss about cryptography, in brief, to
From a security perspective, computer systems have understand more about security.
three general goals, with corresponding threats to them
(see Table 7.5): The aim of cryptography is to encrypt plaintext (mes-
Encryption Decryption
Figure 7.42 | Mechanism of cryptography.
7.11.4 Attacks from within the System Viruses can be divided into different classes based
upon the following characteristics:
When an adversary enters into a system, he or she can 1. Environment
start doing damage. The damage is of the following 2. Operating system (OS)
types: 3. Destructive capabilities
1. Trojan horses: Trojan horse is a program, which
contains a code hidden within a block of code, used to (Not to forget: there also exist other `harmful’ programs
modify/delete/encrypt the user’s data (functioning is or so called `malware’, such as Trojan horses).
similar to the Trojan horse, we read in history). The 1. Environment: Viruses can be described as:
Trojan horse cannot be differentiated from a normal •• File viruses: Most common virus that infects
code of program by reading the source code of the the executable (*.exe) files.
program housing the Trojan horse. •• Boot viruses: The virus infects the boot sector
2. Login spoofing: Related to Trojan horse, it is a program, the contents are replaced by infected
technique to obtain a user’s password. The user is code. Nowadays, modern OS are capable of
provided with the same lookalike page, as his login thwarting the attack by such kind of virus.
and he is unable to differentiate between the actual •• Macro viruses: Macros are defined as a group
page and this page; so he enters his login password of lines, technically speaking a subroutine,
and the vital information does not remain private. which is capable of infecting documents like —
3. Logic bombs: Similar to a bomb, this code explodes MSOffice, that make use of OLE (Object Link-
(causes destruction) when certain condition(s) are ing and Embedding) formats.
met like — a particular day of time or a particular •• Network viruses: These viruses make use of
date or when the user is running a particular file. protocols and commands of computer network
Such a code is embedded in a legitimate program, as or email to spread themselves in the system.
in a Trojan horse. These codes can modify or delete a
There is a large number of combinations possible,
file or can even cause a system to halt or do any other
for example, file-boot viruses infecting both files
damage.
and boot sectors on the disks. Another example of
4. Trap doors: These are also small programs embed-
the combo — network macrovirus, not only infect
ded in the programs to quickly gain access at a later
the documents which are being edited, but also
time. These are also known as `backdoors’. It is very
send copies of itself by email.
difficult to block these backdoors by an OS.
5. Buffer overflow: It is a program written to block 2. Mode of Operation: Viruses can be described
memory or buffer. It allows adversary to modify por- as follows:
tions of the target process address space. •• TSR: A terminate-and-stay-resident (TSR)
virus infects a computer and remains in RAM
and then performs an action for which it was
7.11.5 Attacks from Outside the System
planted. The virus becomes a part of the OS
In the previous sections, we have discussed about the and remains active always. In contrast a non-
threats largely caused by users already (logged) in the resident virus is active for a limited time only.
system; however, for machines connected to the network, •• Stealth algorithm: These viruses do not
there are external threats too. reveal their identity.
•• Self-encrypting and polymorphic: Such
viruses are not detected by antivirus as they
7.11.5.1 Virus Damage Scenarios do not have a fixed signature. They are able
to change themselves with every infection, and
A virus is a program that can do anything a program can
therefore, are very hard to detect.
do. It replicates itself, infects user’s data or files, stops
the operation of the system or functioning of the system. 3. Destructive capabilities: Viruses can be
It gets its name because of the similarities in its func- described as follows:
tionality to a biological virus. The common acronym for •• Harmless: Do not affect the computing
virus is Vital Information and Resources Under Siege. resources.
Nowadays, there are certain programs known as `Kit’, •• Not dangerous: Have a limited effect on com-
capable of generating new viruses automatically. Some puting resources. For example, a certain sound
of the well-known anti-virus softwares are: Symantec, can be produced after the computer is ON for
McAfee, AVG, Kaspersky, etc. say 2 hours.
•• Dangerous: They disrupt the normal work- 2. Identify: Identify the virus (may be your antivi-
ing of computing resources, such as you might rus scanner might tell you).
not be able to read from the USB port of the 3. Remove: All the traces of the virus are removed
computer. (disinfected by the antivirus scanner).
•• Very dangerous: Certain viruses result in
loss of data, such as erasing system files which The antivirus scanners use different approaches to
required when a system is to be booted, so as a protect the system, some of these approaches are:
result, the system does not work (boot). 1. Signature scanning: Each virus has a certain
signature (a code), which is located and deleted.
2. Heuristic scanning: Makes use of heuristics
7.11.6 Anti Virus Approaches
(rules of thumb or a guess) based upon certain
analysis.
The best approach is the protection against the virus. If
3. Generic decryption: The virus decrypts itself
at all the system has been infected by virus, the follow-
after the execution of code.
ing steps are commonly used:
4. Behaviour blocking: Its function is similar to
1. Detect: Locate it (by checking the file size of vital a firewall, it blocks the execution of a suspicious
files). piece of code.
IMPORTANT FORMULAS
SOLVED EXAMPLES
12. If the disk is rotating at 3600 rpm, determine the (a) 0 and 0 (b) 0 and 1
effective data transfer rate which is defined as the (c) 1 and 0 (d) 1 and 1
number of bytes transferred per second between Solution: From the give code, we get P(s) and
disk and memory. (Given size of track = 512 bytes) V(s) decrements and increment the value of sema-
(a) 20Kbps (b) 30 Kbps phore, respectively. So, from the given conditions,
(c) 40Kbps (d) 50Kbps we conclude that to avoid mutual exclusion the
value of Xb should be 1 and that of Yb should
Solution: 3600 rotation → 60 s
be 0.
1 rotation → 60/3600 = 1/60 s Ans. (c)
Transfer rate = (60 × track size) per second 15. Consider the following table of arrival time and
= 60 × 512 bytes/s burst time for three processes P0, P1 and P2.
= 30 Kbps
Ans. (b)
Process Arrival Time Burst Time
13. Which of the following features will characterize an
OS as multiprogrammed OS? P0 0 9
1. More than one program may be loaded into P1 1 4
main memory at the same time.
2. If a program waits for certain event, another P2 2 9
program is immediately scheduled.
3. If the execution of a program terminates, The pre-emptive shortest job first scheduling
another program is immediately scheduled. algorithm is used. Scheduling is carried out only
(a) Only at arrival or completion of process. The average
(b) (1) and (2) only waiting time for the three processes is ———.
(c) (1) and (3) only Solution: Waiting time for P0 = 5 − 1 = 4 s
(d) (1), (2) and (3) only Waiting time for P1 = 1 − 1 = 0 s
Solution: Multiprogrammed OS performs all the Waiting time for P2 = 13 − 2 = 4 s
given three tasks. Average waiting time = 4 + 0 + 11/3 = 5 s
Ans. (d) Ans. (5)
1. Using a larger block size in a fixed block size file following scheduling strategies will result in the
system leads to least CPU utilization (over a long period of time)
for this system?
(a) better disk throughput but poorer disk space
utilization. (a) First come first served scheduling
(b) better disk throughput and better disk space (b) Shortest remaining time first scheduling
utilization. (c) Static priority scheduling with different priori-
(c) poorer disk throughput but better disk space ties for the two processes
utilization. (d) Round robin scheduling with a time quantum
(d) poorer disk throughput and poorer disk space of 5 ms
utilization. (GATE 2003: 2 Marks)
(GATE 2003: 1 Mark)
Solution: Round robin will result in least cpu uti-
Solution: Larger block size in a fixed block size lization for time slice = 5 ms
file system reduces the number of disk access, but Consider process A and B, both process have 10 ms
at the same time increases chances of internal cpu utilization and then 90 ms I/O. Sequence will
fragmentation. be ABAB. After 15 ms process, A can do I/O and
Ans. (a) I/O can be done in parallel; therefore, A finishes
2. In a system with 32-bit virtual addresses and 1-KB I/O at 105th ms and B finishes I/O at 110th ms so
page size, use of one-level page tables for virtual cpu remains idle from 20th to 105th ms leading to
to physical address translation is not practical a total of 85 ms.
because of Ans. (d)
(a) the large amount of internal fragmentation. 4. Which of the following will always leads to an
(b) the large amount of external fragmentation. output starting with `001100110011’?
(c) the large memory overhead in maintaining page (a) P (S) at W, V(S) at X, P(T) at Y, V(T) at Z, S
tables. and T initially 1
(d) the large computation overhead in the transla- (b) P(S) at W, V(T) at X, P(T) at Y, V(S) at Z, S
tion process. initially 1, and T initially 0
(GATE 2003: 1 Mark) (c) P(S) at W, V(T) at X, P(T) at Y, V(S) at Z, S
Solution: Page size = 210 bytes and T initially 1
Total number of pages required = 232/210 = 222, (d) P(S) at W, V(S) at X, P(T) at Y, V(T) at Z, S
which is very large. initially 1, and T initially 0
There will be overhead in maintaining page tables. (GATE 2003: 2 Marks)
Ans. (c) Solution: To print `001100110011’. Process P
3. A uni-processor computer system only has two pro- must start first and process Q must wait until P
cesses, both of which alternate 10 ms CPU bursts prints 00 then process P must wait until process Q
with 90 ms I/O bursts. Both the processes were prints 11 and so on.
created at nearly the same time. The I/O of both In all of the four options P(T) at Y is given,
processes can proceed in parallel. Which of the so process Q have to waits for T and T must be
initialize to 0. Process P must signal T (V(T)) at 7. Which of the following will always lead to an
X. similarly in all four option P(S) is given at W so output staring with `001100110011’?
S must be initially 1 and Process Q signal S at Z to
(a) P (S) at W, V(S) at X, P(T) at Y, V(T) at Z, S
print desired output.
and T initially 1
Ans. (b)
(b) P(S) at W, V(T) at X, P(T) at Y, V(S) at Z, S
Common Data Questions 5 and 6: A processor initially 1, and T initially 0
uses two-level page tables for virtual to physical (c) P(S) at W, V(T) at X, P(T) at Y, V(S) at Z, S
address translation. Page tables for both levels are and T initially 1
stored in the main memory. Virtual and physical (d) P(S) at W, V(S) at X, P(T) at Y, V(T) at Z, S
addresses are both 32 bits wide. The memory is initially 1, and T initially 0
byte addressable. For virtual to physical address
Solution: To get output 001100110011, P and Q
translation, the 10 most significant bits of the vir-
should be executed alternatingly. P(S) at W, V(T)
tual address are used as index into the first-level
at X, P(T) at Y, V(S) at Z, S initially 1, and T
page table while the next 10 bits are used as index
initially 0 will give the correct output.
into the second-level page table. The 12 least sig-
nificant bits of the virtual address are used as offset Ans. (b)
within the page. Assume that the page table entries
8. Consider the following statements with respect to
in both levels of page tables are 4 bytes wide.
user-level threads and kernel-supported threads
Further, the processor has a translation look-
aside buffer (TLB), with a hit rate of 96%. The (i) Context switch is faster with kernel-supported
TLB caches recently used virtual page numbers threads.
and the corresponding physical page numbers. The (ii) For user-level threads, a system call can block
processor also has a physically addressed cache the entire process.
with a hit rate of 90%. Main memory access time (iii) Kernel-supported threads can be scheduled
is 10 ns, cache access time is 1 ns, and TLB access independently.
time is also 1 ns. (iv) User-level threads are transparent to the kernel.
5. Assuming that no page faults occur, the average Which of the above statements are true?
time taken to access a virtual address is approxi- (a) (ii), (iii) and (iv) only
mately (to the nearest 0.5 ns) (b) (ii) and (iii) only
(a) 1.5 ns (b) 2 ns (c) (i) and (iii) only
(c) 3 ns (d) 4 ns (d) (i) and (ii) only
(a) 50% (b) 40% 12. Consider a system with a two-level paging scheme
(c) 25% (d) 0% in which a regular memory access takes 150 ns,
and servicing a page fault takes 8 ms. An aver-
(GATE 2004: 1 Mark)
age instruction takes 100 ns of CPU time, and two
Solution: I/O performance is determined by memory accesses. The TLB hit ratio is 90%, and
input and output devices. Disk scheduling will not the page fault rate is one in every 10000 instruc-
improve the performance of I/O for users. tions. What is the effective average instruction
Ans. (d) execution time?
10. The minimum number of page frames that must be (a) 645 ns (b) 1050 ns
allocated to a running process in a virtual memory (c) 1215 ns (d) 1230 ns
environment is determined by
(GATE 2004: 2 Marks)
(a) the instruction set architecture.
(b) page size. Solution: Total memory access time = 0.90 ×
(c) physical memory size. 150 + 0.10 × 300 = 165 ns
(d) number of processes in memory. Time taken to execute instructions = 100 + 2(165
+ 5000 × 8 × 106) = 1230 ns
(GATE 2004: 1 Mark) Ans. (d)
Solution: The minimum number of page frames
13. Consider two processes P1 and P2 accessing the
required by a process is based on instruction set
shared variables X and Y protected by two binary
architecture.
semaphores Sx and Sy, respectively, both initialized
Ans.(a)
to 1. P and V denote the usual semaphore opera-
11. Consider the following set of processes, with the tors, where P decrements the semaphore value, and
arrival times and the CPU-burst times given in V increments the semaphore value. The pseudo-
milliseconds. codes of P1 and P2 are as follows:
32 bits, and 48-bit integers are used. What is the operated in cycle stealing mode whereby whenever
maximum possible file size? one 4 byte word is ready it is sent to memory;
similarly, for writing, the disk interface reads a 4
(a) 224 bytes (b) 232 bytes
byte word from the memory in each DMA cycle.
(c) 234 bytes (d) 248 bytes
Memory cycle time is 40 nsec. The maximum per-
(GATE 2004: 2 Marks) centage of time that the CPU gets blocked during
DMA operation is:
Solution: Number of disk block pointers in one
(GATE 2005: 2 Marks)
block = 210 byte/32 bit = 256
256 × 256 × 256 × 1KB
Maximum size of file = (a) 10 (b) 25 (c) 40 (d) 50
= 234 bytes Solution: Data transfer in one rotation = 512 ×
Ans. (c) 1024 Bytes
15. What is the swap space in the disk used for? 1 rotation takes = 60/3000 s
(a) Saving temporary html pages 512KB is transferred in = 60/3000 s
(b) Saving process data
60
(c) Storing the super-block 1 byte will be transferred =
(d) Storing device drivers 3000 × 512 × 1024
Solution: To remain in safe state, the maximum Solution: Fork function returns 0 for child pro-
resource requirement must be less than m + n. cess and it gives Process id of child to parent pro-
Ans. (c) cess. Parent process executes else part of code. So
u = a − 5 for parent and for child x = a + 5.
18. Consider a disk drive with the following specifica-
Therefore, u + 10 = x. As there are two copies of a
tions:
is created, so they have different memory locations.
16 surfaces, 512 tracks/surface, 512 sectors/track, Hence, v ! = y.
1 KB/sector, rotation speed 3000 rpm. The disk is Ans. (d)
Linked Answer Questions 20 and 21: We are given any intervening access to the memory location x.
9 tasks T1, T2, … T9. The execution of each task Consider the following implementation of P and V
requires one unit of time. We can execute one task functions on a binary semaphore S.
at a time. Each task Ti has a profit Pi and a dead-
line di. Profit Pi is earned if the task is completed void P (binary_semaphore *S)
before the end of the (di)th unit of time. {
unsigned y;
Tank T1 T2 T3 T4 T5 T6 T7 T8 T9 unsigned *x = &(S->value);
Profit 15 20 30 18 18 10 23 16 25 do
Deadline 7 2 5 3 4 5 2 7 3 {
fetch-and-set x, y;
} while (y);
20. Are all tasks completed in the schedule that gives
}
maximum profit?
void V (binary_semaphore *S)
(a) All tasks are completed {
(b) T1 and T6 are left out S->value = 0;
(c) T1 and T8 are left out }
(d) T4 and T6 are left out
Which one of the following is true?
(GATE 2005: 2 Marks)
(a) The implementation may not work if context
Solution: switching is disabled in P.
T2 T7 T9 T5 T3 T8 T1 (b) Instead of using fetch-and-set, a pair of normal
load/store can be used.
T4 and T6 are left out. (c) The implementation of V is wrong.
Ans. (d) (d) The code does not implement a binary
semaphore.
21. What is the maximum profit earned?
(GATE 2006: 2 Marks)
(a) 147 (b) 165 (c) 167 (d) 175
Solution: If context switching is disabled, this
(GATE 2005: 2 Marks) implementation may not work. Then processes will
Solution: Maximum profit = 20 + 23 + 25 + 18 not be synchronized.
+ 30 + 15 + 16 = 147 Ans. (a)
Ans. (a)
24. A CPU generates 32-bit virtual addresses. The
22. Consider three CPU-intensive processes, which page size is 4 KB. The processor has a translation
require 10, 20 and 30 time units and arrive at look-aside buffer (TLB) which can hold a total of
times 0, 2 and 6, respectively. How many context 128 page table entries and is 4-way set associative.
switches are needed if the operating system imple- The minimum size of the TLB tag is
ments a shortest remaining time first scheduling
algorithm? Do not count the context switches at (a) 11 bits (b) 13 bits
time zero and at the end. (c) 15 bits (d) 20 bits
designers decide to get rid of the virtual memory as possible. For what percentage of time does the
entirely. Which one of the following is true? CPU remain idle?
(a) Efficient implementation of multi-user support (a) 0% (b) 10.6%
is no longer possible. (c) 30.0% (d) 89.4%
(b) The processor cache organization can be made (GATE 2006: 2 Marks)
more efficient now.
(c) Hardware support for memory management is Solution: Let three processes be p0, p1 and p2.
no longer needed. Their execution time is 10, 20 and 30, respectively.
(d) CPU scheduling can be made more efficient now. p0 spends first 2 time units in I/O, 7 units of CPU
(GATE 2006: 2 Marks) time and finally 1 unit in I/O. p1 spends first
4 units in I/O, 14 units of CPU time and finally
Solution: Special hardware support is required
2 units in I/O. p2 spends first 6 units in I/O, 21
for address translation in case of virtual addressing.
units of CPU time and finally 3 units in I/O.
Ans. (c)
26. Consider three processes (process id 0, 1, 2, respec- Ideal P0 P1 P2 ideal
tively) with compute time bursts 2, 4 and 8 time 0 2 9 23 44 47
units. All processes arrive at time zero. Consider
Total time spent = 47
the longest remaining time first (LRTF) schedul-
Idle time = 2 + 3 = 5
Percentage of idle time = (5/47) × 100 = 10.6%
ing algorithm. In LRTF, ties are broken by giving
priority to the process with the lowest process id.
The average turnaround time is Ans. (b)
(a) 13 units (b) 14 units 28. Consider the following snapshot of a system run-
(c) 15 units (d) 16 units ning n processes. Process i is holding xi instances of
a resource R, 1 n. Currently, all instances of R are
(GATE 2006: 2 Marks) occupied. Further, for all i, process i has placed a
Solution: request for an additional yi instances while holding
the xi instances it already has. There are exactly
Process Burst Remaining Turnaround two processes p and q such that yp = yq = 0. Which
Time Time Time one of the following can serve as a necessary condi-
P0 2 2−1=1 12 tion to guarantee that the system is not approach-
ing a deadlock?
4 − 1 = 3 − 1 = 13
(a) min (xp, xq) < maxk ≠ p,qyk
P1 4
2−1=1
(b) xp+ xq ³≥ mink ≠ p,qyk
P3 8 8 − 4 = 4 − 1 = 14 (c) max (xp, xq) > 1
3 − 1 = 2 − 1=1 (d) min (xp, xq) > 1
(GATE 2006: 2 Marks)
P2 P1 P2 P1 P2 P0 P1 P2 P0 P1 P2 Solution: Processes p and q have sufficient
resources so they do not require any additional
0 4 5 6 7 8 9 10 11 12 13 14
resources and both of them can complete and
Average turnaround time = 12 + 13 + 14 = 39/3 release all resources acquired by them which are (xp
= 13 + xq). If the resources released by process p and q
Ans. (a) are sufficient for another process which is waiting
for yk resources, then system will not be in deadlock.
27. Consider three processes, all arriving at time zero,
Ans. (b)
with total execution time of 10, 20 and 30 units,
respectively. Each process spends the first 20% of Linked Answer Questions 29 and 30: Barrier is a
execution time doing I/O, the next 70% of time synchronization construct where a set of processes
doing computation, and the last 10% of time doing synchronizes globally, that is, each process in the
I/O again. The operating system uses a shortest set arrives at the barrier and waits for all others
remaining compute time first scheduling algorithm to arrive and then all processes leave the barrier.
and schedules a new process either when the run- Let the number of processes in the set be three and
ning process gets blocked on I/O or when the run- S be a binary semaphore with the usual P and V
ning process finishes its compute burst. Assume functions. Consider the following C implementa-
that all I/O operations can be overlapped as much tion of a barrier with line numbers shown on left.
Solution: Kernel-level threads are managed by kernel. (a) Both P and Q are true, and Q is the reason for P.
Blocking one thread may not block other threads. (b) Both P and Q are true, but Q is not the reason
Ans. (d) for P.
34. An operating system uses shortest remaining time (c) P is false, but Q is true.
first (SRT) process scheduling algorithm. Consider (d) Both P and Q are false.
the arrival times and execution times for the fol- (GATE 2007: 2 Marks)
lowing processes:
Solution: P: True, it is Belady’s anomaly.
Process Execution Time Arrival Time Q: True, but not reason for Belady’s anomaly.
Ans. (b)
P1 20 0
36. A single processor system has three resource types
P2 25 15 X, Y and Z, which are shared by three processes.
P3 10 30 There are 5 units of each resource type. Consider
P4 15 45 the following scenario, where the column alloc
denotes the number of units of each resource type
allocated to each process, and the column request
What is the total waiting time for process P2?
denotes the number of units of each resource type
(a) 5 (b) 15 requested by a process in order to complete execu-
(c) 40 (d) 55 tion. Which of these processes will finish LAST?
(GATE 2007: 2 Marks) alloc request
Solution:
X Y Z X Y Z
P1 P2 P3 P2 P4
P0 1 2 1 1 0 3
0 20 30 40 55 70 P1 2 0 1 0 1 2
Waiting time of P2 = C ompletion time − (Arrival P2 2 2 1 1 2 0
time + Execution time)
= 55 − (15 + 25) (a) P0
= 15 (b) P1
Ans. (b) (c) P2
35. A virtual memory system uses first in first out (d) None of the above, since the system is in a
(FIFO) page replacement policy and allocates a deadlock
fixed number of frames to a process. Consider the (GATE 2007: 2 Marks)
following statements:
Solution: Available resources: 0 1 2
P: Increasing the number of page frames allocated P1’s request can be fulfilled, it will start executing.
to a process sometimes increases the page fault rate. Available after P1 execution: 2 1 3
Q: Some programs do not exhibit locality of P0’s request can be fulfilled and then P2 will execute.
reference. P2 will finish last.
Which one of the following is TRUE? Ans. (c)
37. Two processes, P1 and P2, need to access a critical section of code. Consider the following synchronization con-
struct used by the processes:
In process P1 and P2, variables wants1 and wants2 both become true, but process P1 and P2 are stuck for infinite time
in their while loops waiting for each other to finish. So, they do not prevent deadlock. But mutual exclusion is ensured.
P1 P2
Which one of the following statements is TRUE 40. The data blocks of a very large file in the Unix file
about the above construct? system are allocated using
(a) It does not ensure mutual exclusion. (a) contiguous allocation.
(b) It does not ensure bounded waiting. (b) linked allocation.
(c) It requires that processes enter the critical sec- (c) indexed allocation.
tion in strict alternation. (d) an extension of indexed allocation
(d) It does not prevent deadlocks, but ensures
(GATE 2008: 1 Mark)
mutual exclusion.
(GATE 2007: 2 Marks) Solution: i-node is used in Unix file system.
Ans.(d)
Solution: When wants1 and wants2 both become
true, both processes will be in wait state. But 41. For a magnetic disk with concentric circular tracks,
mutual exclusion is ensured. the seek latency is not linearly proportional to the
Ans. (d) seek distance due to
Linked Answer Questions 38 and 39: A process has (a) non-uniform distribution of requests.
been allocated 3-page frames. Assume that none (b) arm starting and stopping inertia.
of the pages of the process are available in the (c) higher capacity of tracks on the periphery of
memory initially. The process makes the following the platter.
sequence of page references (reference string): 1, 2, (d) use of unfair arm scheduling policies.
1, 3, 7, 4, 5, 6, 3, 1.
(GATE 2008: 1 Mark)
38. If optimal page replacement policy is used, how
many page faults occur for the above reference Solution: For a magnetic disk with concentric
string? circular tracks, the seek latency is not linearly pro-
portional to the seek distance due to arm starting
(a) 7 (b) 8
and stopping inertia.
(c) 9 (d) 10
Ans. (b)
(GATE 2007: 2 Marks)
Solution: 42. In an instruction execution pipeline, the earliest
Using optimal page replacement: page faults = 7 that the data TLB (Translation Lookaside Buffer)
can be accessed is
1
(a) before effective address calculation has started.
2 → 7 → 4 → 5 →6 (b) during effective address calculation.
3 (c) after effective address calculation has
Ans. (a) completed.
39. Least recently used (LRU) page replacement (d) after data cache lookup has completed.
policy is a practical approximation to optimal page (GATE 2008: 2 Marks)
replacement. For the above reference string, how
many more page faults occur with LRU than with Solution: For effective address calculation, TLB
the optimal page replacement policy? can be accessed earliest.
Ans. (b)
(a) 0 (b) 1
(c) 2 (d) 3 43. The P and V operations on counting semaphores,
(GATE 2007: 2 Marks) where s is a counting semaphore, are defined as
follows:
Solution:
Using LRU: page faults = 9 P(s) : s = s - 1;
if s < 0 then wait;
1→4→3
V(s) : s = s + 1;
2→7→6 if s <= 0 then wakeup a
process waiting on s;
3→5→1
9 − 7 = 2 more page faults when LRU is used. Assume that Pb and Vb the wait and signal opera-
Ans. (c) tions on binary semaphores are provided. Two
binary semaphores xb and yb are used to imple- 45. Which of the following is NOT true of deadlock
ment the semaphore operations P(s) and V(s) as prevention and deadlock avoidance schemes?
follows:
(a) In deadlock prevention, the request for resources
P(s) : Pb (xb); is always granted if the resulting state is safe.
s = s -1; (b) In deadlock avoidance, the request for resources
if (s < 0) { is always granted if the result state is safe.
Vb (xb); (c) Deadlock avoidance is less restrictive than
Pb (yb); deadlock prevention.
} (d) Deadlock avoidance requires knowledge of
else Vb (xb); resource requirements a priori.
(GATE 2008: 1 Mark)
V(s) : Pb (xb); Solution: Deadlock avoidance scheme is less
s = s + 1; restrictive than deadlock prevention because in
if (s <=0) Vb (yb); the latter, the request for a resource may not be
Vb (xb); granted even if the resulting state is safe.
Ans. (c)
The initial values of xb and yb are, respectively,
46. A process executes the following code
(a) 0 and 0 (b) 0 and 1
(c) 1 and 0 (d) 1 and 1 for (i = 0; i < n; i + +) for ( );
48. In which one of the following page replacement Which one of the following statements is TRUE
policies, Belady’s anomaly may occur? if all three processes run concurrently starting at
time t = 0?
(a) FIFO (b) Optimal
(c) LRU (d) MRU (a) All processes will finish without any deadlock.
(b) Only P1 and P2 will be in deadlock.
(GATE 2009: 1 Mark) (c) Only P1 and P3 will be in a deadlock.
(d) All three processes will be in deadlock.
Solution: Belady’s anomaly refers to the phenom-
enon where increasing the number of page frames (GATE 2009: 2 Marks)
results in increase in the number of page faults. Solution: Apply deadlock detection algorithmand
This phenomenon is experienced when FIFO page see that there is no process waiting indefinitely for
replacement algorithm is used. a resource.
Ans. (a) Ans. (a)
49. The essential content(s) in each entry of a page 51. Consider a disk system with 100 cylinders. The
table is/are requests to access the cylinders occur in the follow-
(a) virtual page number ing sequence:
(b) page frame number 4, 34, 10, 7, 19, 73, 2, 15, 6, 20
(c) both virtual page number and page frame Assuming that the head is currently at cylinder 50,
number what is the time taken to satisfy all requests if it
(d) access right information takes 1ms to move from one cylinder to adjacent
(GATE 2009: 1 Mark) one and shortest seek time first policy is used?
Solution: (a) 95 ms (b) 119 ms
Page frame number is must in page table. (c) 233 ms (d) 276 ms
Ans. (b) (GATE 2009: 2 Marks)
50. Consider a system with four types of resources R1 Solution: 4, 34, 10, 7, 19, 73, 2, 15, 6, 20
(3 units), R2 (2 units), R3 (3 units), R4 (2 units). A Since shortest seek time first policy is used, head
non-preemptive resource allocation policy is used. will first move to 34. This move will cause 16 × 1
At any given instance, a request is not entertained ms. After 34, head will move to 20 which will cause
if it cannot be completely satisfied. Three processes 14 × 1 ms and so on. So, cylinders are accessed in
P1, P2, P3 request the sources as follows if executed the following order 34, 20, 19, 15, 10, 7, 6, 4, 2, 73
independently. and total time will be
taken to switch between two processes be t2. Which The pre-emptive shortest job first scheduling
of the following is TRUE? algorithm is used. Scheduling is carried out only
(a) t1 > t2 at arrival or completion of processes. What is the
(b) t1 = t2 average waiting time for the three processes?
(c) t1 < t2 (a) 5.0 ms (b) 4.33 ms
(d) Nothing can be said about the relation between (c) 6.33 ms (d) 7.33 ms
t1and t2
(GATE 2011: 2 Marks)
(GATE 2011: 1 Mark)
Solution:
Solution: Process switching requires more time. P0 P1 P0 P2
Ans. (c)
0 1 5 13 22
62. A thread is usually defined as a `light weight pro-
cess’ because an operating system (OS) maintains Waiting time
smaller data structures for a thread than for a pro- P0 = 5 − 1 = 4 ms
cess. In relation to this, which of the following is P1 = 1 − 1 = 0 ms
TRUE? P2 = 13 − 2 = 11 ms
(a) On per-thread basis, the OS maintains only Average waiting time = 4 + 11/3 = 5 ms
CPU register state. Ans. (a)
(b) The OS does not maintain a separate stack for 65. An application loads 100 libraries at startup.
each thread. Loading each library requires exactly one disk
(c) On per-thread basis, the OS does not maintain access. The seek time of the disk to a random loca-
virtual memory state. tion is given as 10 ms. Rotational speed of disk
(d) On per-thread basis, the OS maintains only is 6000 rpm. If all 100 libraries are loaded from
scheduling and accounting information. random locations on the disk, how long does it take
to load all libraries? (The time to transfer data
(GATE 2011: 1 Mark)
from the disk block once the head has been posi-
Solution: Operating System maintains separate tioned at the start of the block may be neglected.)
stack, register and thread-specific data for differ-
ent threads. OS does not maintain virtual memory (a) 0.50 s (b) 1.50 s
state. So option (c) is correct. (c) 1.25 s (d) 1.00 s
Ans. (c) (GATE 2011: 2 Marks)
63. Let the page fault service time be 10 ms in a com- Solution: Since transfer time can be neglected,
puter with average memory access time being 20 the average access time is sum of average seek
ns. If one page fault is generated for every 106 time and average rotational latency. Average seek
memory accesses, what is the effective access time time for a random location time is given as 10 ms.
for the memory? The average rotational latency is half of the time
needed for complete rotation. It is given that 6000
(a) 21 ns (b) 30 ns rotations need 1 min. So one rotation will take
(c) 23 ns (d) 35 ns 60/6000 s, which is 10 ms. Therefore, average rota-
(GATE 2011: 1 Mark) tional latency is half of 10 ms, which is 5 ms.
Solution: p = page fault rate = 1/106 Average disk access time = S eek time + rotational
latency
Effective access time = p × page fault service time = 10 ms + 5 ms = 15 ms
+ (1 − p) × memory access time For 100 libraries, the average disk access time will
= 1/106 × 10 + [(1 − (1/106)] × 20 ≈ 30 ns be 15 × 100 ms.
Ans. (b) Ans. (b)
64. Consider the following table of arrival time and 66. A process executes the code
burst time for three processes P0, P1 and P2. fork();
fork();
Process Arrival Time Burst Time fork();
P0 0 ms 9 ms The total number of child processes created is
P1 1 ms 4 ms (a) 3 (b) 4
P2 2 ms 9 ms (c) 7 (d) 8
(GATE 2012: 1 Mark)
Process Y: Process Y:
Which one of the following represents the Solution: S = 2 (only two processes can run at
CORRECT implementations of ExitX and EntryY? the same time)
(A) (B) x=0
ExitX(R, S) { ExitX(R, S) {
W X Y Z
P(R); V(R);
V(S); V(S); P(S) P(S) P(S) P(S)
} } R(x) R(x) R(x) R(x)
EntryY (R, S) { EntryY(R, S) {
P(S); P(R);
x++ x++ x=x-2 x=x-2
V(R); P(S); W(x) W(x) W(x) W(x)
} }
V(S) V(S) V(S) V(S)
(C) (D)
ExitX(R, S) { ExitX(R, S) { Let X and Y are running:
P(S); V(R); X: R(x) → 0 Y: R(x) → 0
V(R); P(S); x++ = 1 x = x − 2 = −2
} } w(x) =1 w(x) = −2
EntryY(R, S) { EntryY(R, S) { Let X wrote after Y, so x will contain 1.
V(S); V(S); Now when W and Z will execute, W will increment
P(R); P(R); it to 2 and Z will decrement to −1, so maximum
} } possible values is 2.
Ans. (d)
(a) A (b) B
(c) C (d) D Common Data Questions 76 and 77: A computer
(GATE 2013: 2 Marks) uses 46-bit virtual address, 32-bit physical address,
Solution: P(S) is for wait, V(S) is for signal. In and a three-level paged page table organization.
option (c), mutual exclusion is ensured. Deadlock The page table base register stores the base address
also does not occur. Other options are leading either of the first-level table (T1), which occupies exactly
to deadlock or do not ensure mutual exclusion. one page. Each entry of T1 stores the base address
Ans.(c) of a page of the second-level table (T2). Each entry
of T2 stores the base address of a page of the third-
75. A shared variable x, initialized to zero, is operated
level table (T3). Each entry of T3 stores a page
on by four concurrent processes W, X, Y, Z as fol-
table entry (PTE). The PTE is 32 bits in size. The
lows. Each of the processes W and X reads x from
processor used in the computer has a 1 MB 16-way
memory, increments by one, stores it to memory,
set-associative virtually indexed physically tagged
and then terminates. Each of the processes Y and
cache. The cache block size is 64 bytes.
Z reads x from memory, decrements by two, stores
it to memory, and then terminates. Each process 76. What is the size of a page in KB in this computer?
before reading x invokes the P operation (i.e., wait)
(a) 2 (b) 4
on a counting semaphore S and invokes the V oper-
(c) 8 (d) 16
ation (i.e., signal) on the semaphore S after storing
x to memory. Semaphore S is initialized to two. (GATE 2013, 2 Marks)
What is the maximum possible value of x after all Solution: All the page table resides in the physi-
processes complete execution? cal memory (RAM) since the physical address is
(a) −2 (b) −1 (c) 1 (d) 2 32 bit addressable. Each level table holds a 32-bit
(GATE 2013: 2 Marks) (4-byte) address of the RAM.
Assume the size of a page is x bytes (which we to different sets in the processor cache of this
need to find). As the size of level-1 table is also x computer?
(as given in the question that level-1 table accom- (a) 2 (b) 4
modates exactly a page), it can hold x/4 base (c) 8 (d) 16
addresses of x/4 level-2 tables (which also accom-
modates exactly a page) and each level-2 table (GATE 2013: 2 Marks)
holds base addresses of x/4 level-3 tables. Solution:
The level-3 table has entities equal to 246/x Let page sixe is x bytes.
That is, (x/4)(x/4)(x/4) = 246/x Size of level 1 table is also x (given in the question).
On solving, x = 8192, which is in bytes so 8192/1024 It can hold x/4 base addresses of x/4 level 2 tables
= 8 KB and each level 2 table can hold base addresses of
x/4 level 3 tables.
Ans. (c) The level 3 table has entities equal to 246/x, that is,
77. Consider the same data as in the above question. (x/4)(x/4)(x/4) = 246/x
What is the minimum number of page colours On solving x = 8192 bytes = 8 K bytes
needed to guarantee that no two synonyms map Ans. (c)
PRACTICE EXERCISES
(a) 12.8 ms (b) 11.8 ms What will be the safety sequence of the above pro-
(c) 10.8 ms (d) 14 ms cesses to avoid the deadlock?
48e. If CPU scheduling policy is priority scheduling with (a) P0, P3, P1, P2 (b) P0, P1, P2, P3
pre-emption, then average waiting time will be (c) P0, P1, P3, P2 (d) P0, P2, P1, P3
(a) 19 ms (b) 7.6 ms 5. Assume the processes are arrived into the ready
(c) 8 ms (d) 14.2 ms queue in the correct order in the above question
at time to. What will be the average waiting time
using round-robin scheduling with a time quantum
Set 2 5 ms? (The CPU burst requirements of P0, P1, P2,
P3 are 5 ms, 10 ms, 7 ms and 8 ms, respectively.)
1. Which scheduling policy is most suitable for time-
shared operating systems? (a) 14.5 ms (b) 13 ms
(c) 12.5 ms (d) 15 ms
(a) Shortest job first
(b) Round robin 6. When the result of a computation depends on the
(c) First come first serve speed of the processes involved, there is said to be
(d) Elevator (a) Cycle stealing (b) Race condition
2. Four jobs to be executed on a single processor (c) A time lock (d) A deadlock
system arrive at time 0+ in the order A, B, C, D. 7. Each process Pi, I = 1 … 9 is coded as follows:
Their burst CPU time requirements are 4, 1, 8, 1
time units, respectively. The completion time of A Repeat
under round-robin scheduling with time slice of one P (mutex)
time unit is . {critical section}
V (mutex)
3. The sequence is an optimal non-preemptive sched-
uling sequence for the following jobs which leaves Forever
the CPU idle for unit(s) of time. The code uses P10 is identical except that it uses
V (mutex) in place of P (mutex). What is the larg-
Job Arrival Time Burst Time est no of processes that can be inside the critical
section at any moment?
1 0.0 9
(a) 1 (b) 2
2 0.6 5 (c) 3 (d) None of these
3 1.0 1 8. Consider Peterson’s algorithm for mutual exclusion
between two concurrent processes i and j. The pro-
(a) {3,2,1},1 gram executed by process is shown below:
(b) {2,1,3},0
Repeat
(c) {3,2,1},0
(d) {1,2,3},5 Flag[i] = true;
Turn=j;
4. The Banker’s algorithm for avoiding the deadlock
While (P) do no -op;
has got the following data structure, with four pro-
cesses P0,P1,P2,P3 and resourcetypes A, B, C and D. Enter critical section, perform actions,then
Then
Exit critical section
Process Alloca- Maxi- Available Flag[i]=false;
tion mum Perform other non-criticalsection actions.
ABCD ABCD ABCD Until false;
9. Consider an operating system containing `n’ pro- (a) R elease all resources before requesting a new
cesses each requiring four resources. The maximum resource.
number of processes that can be executed without (b) Number the resources uniquely and never
any deadlock if there are 18 resources is request a lower numbered resource than the
last one requested.
10. The average page fault service time in a system is (c) Never request a resource after releasing any
25 ms and memory access time is 220 ns. If page resource.
fault rate is 0.0004, what will be the effective access (d) Request and all required resources be allocated
time? before execution.
(a) 10 (b) 7.36 15. Consider the disk drive with the following
(c) 10.21 (d) 11.36 specification:
Eight surfaces, 1024 tracks/surface, 512 sectors/
11. Consider n processes sharing the CPU in a round- track, 4KB/sector, rotation speed is 3000 rpm and
robin fashion. Assuming that each process switch the disk is operated in burst mode. The processor
takes s seconds, what must be the quantum size runs at 600 MHz and takes 300 and 900 clock cycle
q such that the overhead resulting from process to initiate and complete DMA transfer, respec-
switching is minimized but at the same time each tively, if the size of transferred data is 20KB.
process is guaranteed to get its turn at the CPU at
least every t seconds? What is the size of the hard disk?
(a) 16 GB (b) 8 GB
t − ns t − ns
(a) q ≤ (b) q ≥ (c) 16 MB (d) 8 MB
n −1 n −1
16. What is the percentage of processor time consumed
t − ns t − ns
(c) q ≤ (d) q ≥ for the transfer operation in the above case?
n +1 n +1 (a) 0.69% (b) 27.77%
(c) 97% (d) 20%
12. A solution to the Dining Philosophers Problem
17. Consider a system having m resources of the same
which avoids deadlock is
type. These resources are shared by three processes
(a) Ensure that all philosophers pick up the left A, B and C which have peak demands of 3, 5 and
fork before the right fork 7, respectively. For what value of m deadlock will
(b) Ensure that all philosophers pick up the right not occur?
fork before the left fork (a) 13 (b) 14
(c) Ensure that one particular philosopher picks up (c) 15 (d) 16
the left fork before the right fork, and that all
18. A ROM is used to store the table for multiplication
other philosophers pick up the right fork before
of two 8-bit unsigned integers. The size of ROM
the left fork
required is
(d) None of these
(a) 256 × 16 (b) 64K × 8
13. A computer has six tape drives, with n processes (c) 4K × 16 (d) 64K × 16
competing for them. Each process may need two 19. Dirty bit for a page in a page table
drives. What is the maximum value of n for the
system to be deadlock free? (a) helps avoid unnecessary writes on paging device
(b) helps maintain LRU information
(a) 6 (b) 5 (c) allows only read on a page
(c) 4 (d) 4 (d) none of the above
14. Which of the following is NOT a valid deadlock 20. Consider the following heap (figure) in which blank
prevention scheme? regions are not in use and hatched regions are in use.
Increasing addresses
The sequence of requests for blocks of size 300, 25, (a) 7 (b) 8
125, 50 can be satisfied if we use (c) 9 (d) 10
(a) Either first-fit or best-fit policy(any one) 27. If number of frames is 3, then how many more
(b) First-fit but not best-fit policies page faults will occur using the same page reference
(c) Best-fit but not first-fit policy string?
(d) None of the above
(a) 0 (b) 1
21. The capacity of memory unit is defined by the (c) 2 (d) 3
number of words multiplied by the number of bits/
word. How many separate address and data lines 28. Which of the following disk-scheduling strategies is
are needed for a memory of 4K × 16? likely to give the best throughput?
(a) 10 address, 16 data lines (a) Farthest cylinder next
(b) 11 address, 8 data lines (b) Nearest cylinder next
(c) 12 address, 16 data lines (c) First come first serve
(d) 12 address, 12 data lines (d) Elevator algorithm
22. If an instruction takes i μs and a page fault takes an 29. In the index allocation scheme of blocks to a file,
additional j μs, the effective instruction time if on the maximum possible size of the file depends on
the average a page fault occurs every k instruction is
j (a) T he size of blocks, and the size of the address
(a) i + (b) i + j ∗ k of the blocks.
k
(b) The number of blocks used for the index, and
i+j
(d) (i + j) ∗ k
the size of the blocks.
(c)
k (c) The size of blocks, the number of blocks used
23. Suppose the time to service a page fault is on the for the index and the size of the address of the
average 10 ms, while a memory access takes 1 μs. blocks.
Then a 99.99% hit ratio results in average memory (d) None of the above
access time of 30. Suppose two jobs, each of which needs 10 min of
(a) 1.9999 ms CPU time, start simultaneously. Assume 50% I/O
(b) 1 ms wait time.
(c) 9.999 μs How long will it take for both to complete if they
(d) 1.9999 μs run sequentially?
24. Consider a process that has been allocated 3 page (a) 10 (b) 20
frames. Assume that system uses pure demand (c) 30 (d) 40
paging. While undergoing execution, the process
31. How long will it take if they run in parallel in the
makes the following sequence of page references
above case?
1,4,7,1,6,3,1,3,2,4,9,1.
(a) 10 (b) 20
If optimal page replacement policy is used, then
(c) 30 (d) 40
the number of page faults for the above reference
string is 32. A multilevel page table is preferred in comparison
to a single-level page table for translating virtual
(a) 7 (b) 6
address to physical address because
(c) 8 (d) None of these
(a) It reduces the memory access time for read and
25. If LRU is used for the same reference string in the
write.
above question then which of the following is true?
(b) It helps to reduce the size of page table needed
(a) Optimal has 22% lesser page faults than LRU to implement the virtual address space of a
(b) Optimal has 33% lesser page faults than LRU process.
(c) Optimal has 44% lesser page faults than LRU (c) It is required by translation look-aside buffer.
(d) None of these (d) It helps to reduce the number of page faults in
page replacement algorithms.
26. What is the number of page faults by least recently
used page replacements for a memory with 4 frames 33. A process executes the following code for(i = 0; i <
for the page reference string 2, 0, 1, 2, 4, 0, 5, 1, 4, n; i++) for ();the total number of child processes
6, 4, 2, 1, 3, 0? created is
Set 1
3. (a) In the first sequence (3, 2, 1), CPU is ideal for 10. (c) Effective access time = [(1 − p) × Memory
1 unit time because 3rd process arrives at time 1. access time + p × page fault service time]
Process with large burst time will not have to wait ⇒ [(1 − 0.0004) × 0.22 + 0.0004 × 2500] μs
more so CPU will not be ideal more time. ⇒10.219
11. (a) Next execution of the process should occur 24. (d)
within t seconds. Each process runs for q period, 1 7 7 1 6 3 1 3 2 4 9 1
and if there are n processes p1 p2 p3 … pn p1 p2 … 1 1 1 1 1 1 1 1 1 1 1 1
then p1 turn comes again when it has completed 4 4 4 4 4 4 4 4 4 9 9
time quanta for remaining process p2 to pn, that is, 7 7 6 3 3 3 2 2 2 2
it would take at most (n − 1)q time. F F F H F F H H F H F H
So each process in round-robin gets its turn after
≤ (n − 1)q time when we do not consider over- 25. (a)
heads, but if we consider overhead(s) then it would
1 4 7 1 6 3 1 3 2 4 9 1
be ns + (n − 1)q. 1 1 1 1 1 1 1 1 1 4 4 4
So, we have ns + (n − 1)q ≤ t overhead will be 4 4 4 6 6 6 6 2 2 2 1
reduced when time quantum is maximum allow- 7 7 7 3 3 3 3 3 9 9
able, that is, q = (t − ns)/(n − 1) F F F H F F H H F F F H
12. (a) It is a condition to implement correctly Dining
Philosopher Problem. We have to start checking 26. (d)
from left fork to the right fork.
String 2 0 1 2 4 0 5 1 4 6 4 2 1 3 0
13. (a) Every process needs two resources to complete
its task. If we allocate five resources to five pro- Frame 1 2 2 2 0 0 1 2 4 0 5 5 1 6 4 2
cesses as one process each, then no one process can Frame 2 0 0 1 1 2 4 0 5 1 1 6 4 2 1
finish its task. But if we give one more resource to
Frame 3 1 2 2 4 0 5 1 4 6 4 2 1 3
any process then it will complete its task and free
its resources to be used by the other. By which all Frame 4 4 0 5 1 4 6 4 2 1 3 0
process can finish their tasks. So answer is 6.
Bolds are representing misses. So, total 10 misses.
14. (c) This is not a valid deadlock prevention scheme.
27. (d)
15. (a) Disk size = 8 × 1024 × 512 × 4 K = 16 × 230
bytes = 16 GB String 2 0 1 2 4 0 5 1 4 6 4 2 1 4 0
16. (b) Refer text Frame 1 2 2 2 0 1 2 4 0 5 1 1 6 4 2 1
Frame 2 0 0 1 2 4 0 5 1 4 6 4 2 1 4
17. (d) The peak demand for each processes A, B and
C is given as 3, 5 and 7, respectively. Suppose the Frame 3 1 2 4 0 5 1 3 6 4 2 1 4 0
peak demand for all the three processes happens
simultaneously, then we will require 3 + 5 + 7 = Bolds are representing misses. So 13 misses. So dif-
15 resources to satisfy the demand. ference is 13 − 10 = 3.
18. (d) 28 × 28 = 64K × 16 28. (b) Nearest cylinder next approach gives the better
results.
19. (d) The pages which have dirty bits are the pages
written back in memory, which avoids unnecessary 29. (c) Number of disk block possible in one disk block
writing.
DBA size
=
20. (b) Refer text DBA
21. (c) 4K = 212 → 12 addresses
Each process takes total of 10 min for I/O +
22. (c) Calculate by the given formula (EMAT = p × s 10 min for CPU, that is, 20 min
+ (1 − p) × m) Total time for complete = 20 min + 20 min
where p = page fault rate, s = page fault service = 40 mins
time, m = memory access time. 31. (b) Probability p for which time is spent in I/O
23. (a) Calculate by the given formula: [(% of Page miss × Page
p =fault
1/2 sorervice
50% time)
I/O
+ (% of Page hit × Memory access time)]
CPU utilization = 1 − p2 = 0.75
Average memory access time = T × 0.75 = 20
100
[(% of Page miss × Page fault service time) 32. (b) The use of multilevel page table reduces the
+ (% of Page hit × Memory access time)] size of the page table needed to implement the vir-
memory access time =
100 tual address space of a process.
33. (b) Total number of process is 2n, but we subtract 35. (a)
the main process, therefore total number of chil- (a) Critical region: Non-sharable region of code.
dren = (2n−1) (b) In concurrent programming, mutual exclusion
34. (c) Number of disk block pointer that will fit in one is achieved by semaphore.
block = 210/4 = 256 (c) Hoare’s monitor: Monitors used for synchroni-
zation were invented by C. A. R. Hoare.
Maximum file size = 256 × 256 × 256 × 1 KB (d) condition fordeadlockto occur: Hold and wait,
= 234 bytes circular wait, mutual exclusion, no pre-emption
8
7
Number of questions
6
5 Marks 1
4 Marks 2
3 Total number of questions
2
1
0
2015 2014 2013 2012 2011 2010
Year Concepts
2015 SQL, ER Diagram, Disk, Functional Dependency, Transactions, B+ Trees,
Transaction Properties, Transaction Recovery
2014 Relational algebra, SQL query, Serializability, Attributes of relational schema
2013 Indexing, Relational schemas, Normalizations, Functional dependencies
2012 ER and relational models, SQL query, Normalizations, Relational algebra
2011 SQL query, Indexing
2010 B+ tress, Relational schema, SQL query, Concurrency control protocols,
Functional dependencies
DATABASES
Syllabus: ER model, relational model (relational algebra, tuple calculus), database design (integrity constraints,
normal forms), query languages (SQL), file structures (sequential files, indexing, B and B+ trees), transactions and
concurrency control.
database system contains not only data but it describes ••Operating system software like Microsoft Win-
the description of the database structure. dows, Linux OS, Mac OS.
••DBMS software such as Oracle 8I, MySQL,
8.1.2 Database Management System Access (Jet, MSDE), SQL Server etc.
••Network softwares are used for sharing share the
A database is a collection of related data. Data is a col- data of database among multiple users.
lection of raw facts or figures, processed to form infor- ••Application programs are developed like C++,
mation. Database management system is a collection of
VB, dotnet etc. are used to access database in
programs for the creation and maintenance of database.
dbms. These are used to access and manipulate
It is an efficient and reliable approach to retrieve data
the data in the database.
for many users. It provides various functions such as:
1. Redundancy control: It provides redundancy 2. Hardware: It consists of all system’s physical
by removing duplicity of data by following rules of devices such as computers, storage devices, I/O
normalization. channels, electromechanical devices etc. It also
2. Data independence: It provides independence includes peripherals, such as, keyboard, mouse,
to application programs from details of data repre- modems, printers, etc.
sentation and storage. It also provides an abstract 3. Data: It is the collection of facts. The database
view of the data to insulate application code from contains the data and the metadata.
such details. 4. Procedures: There are the instructions and rules
3. Data integrity: It promotes and enforces some to design and use the database system. These
integrity rules for reducing data redundancy and includes the following:
increasing data consistency. ••Steps for the installation of DBMS
4. Concurrency control: It supports sharing of ••Steps to use the DBMS or application program
data, so, it has to provide an approach for managing
••Steps for the backup of DBMS
concurrent access of the database. Hence, preserv-
ing the inconsistent state and integrity of the data. ••Steps to change the structure of DBMS
5. Transaction management: It provides an ••Steps for the generation of reports.
approach to ensure that either all the updates for a
5. Data access language: The users can use it to
given transaction will execute or that none of them
access the data to and from the database. The func-
would execute.
tion of data access language is the entry of new data,
6. Backup and recovery: It provides mechanisms
manipulation of the existing data and the retrieval of
for backing up data periodically and recovering
the existing data in the database. The most popular
from different types of failures, thus, preventing
database access language is SQL (Structured Query
loss of data.
Language). Users can perform these functions with
7. Non-Procedural query language: It provides
the help of commands. The role of administrator is
with query language for retrieval and manipulation
to access, to create and to maintain the database.
of data.
6. People: Persons involved to access, to create and
8. Security: It protects unauthorized access in the
to maintain the database are called users. These
database. It ensures the access to authorized users.
are of various types according to the role performed
by them (Fig. 8.1). These are as follows:
8.2 COMPONENTS OF DATABASE ••System Administrator: The role of system
SYSTEMS a dministrator is to supervise the general opera-
tions of DBMS.
••Database Administrator: The role of database
DBMS consists of several components, namely software,
hardware, data, procedures and data access language. administrator (DBA) is to manage the DBMS.
These components are responsible for the definition, col- ••Database Designer: The role of database
lection, management and use of data within the envi- designer is to design the structure of the database.
ronment. Figure 8.1 shows the components of database ••Application Programmer: The role of
system. The description of each component is as follows: application programmer is to create the data
1. Software: It is the collection of programs used entry forms, reports and procedures.
by the computers within the database system. It is ••End User: The role of end user is to use the
used to handle, control and manage the database. application programs by entering new data and
It includes the following software: manipulating and accessing existing data.
5. Network model: In this model, data is repre- Table 8.1 | Notations of fundamental operations
sented as record types. The data in this model has of relational algebra
many-to-many relationship.
6. Hierarchical model: In this model, the data is Fundamental Operation Symbol
represented as a hierarchical tree structure. Select s
Project p
8.4.1 Relational Model
Union ∪
The database in relational model is represented as a col- Intersection ∩
lection of relations (tables). A relation is a kind of set. It Set difference −
is also a subset of a Cartesian product of an unordered
set of ordered tuples. Relational model was proposed by Cartesian product ×
E. F. Codd, which stores data in a tabular form. It con- Rename r
sists of a table where rows represent records and columns Natural join
represent the attributes. It has various terminologies as
follows:
Fundamental operations of relational algebra are as
1. Tuple: It represents a single row of a table, which follows (see Table 8.1):
contains a single record for that relation.
1. Select: It is used to select rows from a relation. It
2. Relation instance: It represents a finite set of
is denoted by s.
tuples in the relational database system.
3. Relation schema: It represents the relation Syntax of select s p (r), r is relation and p is prepo-
name, that is, table name, attributes and their sitional logic.
names. p uses connectors and operators Ù, Ú, =, ≠,<,>, £, ³.
4. Relation key: It represents the unique key for For example, s empname = "John" (emp).
the relation or table. Each row has one or more
attributes, which can identify the row in the table 2. Project: It is used to project columns in a relation.
uniquely. It is denoted by p . The duplicate tuples are auto-
5. Attribute domain: It represents the predefined matically eliminated.
value scope of each attribute. Syntax of projects p A (r), r is relation and A is the
attribute name in a relation.
8.4.1.1 Constraints in Relational Model For example, p empname, sal (emp)
Constraints are the restrictions that one wishes to apply 3. Union: It returns a relation instance, which con-
on database. The following constraints are applied on tains all tuples occurring in the first relation or in
relational model. the second relation. It is denoted as R∪S , where R
and S are two relations. The duplicate tuples are
1. Key constraints: Each relation has at least one automatically eliminated.
minimal subset of attributes, which can identify a This operation is valid for the following:
tuple uniquely. ••Both relations must have the same number of
••No two tuples have identical value for key at-
attributes.
tributes. ••Attribute domains must be compatible.
••Key attribute does not have NULL value.
4. Intersection: It returns a relation instance,
2. Domain constraints: Attributes have specific which contains all tuples occurring in both
domain values in real world. For example, value of relations. It is denoted as R ∩ S , where R and S
age can only be positive. are two relations.
3. Referential integrity constraints: If a relation
5. Set difference: It returns a relation instance,
refers to a key attribute of a different relation then
which contains all tuples that occur in the first
that key element must exist.
relation but not in the second relation. It is denoted
as R − S , where R and S are two relations.
8.4.1.2 Relational Algebra 6. Cartesian product: It returns a relation
instance, which contains all the fields of the first
It is a procedural query language. It takes instances of relation followed by all the fields of the second
relations as input and returns instances of relations as relation. It is denoted as R×S , where R and S are
output. Operators are used to perform queries. two relations.
7. Rename: It returns a relation but without any { t | Condition}, wherte t is a tuple variable and Condition
name. It is used to rename the output relation. It is a conditional expression.
is denoted as r. Preliminaries of tuple calculus are as follows:
8. Joins: It returns combined information from two
or more relations. 1. Constants
2. Predicates
••Condition joins: It accepts a join condition c 3. Boolean and, or, not
and a pair of relation instances as arguments, 4. $ there exists
and returns a relation instance. It is denoted as 5. " for all
s c (R×S ) .
••uijoin: It is a special case of condition joins
8.4.2 ER Model
where the condition c contains equalities.
••Natural join: It is a Cartesian product of two
ER model represents the conceptual view of a database.
relations. It is denoted by . It describes the relation of data to each other (Table 8.2).
It was developed by Peter Chen in 1976. It views real-
8.4.1.3 Tuple Calculus world data as systems of entities and relationships.
ER model has three basic elements: entity, attribute
It is a non-procedural query language. In this, number and relationship. These are discussed in the following
of tuple variables is specified. It is represented as sections.
Weak entity
Order Order item
Attribute
Name
Simple attribute
Order
Name
Derived attribute
Date−of−birth Age
Single-valued attribute
Roll_number
Multi-value attribute
Phone_number
(Continued)
Relationship
One-to-One 1 1
relationship Department has Manager
One-to-Many
1 N
relationship Manager manages Employee
Many-to-One N 1
relationship Employee works Department
Many-to-Many N N
relationship Employee handles Projects
Entities represent the real-world things. These are data Order Order item
objects which maintain different relationships with each
other, for example, Employee, Department, etc. These
are represented by means of rectangles.
8.4.2.2 Attributes
into sub parts. Examples are mobile number, roll For example contact number, email ids.
number.
Order Phone−number
Name
3. Derived attribute: Derived attributes are those Some basic terminologies related to relationship are given
whose value is derived from some other attribute below.
in the database. For example, age of person can be As we have discussed, relations are the core compo-
calculated from date of birth. Dotted oval is used nents in RDBMS, these relations are defined by two
to represent derived attributes. major characteristics–relationship set and the degree of
relationship, defined in the following text.
1. Relationship set: Relationship of similar type
is called relationship set. It has attributes. These
attributes are called descriptive attributes.
2. Degree of relationship: It defines the number
For example, of participating entities in a relationship. They are
of the following types:
••Unary relationship (Degree 1): One entity
participates. For example,
Date−of−birth Age
Manages
Specialization
Course Registration Faculty Employee
1 1
Department has Manager Employee works Department
2. Entity integrity constraint: It defines that Also, sometimes called, Project-Join Normal Form
the primary keys cannot be null. There must be a (PJNF).
proper value in the primary key field.
3. Referential integrity constraint: It is speci- 8.5.3 Attribute Closure
fied between two tables. It is used to maintain
the consistency among rows between the two Set of all attributes functionally determined by X is
tables. called closure of X. Closure set of X is denoted by X+.
4. Foreign key integrity constraint: There are
two types of foreign key integrity constraints:
Problem 8.1: Functional dependencies F = {A → B,
••Cascade update related fields: Whenever the B → C, C → D} is given, find closure of A, B, C
primary key of a row in the primary table is and D.
changed, the foreign key values are updated in
the matching rows in the related table. Solution:
••Cascade delete related rows: Whenever a row in
Closure of A = A+ = (A, B, C, D)
the primary table has been deleted, the match-
ing rows are automatically deleted in the related Closure of B = B+ = (B, C, D)
table. Closure of C = C+ = (C, D)
Closure of D = D+ = (D)
8.5.2 Normal Forms
in a table. Every candidate key is a superkey but not (a) Algorithm for finding decomposition is lossless:
vice versa.
tep 1: Union of all decomposed sub-relation
S
In other words, candidate key is the minimal super- should be equal to relation R.
key. If X is a superkey and none of the proper subset of
X is a superkey, then X is called the minimal superkey R1 ∪ R2 ∪ R3 ∪ … ∪ Rn = R
or candidate key.
tep 2: Any two sub-relations Ri and Rj can be
S
merged into Rij with R1 ∪ R2 only if
Closure of C = C+ = {C, D}
Problem 8.4: Consider a schema R (ABCDEFGHIJ)
Closure of AB = AB+ = {A, B, C, D, E}
and functional dependencies {FDs = (AB → C, A →
So, AB and B are superkeys. But only B is the can- D, B → F, F → GH, D → IJ)} and decompositions
didate key. (a) {D = (ABCDE, BFGH, DIJ)}
(b) {D = (ABCD, DE, BF, FGH, DIJ)}
Check whether the decomposition is lossless or not.
8.5.4.3 Primary Key
Solution:
It is one or more data attributes that uniquely identify (a) Given
an entity. It does not allow null values. R1 = (ABCDE)
1. Alternate key: The candidate key, which is not R2 = (BFGH)
selected as a primary key.
2. Composite key: It consists of two or more R3 = (DIJ)
attributes. Apply algorithm for lossless decomposition:
3. Foreign key: It is an entity that is the reference
to the primary key of another entity. Step 1:
R1 ∪ R2 ∪ R3 = {(ABCDE ) ∪ (BFGH ) ∪ (DIJ )} = (ABCDEFGHIJ
8.5.5 Decomposition R1 ∪ R2 ∪ R3 = {(ABCDE ) ∪ (BFGH ) ∪ (DIJ )} = (ABCDEFGHIJ ) = R
It is required to eliminate redundancy from the schema. Step 1 satisfies the given condition, so it is true.
If a relational schema R has redundancy in the data, Step 2:
then decompose R into two R1 and R2 schema. There For R1 and R2:
are two properties, which should be maintained when
we perform decomposition, it should be a lossless join as R1 ∩ R2 = (ABCDE ) ∩ (BFGH ) = B
well as dependency preserving.
Find closure of B = B+ = {B, F, G, H} .
Condition (ii) is satisfied, so R1 and R2 can be
8.5.5.1 Lossless Join
merged together. After merging R1 and R2,
Let R be a relation schema and let F be a set of func- R12 = (ABCDEFGH)
tional dependency (FD) over R. R is decomposed into R1
and R2. R1 and R2 are called lossless-join decomposition Now merge R12 and R3.
if R = R1 R2 or if we can recover original relation
from the decomposed relation. R12 ∩ R3 = (ABCDEFGH ) ∩ (DIJ ) = D
Find closure of D = D+ = {D, I, J }. Condition (ii) of step 2 is satisfied, so R134 and R5
can be merged together. After merging R134 and R5
Condition (ii) is satisfied, so R12 and R3 can be
merged together. After merging R12 and R3 R1345 = (ABCDFGHIJ )
••Syntax of grant command in logarithmic time. B trees are a general form of binary
trees where a node can have more than one child.The
GRANT privilege_name ON object_name
B-trees are efficient for those systems that read and write
TO {user_name | PUBLIC | role_name}
large blocks of data, that is, databases and file systems.
[with GRANT option];
B trees are self-balancing trees. All the leaf nodes are
For example, GRANT SELECT ON emp TO
at the same level. A B tree with order p has:
user1;
1. Root node may contain minimum 1 key
••Syntax of revoke command p
2. Minimum number of child = − 1
REVOKE privilege_name ON object_name 2
3. Maximum number of children = p
FROM {User_name | PUBLIC | Role_name};
4. Maximum keys = p − 1
For example, REVOKE SELECT ON emp TO
user1; The order of B-tree can be found as follows:
p × P + ( p − 1) (K + Pr ) ≤ Block size
8.7 FILE STRUCTURES where p is order of the tree, P is the block pointer, Pr is
the record pointer and K is the key pointer.
File structure mainly deals with how files are stored on
the disk. Various file organisations are described below. Problem 8.7: The order of B-tree index is the maxi-
mum number of children it can have. Suppose that
8.7.1 Sequential Files a block pointer takes 6 bytes, the search field value
takes 9 bytes, record pointer takes 7 bytes and the
It is a file organisation system where every file record block size is 512 bytes. What is the order of the B
contains an attribute to uniquely identify a particular tree?
record. Records are placed in a sequential order with
some unique key. Solution:
Given that block size = 512 B; record pointer (Pr) =
8.7.2 Indexing 7 B; block pointer (PB) = 6 B; key pointer (K) = 9
B. So, we have
Indexing is a data structure mechanism to efficiently
retrieve records from the database, for example, book p ×P + ( p − 1) (K + Pr ) ≤ Block size
index. It is defined based on its indexing attributes.
Indexing is of three types: 6p + (p − 1)(9 + 7) = 512
Order of leaf nodes: (no transaction will affect the existence of any
Pleaf ×[k + Pr ] + PB ≤ Block size other transaction).
4. Durability: Persistence of data even if the system
where p is the order of internal nodes, Pr is record fails and restarts.
pointer, PB is block pointer, k is key pointer, Pleaf is the
order of leaf nodes.
8.8.2 Schedule
Problem 8.8: The order of an internal node in a B+
A chronological execution sequence of transactions is called
tree index is the maximum number of children it can
schedule. It is a list of actions reading, writing, aborting
have. Suppose that a block pointer takes 6 bytes, the
or committing from a set of transactions. A schedule can
search field value takes 9 bytes, record pointer take
have many transactions. Schedule can be further divided
7 bytes and the block size is 512 bytes. What is the
into two types–serial schedule and concurrent schedule.
order of the internal node and leaf node?
8.8.2.3 Comparison between Serial and Example: Let value of data item A = 1000, X =
Concurrent Schedule 100 and Y = 200, then transaction Ti will update
the database with value 1100. Transaction Tj will
Table 8.5 shows the comparison between serial and con- read data item value as 1100. Transaction Tj will
current schedule. update database with value 1300. As transaction Ti
fails, the database value should be 1200. Therefore,
Table 8.5 | Serial schedule vs. concurrent schedule this problem is known as uncommitted read prob-
lem or dirty read problem.
Serial Schedule Concurrent Schedule
2. RW problem (Write after Read): RW
All serial schedules are All serial schedules are problem is also known incorrect summary prob-
consistent schedules not consistent schedules lem or unrepeatable read problem (Fig. 8.8). Let
Less throughput More throughput there be two transactions Ti and Tj of schedule S.
Poor resource utilization Good resource utilization Transaction Ti read a data item and Tj also read
similar data item. Transactions Ti and Tj both
More response time Less response time have write operation on that data item. If in both
For the given For the given transactions, read operation occurs before commit
transactions, the number transactions, the number of the other transaction and before writing back
of serial schedule is of concurrent schedule is that data item in database by other transaction
very much less than the more than the number of then RW problem will arise.
number of concurrent serial schedule
schedule Transaction Ti Transaction Tj
Read (A)
8.8.2.4 Problems Occurring due to If (A > 0)
Concurrent Schedule A=A+1
Read (A)
In concurrent schedule, more than one transaction is
If (A > 0)
executed simultaneously; and due to this some problem
A=A+1
arises with concurrent schedule given as follows:
Write (A)
1. WR problem (Read after Write): WR problem Commit Write (A)
is also known as dirty read problem or uncommit- Commit
Let there be two transactions Ti and Tj of schedule S. If Schedule Problem Exists Problem
transaction Tj reads a data item which is updated by Remove
transaction Ti and transaction Tj commits before the
commit (or rollback) of transaction Ti , then the given Irrecoverable All None
schedule S is called irrecoverable schedule. Recoverable Incorrect summary Irrecoverable
problem (RW),
Uncommitted Read
8.8.3.2 Recoverable Schedule problem (WR
problem), lost
Let a schedule S have two transactions Ti and Tj . If
update (WW) and
transaction Tj reads a data item which is updated by
cascading rollback
transaction Ti and transaction Tj is not allowed to
problem
commit (or rollback) before the commit (or rollback) of
transaction Ti , then the given schedule S is called recov- Cascading Incorrect summary Irrecoverable,
erable schedule. Recoverable schedule may suffer from rollback problem (RW) and Uncommitted
uncommitted read, lost update and incorrect summary recoverable lost update (WW). Read problem
problem. (WR problem)
and cascading
rollback
8.8.3.3 Cascading Rollback Recoverable problem.
Schedule
Strict Incorrect summary Irrecoverable,
Let there be four transactions (T1 , T2 , T3 , T4 ) in a given recoverable problem (RW) Uncommitted
schedule S. In schedule S, if rollback of a transaction (T1 ) Read problem
results in the rollback of the other transactions (T2 , T3 , T4 ) (WR problem),
(because of their dependency on each other), then this is lost update
called cascading rollback. (WW) and
cascading
If a schedule is recoverable and has no cascading roll- rollback
back, then it is called cascading rollback recoverable problem.
schedule. Incorrect summary and lost update problems
Problem 8.9: A concurrent schedule S has three Step 3: Check for strict recoverable. There
transactions T1 , T2 , T3. Transactions execute read/write should be commit operation between write operations
operation in the following sequence: on similar data items by two different transactions. In
the given problem, transaction T2 and transaction T3
Read1(x), Read2 (z), Read3 (x), Read1(z), Read2 (y), have write operations on data item (y), but no commit
Read3 (y), Write1(x), Commit1 , Write2 (z), Write3 (y), operation is performed by transaction T3 before data
item is updated by transaction T2. Hence, the given
Write2 (y), Commit3 , Commit2 .
schedule is not strict recoverable.
Find out the type of recoverable schedule?
Step 1: Check for recoverable. By using the defi- If a pair of operations do not fulfill the above three con-
nition of recoverable schedule we can find that given ditions, then the pair is call a non-conflict pair.
schedule is recoverable or not. (In this problem, no
transaction has read operation on a data item after
that data item is written by another transaction, so Problem 8.10: A schedule S1 with two transactions
the given schedule is recoverable.) T1 and T2 is given as:
Step 2: Check for cascading recoverable. In Read1(x), Write1(x), Read2 (x), Read1(y), Write1(y),
cascading recoverable schedule, no uncommitted read
Read2 (y)),....
is allowed. In the given problem, there is no uncommit-
ted read so the given schedule is cascadless schedule. Find conflict equivalent schedule to S1 .
Solution:
Transaction T1 Transaction T2
Vertices V1 and V2 represent transactions T1 and T2.
Read1 (x) ------ Edge 1 for
V2 : Read(x) operation and V1 : Write (x) operation
Write1 (x) ------
Edge 2 for
Read1 (y )
V1 : Write(y) operation and V2 : Read (y) operation
------
Read2 (x )
1
------
Write1 (y ) ------ V2
Read2 (y )
V1
------
2
Conflict equivalent schedules.
Transaction Transaction Transaction 1. If transaction Ti reads the initial value of data
T1 T2 T3 item (x) in schedule S1, then transaction Ti must
read the initial value of (x ) in schedule S2 .
Read (x) ------ ------ 2. In schedule S1 , if transaction Ti reads the value
Write (x) ------ ------ of data item (x ) produced by transaction Tj then
Ti must reads the value of (x ) that produced by
------ Read (x) ------ transaction Tj in the schedule S2 .
------ ------ Write (x) 3. If transaction Ti write the final value of data item
(x ) in schedule S1 , then transaction Ti must write
------ Write (y) ------ the final value of (x ) in schedule S2 .
Write (y) ------ ------
Problem 8.13: Identify whether the given concurrent
------ ------ Read (y) schedule is a view serializable schedule or not?
Solution: T1 T2 T3
Vertices V1, V2 and V3 represent transactions T1, T2 ------ ------ Read (x)
and T3.
------ Read (x) ------
Edge 1 for
V1 : Write(x) operation and V2 : Read (x) operation ------ ------ Write (x)
Edge 2 for Read (x) ------ ------
V2 : Read (x) operation and V3 : Write(x) operation Write (x) ------ ------
Edge 3 for
V2 : Write(y) operation and V1 : Write(y) operation
Solution: We have a concurrent schedule S1 with
Edge 4 for three transactions, so the number of possible serializ-
V1 : Write(y) operation and V3 : Read (y) operation able schedules with three transactions is 8 (23 = 8).
Edge 5 for So, we will pick one serializable schedule S2 and if
V2 : Write(y) operation and V3 : Read (y) operation the given concurrent schedule is be view equivalent
to that schedule, only then would the given schedule
will be view serializable.
3
V1
T1 T2 T3
1
V2 ------ ------ Read (x)
4
------ Read (x) ------
2 and 5 ------ ------ Write (x)
Read (x) ------ ------
V3
Write (x) ------ ------
•• Transaction T issues Write(x) operation to inspect all the operation performed by trans-
(a) If (RTS(x) > Time Stamp( Ti )) actions in a schedule. There are various deadlock
Rollback Ti prevention scheme which uses time stamp ordering
mechanism.
(b) If (WTS(x) > Time Stamp( Ti )) 6. Wait-Die Protocol: Assume TimeStamp (T1 ) < … < TimeSt
Rollback Ti TimeStamp (T1 ) < … < TimeStamp (Tn ) and Ti and Tj are any two
Note: Basic Time Ordering protocol ensures seri- transactions and transaction Ti is older than trans-
alizability, equivalent serial schedule based on time action Tj . This implies
stamp Ordering. It is deadlock free protocol, but
basic time ordering protocol can have starvation TimeStamp (Ti ) < … < TimeStamp (Tj ).
and irrecoverable schedule (because it does not •• If transaction Ti required a lock which is hold
ensure order of commit). by transaction Tj, then transaction Ti is allowed
4. Strict Time Ordering Protocol: In this pro- to wait.
tocol, an additional condition is added in the Basic •• If transaction Tj required a lock which is hold by
Time Ordering protocol. This condition is as follows: transaction Ti then transaction Tj will rollback.
Let a schedule have two transactions Ti and Tj 7. Wound-Wait Protocol: Let TimeStamp (T1 ) < … < TimeSt
where time stamp of ( Ti ) is less than timeTimeStamp
stamp (T1 ) < … < TimeStamp (Tn ) and Ti and Tj are any two
of Tj . If transaction Tj issues Read(x)/ Write(x) transactions and transaction Ti is older than trans-
operation with WTS(x) < Time Stamp (Tj ) then action Tj . This implies
(Tj ) has to be delayed Read(x)/ Write(x) opera-
tion until commit or rollback of transaction Ti TimeStamp (Ti ) < TimeStamp (Tj )
that has performed Write(x). •• If transaction Ti required a lock which is hold
Strict Time Ordering Protocol ensures serializability by transaction Tj then rollback transaction Tj.
and deadlock free. But it may suffer from starvation. •• If transaction Tj required a lock which is hold
5. Deadlock Prevention: To prevent deadlock by transaction Ti then transaction Tj is allowed
situation in a database system it is very important to wait.
IMPORTANT FORMULAS
1. All the 3NF and BCNF decomposition guarantee 5. CP: Child Pointer
lossless join.
6. KV: Key Value
2. All 3NF and BCNF decomposition may not guar-
7. Order of internal node in B+ tree is
antee dependency preservation.
PI . CP + (PI − 1)KV ⇐ Block size
3. Any relation with two attributes is in BCNF.
4. Functional dependency F: X → Y implies that for
any two tuples if t1[X ] = t2[X ], they must have
t1[Y ] = t2[Y ].
Inference Rules:
1. Reflexivity 5. Decomposition
If Y ⊆ X, then X → Y If X → YZ, then X → Y and X → Z
2. Augmentation 6. Pseudo-transitivity
If X → Y, then XZ → YZ for any Z If A → B and BC → D, then AC → D
3. Transitivity 7. Number of rows in cross product = r1 × r2
If X → Y and Y → Z, then X → Z
8. Number of attributes in cross product = m + n
4. Union
If X → Y and X → Z, then X → YZ 9. Complete set of operations are {s, p, ∪, −, ×}
10. In clustering index, file is ordered on non-key field. 12. Different kind of keys — Primary Key, Secondary
Key, Candidate Key, Alternate key, Foreign Key,
11. Secondary indexes are created on unordered file on Superkey, Compound key (or Composite key)
either key or non-key field. (or concatenated key).
SOLVED EXAMPLES
1. Which of the following operations does not modify Solution: Lost update, inconsistent data and
the database? uncommitted (WR) problems are overcome by
database lock.
(a) Sorting (b) Insertion at the beginning
Ans. (d)
(c) Append (d) Modify
6. Database language may consist of the following?
Solution: Sorting does not involving addition/
(a) DDL and DML
insertion or modification of any element.
(b) DML
Ans. (a)
(c) Query language
2. Which of the following operations is based on rela- (d) DDL, DML, query language
tional algebra?
Solution: DDL, DML, query language are all data
(a) rename and union base languages.
(b) select and union Ans. (d)
(c) only union
7. Which of the following is not a function of DBA?
(d) rename, select and union
(a) Network maintenance
Solution: Rename, select and union are some of (b) Routine maintenance
the operations of relational algebra. (c) Defining the schema
Ans. (d) (d) Data access through authorization
3. ACID properties of a transaction are Solution: The role of database administrator is to
(a) atomicity, constant, integrity, durability. manage the DBMS.
(b) atomicity, consistency, isolation, durability. Ans. (a)
(c) atomicity, compact, in-built, durability. 8. In a relational database the category type of infor-
(d) automatically, consistency, isolation, database. mation is represented in
Solution: ACID properties are atomicity, consis- (a) tuple. (b) field.
tency, isolation and durability. (c) primary key. (d) database name.
Ans. (b) Solution: Field shows type of information in rela-
tional database.
4. Which normal form is concerned with removing
Ans. (b)
transitive dependency?
9. Which key is used to identify a tuple uniquely?
(a) 1NF (b) 2NF
(c) 3NF (d) BCNF (a) Primary key (b) Tuple key
(c) Unique key (d) Domain key
Solution: 3NF is also the highest normal form.
Solution: Primary key.
Ans. (c) Ans. (a)
5. The concept Database Lock is used to overcome
10. An association of the information is represented by
the problem of
(a) an attribute. (b) a relationship.
(a) lost update and inconsistent data.
(c) a normal form. (d) the records.
(b) uncommitted dependency and inconsistent data.
(c) inconsistent data only. Solution: Relationship shows association of
(d) lost updates, inconsistent data and uncommitted information.
dependency. Ans. (b)
11. In which stage of database design, all the necessary Solution: It provides data storage and responsive-
fields and their types of a database are listed? ness to queries.
Ans. (a)
(a) Data definition (b) Data field definition
(c) E-R diagram (d) User definition 17. If D1, D2, … Dn are domains in a relational
model, then the relation is a table, which is a
Solution: In data definition stage, all the fields
subset of
and their types are listed.
(a) D1 ⊕ D2 ⊕ … ⊕ Dn (b) D1 × D2 × … × Dn
(c) D1 ∪ D2 ∪ … ∪ Dn (d) D1 ∩ D2 ∩ … ∩ Dn
Ans. (a)
(a) Time stamp protocols avoid deadlock. (a) CD (b) EC (c) AE (d) AC
(b) Locking technique is used to avoid deadlock.
(c) 2Phase locking does not provide serializability. Solution: To find key, we try to find closure.
(d) 2Phase deals with input and storing phase. (CD)+ = {C, D, F}
Solution: Time stamp protocol is a deadlock free (EC)+ = {A, B, C, D, E, F}
protocol. (AE)+ = {A, B, E}
(AC)+ = {A, B, C, F}
Ans. (a)
15. Data integrity control makes use of for
maintaining the integrity of database. Only EC satisfies the closure property.
Ans. (b)
(a) specific alphabets (uppercase and lowercase
alphabets) 20. Give the following relation instance:
(b) passwords
(c) relational algebra x y z
(d) storing on a backup hard disk 1 4 2
1 5 3
Solution: It promotes and enforces some integ- 1 6 3
rity rules for the reducing data redundancy and 3 2 2
increasing data consistency.
Ans. (a) Which of the following functional dependencies are
16. Data warehouse provides satisfied by the instance?
Solution: Functional dependency should uniquely maximum and minimum sizes of the join, respec-
identify the value. From the given options only the tively, are
following satisfies this condition:
(a) m + n and 0. (b) mn and 0.
XY → Z, YZ → X, Y → Z AND Y → X (c) m + n and |m − n|. (d) mn and m + n.
Ans. (b)
Solution: Number of tuples in their join gets mul-
21. Relation R is decomposed using a set of functional tiplied with number of tuples in both the relations.
dependencies F, and relation S is decomposed So, the maximum and minimum tuples will be mn
using another set of functional dependencies G. and 0, respectively.
One decomposition is definitely BCNF, the other is Ans. (b)
definitely 3NF, but it is not known which is which.
To make a guaranteed identification, which on one 23. Given the relations
of the following tests should be used on the decom-
Employee(name, salary, deptno) and depart-
positions? (Assume that the closures of F and G
ment (deptno, deptname, address), which
are available.)
of the following queries cannot be expressed
(a) Dependency-preservation using the basic relational algebra operations
(b) Lossless-join (s, p, , E, ∩, −)?
(c) BCNF definition (a) Department address of every employee
(d) 3NF definition (b) Employees whose name is the same as their
department name
Solution: All the 3NF and BCNF decomposition (c) The sum of all employees’ salaries
guarantee lossless join. All 3NF and BCNF decom- (d) All employees of a given department
position does not guaranty dependency preservation.
According to this, only option (c) can be correct. Solution: Aggregate functions such as sum, avg,
Ans. (c) min and max cannot be expressed in terms of
basic relational algebra operations. These require
22. Consider the join of a relation R with a relation extended relational algebra.
S. If R has m tuples and S has n tuples then the Ans. (c)
1. Which of the following scenarios may lead to an 2. Consider the following SQL query:
irrecoverable error in a database system?
SELECT distinct a1,a2,K,an a
(a) A transaction writes a data item after it is read FROM r1,r2,L,rm r
by an uncommitted transaction. WHERE P
(b) A transaction reads a data item after it is read
by an uncommitted transaction. For an arbitrary predicate P, this query is equiva-
(c) A transaction reads a data item after it is lent to which of the following relational algebra
written by a committed transaction. expressions?
(d) A transaction reads a data item after it is (a) Πa1 ,a2 ,…,a n s p (r1 × r2 × × rm )
written by an uncommitted transaction.
(GATE 2003: 1 Mark) (b) Πa ,a , K ,an s p (r1 r2 L rm)
1 2
consider all the tables r1, r2, … rn. Row (s ) selects Grades: (Roll_number, Course_number, Grade)
all the rows satisfying predicate P. SELECT distinct Name
Ans. (a) FROM Students, Courses, Grades
3. Consider the following functional dependencies in a WHERE Students.Roll_number = Grades.
database: Roll_number
Date_of_birth → Age and Courses.Instructor = Shyam
Age → Eligibility and Courses.Course_number = Grades.
Name → Roll_number Course_number
Roll_number → Name and Grades.grade = A
Course_number → Course_name Which of the following sets is computed by the
above query?
Course_number → Instructor
(Roll_number, Course_number) → Grade (a) N ames of students who have got an A grade in
all courses taught by Shyam.
The relation (Roll_number, Name, Date_of_
(b) Names of students who have got an A grade in
birth, Age) is
all courses.
(a) In Second normal form but not in Third normal (c) Names of students who have got an A grade in
form at least one of the courses taught by Shyam.
(b) In Third normal form but not in BCNF (d) None of the above.
(c) In BCNF (GATE 2003: 2 Marks)
(d) In none of the above
(GATE 2003: 2 Marks) Solution: Use of distinct selects the name only
once. Due to distinct selected names will be unique,
Solution: The applicable FDs are: so one person will be selected only once, in spite of
Date_of_birth → Age getting grade A in any number of courses taught
Age → Eligibility by Shyam.
Ans. (c)
Name → Roll_number
Roll_number → Name 5. Consider three data items D1, D2 and D3, and the fol-
The candidate keys are lowing execution schedule of transactions T1, T2 and
(Roll_number, Date_of_birth) and (Name, T3. In the diagram, R(D) and W(D) denote the actions
Date_of_birth) reading and writing the data item D, respectively.
For candidate key the FD Date_of_birth → Age
T T2 T3
is a partial dependency.
So, the relation is in 1NF. R(D3);
Candidate keys for given relation are: (date of R(D2);
birth, name) and (date of birth, roll_number)
W(D2);
Data of Birth → Age
R(D2);
Age (non-prime) is partially dependent upon Date
of Birth which is prime attribute. According to R(D3);
2NF definition, every non-prime attribute should R(D1);
be fully functional dependent on key, so it is not in W(D1);
2NF. Therefore, it will not be in 3NF and BCNF
W(D2);
also.
Ans. (d) W(D3);
R(D1);
4. Consider the set of relations shown below and the
SQL query that follows: R(D2);
W(D2);
Students: (Roll_number, Name, Date_of_
birth) W(D1);
Courses: (Course number, Course_name,
Instructor) Which of the following statements is correct?
(a) The schedule is serializable as T2; T3; T1. present in (Student * Enroll), where `*’ denotes
(b) The schedule is serializable as T2; T1; T3. natural join?
(c) The schedule is serializable as T3; T2; T1.
(a) 8, 8 (b) 120, 8
(d) The schedule is not serializable.
(c) 960, 8 (d) 960, 120
(GATE 2003: 2 Marks)
(GATE 2004: 1 Mark)
Solution: Let us draw a flow diagram between
the three schedules. An edge is drawn between two Solution: The maximum and minimum number
schedules if there exists read-write or write-write of tuples presented in (Student * Enroll) would be
dependency between them. represented by the minimum of these 120 and 8,
that is, min(120, 8) = 8
Natural join has inbuilt condition of equality. So in
T2 both the relations, minimum and maximum tuples
T3 will be 8.
Ans. (a)
8. It is desired to design an object-oriented employee
record system for a company. Each employee has
a name, unique id and salary. Employees belong
T1 to different categories and their salary is deter-
mined by their category. The functions getName,
getId and compute salary are required. Given the
The schedule has cycle between T1 and T3. So, the class hierarchy below, possible locations for these
schedule is not serializable. functions are:
Ans. (d)
(i) getId is implemented in the superclass
6. Let R1(A, B, C, D) and R2(D, E) be two relation (ii) getId is implemented in the subclass
schema where the primary keys are shown (iii) getName is an abstract function in the
underlined, and let C be a foreign key in R1 refer- superclass
ring to R2. Suppose there is no violation of the (iv) getName is implemented in the superclass
above referential integrity constraint in the corre- (v) getName is implemented in the subclass
sponding relation instances r1 and r2. Which one of (vi) getSalary is an abstract function in the
the following relational algebra expressions would superclass
necessarily produce an empty relation? (vii) getSalary is implemented in the superclass
(a) ΠD (r2 ) − ΠC (r1 ) (b) ΠC (r1 ) − ΠD (r2 ) (viii) getSalary is implemented in the subclass
(c) ΠD (r1 C ≠D (d) ΠD (r1 C = D r2 )
(GATE 2004: 2 Marks)
r2 )
(GATE 2004: 1 Mark)
Employee
9. The relation scheme Student Performance (Name, 11. The order of an internal node in a B+ tree index
CourseNo, RollNo, Grade) has the following func- is the maximum number of children it can have.
tional dependencies: Suppose that a child pointer takes 6 bytes, the
Name, CourseNo → Grade search field value takes 14 bytes, and the block
RollNo, CourseNo → Grade size is 512 bytes. What is the order of the internal
Name → RollNo node?
RollNo → Name (a) 24 (b) 25
The highest normal form of this relation scheme is (c) 26 (d) 27
(a) 2NF (b) 3NF (GATE 2004: 2 Marks)
(c) BCNF (d) 4NF
Solution: Order of internal node is PI . CP + (PI − 1)
(GATE 2004: 2 Marks) KV <= Block Size
Child pointer (CP) = 6 B
Solution: Name and RollNo are candidate keys. Key value (KV) = 14 B
The attributes are not repeated. So, the relation Block size = 512 B
schema is in 1 NF. 6 + (PI − 1) × 14 = 512
There is no partial dependency. So, the relation is PI = 26
in 2NF. Ans. (c)
Grade is not fully dependent upon all candidate
keys, so it is not in 3NF. 12. The employee information in a company is stored
Candidate keys for given relation are: (rollNo, in the following relation:
courseNo) and (name, courseNo) there is no partial Employee(name, sex, salary, deptName)
and transitive dependencies. So the highest normal
form is 3NF. name → rollNo and rollNo → name Consider the following SQL query:
are violating the condition of BCNF.
SELECT deptName
Ans. (b)
FROM Employee
10. Consider the relation Student (name, sex, marks), WHERE sex = ‘M’
where the primary key is shown underlined, per-
taining to students in a class that has at least one GROUP by deptName
boy and one girl. What does the following rela- HAVING avg(salary)>
tional algebra expression produce? (SELECT avg (salary) FROM Employee)
(Note: r is the rename operator).
It returns the names of the department in which
Π name (rsex = female (Student) − Π name (Studentsex = female Λx = maleΛmarks≤m rn ,x ,m (Student))
(a) the average salary is more than the average
) − Π name (Studentsex = female Λx = maleΛmarks≤m rn ,x ,m (Student)) salary in the company.
(b) the average salary of male employees is more
(a) Names of girl students with the highest marks than the average salary of all male employees
(b) Names of girl students with more marks than in the company.
some boy student (c) the average salary of male employees is more
(c) Names of girl students with marks not less than than the average salary of employees in the
some boy student same department.
(d) Names of girl students with more marks than (d) the average salary of male employees is more
all the boy students than the average salary in the company.
(GATE 2004: 2 Marks)
(GATE 2004: 2 Marks)
Solution: The query is Part 1 − Part 2. Part 2 Solution: Inner query selects the average salary
of the query results in names of girls having marks of employees in the company. Outer query selects
less than or equal to marks of all the boys. And average salary of male employees. So, the whole
Part 1 selects the names of all the girls in class. So, query finds the department name of whose average
Part 1 − Part 2 will result in names of girl students salary of male employees is more than the average
having more marks than all the boy students. salary in the company.
Ans. (d) Ans. (d)
13. Which one of the following is a key factor for pre- Table r1 Table r2
ferring B+ trees to binary search trees for indexing
A B C A D
database relations?
1 10 100 1 1000
(a) Database relations have a large number of
records. 1 20 200 1 2000
(b) Database relations are sorted on the primary key.
(c) B+ trees require less memory than binary Table s (natural join of r1 and r2)
search trees.
(d) Data transfer from disks is in blocks. A B C D
(GATE 2005: 1 Mark)
1 10 100 1000
Solution: Indexing performs well for large data 1 20 200 1000
blocks. 1 20 100 200
B+ trees are preferred over the binary search
1 20 200 2000
trees.
B+ trees transfer data from disk to primary Ans. (c)
memory in form of data blocks.
In case of B+ trees, data moves in terms of blocks. 16. Let E1 and E2 be two entities in an E/R diagram
Ans. (d) with simple single-valued attributes. R1 and R2 are
two relationships between E1 and E2, where R1 is
14. Which one of the following statements about one-to-many and R2 is many-to-many. R1 and R2
normal forms is FALSE? do not have any attributes of heir own. What is the
(a) BCNF is stricter than 3NF. minimum number of tables required to represent
(b) Lossless, dependency-preserving decomposition this situation in the relational model?
into 3NF is always possible. (a) 2 (b) 3
(c) Lossless, dependency-preserving decomposition (c) 4 (d) 5
into BCNF is always possible. (GATE 2005: 2 Marks)
(d) Any relation with two attributes is in BCNF.
(GATE 2005: 1 Mark) Solution:
R2
Solution: It is not always possible to decompose E1 E2 E1 E2
a table in BCNF and preserve dependencies. For
l x L x
example, a set of functional dependencies {AB → C,
C → B} cannot be decomposed in BCNF. m y L y
Ans. (c) n z M y
15. Let r be a relation instance with schema R = The one-to-many relationships are represented
(A, B, C, D). We define r1 = PA, B, C(R) and r2 = with entity set from one side. In one-to-many rela-
PA, D(r). Let s = r1 * r2 where * denotes natural tionships, each entity in the entity set can be asso-
join. Given that the decomposition of r into r1 and ciated with at most one entity of the other. So, the
r1 is lossy, which one of the following is TRUE? table is not formed for R1. Hence, the tables are
formed for R2, E1 and E2.
(a) s ⊂ r (b) r ∪ s = r
Using cross-reference technique, minimum of 3
(c) r ⊂ s (d) r * s = s
tables are required.
(GATE 2005: 1 Mark)
Ans. (b)
Solution: 17. The following table has two attributes A and C
where A is the primary key and C is the foreign key
Table r
referencing A with on-delete cascade.
A B C D
A C
1 10 100 1000
2 4
1 20 200 1000
3 4
20 200 2000
(Continued)
Continued {A → B, BC → D, E → C, D → A}.
A C What are the candidate keys of R?
4 3 (a) AE, BE (b) AE, BE, DE
5 2 (c) AEH, BEH, BCH (d) AEH, BEH, DEH
7 2 (GATE 2005: 2 Marks)
9 5
Solution: Let S be a candidate key of relation R
6 4 if the closure of S is all attributes of R and there
is no subset of S whose closure is all attributes
The set of all tuples that must be additionally of R.
deleted to preserve referential integrity when the Closure of AEH: AEH+ = {ABCDEH}
tuple (2, 4) is deleted is
Closure of BEH: BEH+ = {ABCDEH}
(a) (3, 4) and (6, 4)
(b) (5, 2) and (7, 2) Closure of DEH: DEH+ = {ABCDEH}
(c) (5, 2), (7, 2) and (9, 5) Ans. (d)
(d) (3, 4), (4, 3) and (6, 4)
20. Consider the following log sequence of two transac-
(GATE 2005: 2 Marks) tions on a bank account, with initial balance 12000,
that transfer 2000 to a mortgage payment and then
Solution: When (2,4) is deleted, as C is a foreign apply a 5% interest.
key referring A with delete on cascade, all entries
with value 2 in C must be deleted. 1. T1 start
So, (5, 2) and (7, 2) are deleted. As a result of 2. T1 B old = 1200 new = 10000
this, 5 and 7 are deleted from A, which causes
(9, 5) to be deleted. 3. T1 M old = 0 new = 2000
Ans. (c) 4. T1 commit
5. T2 start
18. The relation book (title, price) contains the titles
and prices of different books. Assuming that no 6. T2 B old = 10000 new = 10500
two books have the same price, what does the fol- 7. T2 commit
lowing SQL query list?
Suppose the database system crashes just before log
SELECT title record 7 is written. When the system is restarted,
FROM book as B which one statement is true of the recovery procedure?
WHERE (SELECT count(*) (a) We must redo log record 6 to set B to 10500.
FROM book as T (b) We must undo log record 6 to set B to 10000
and then redo log records 2 and 3.
WHERE T.price > B.price) < 5
(c) We need not redo log records 2 and 3 because
(a) Titles of the four most expensive books transaction T1 has committed.
(b) Title of the fifth most inexpensive book (d) We can apply redo and undo operations in arbi-
(c) Title of the fifth most expensive book trary order because they are idempotent.
(d) Titles of the five most expensive books (GATE 2006: 1 Mark)
(GATE 2005: 2 Marks)
Solution: When a transaction is committed, no
Solution: The outer query selects all titles from need to redo or undo operations.
book table. For every selected book, the sub-query Ans. (c)
returns count of those books, which are more expen-
sive than the selected book. The where clause of outer 21. Consider the relation account (customer, balance)
query will be true for five most expensive books. where customer is a primary key and there are
Ans. (d) no null values. We would like to rank customers
according to decreasing balance. The customer with
19. Consider a relation scheme R = (A, B, C, D, E, H ) the largest balance gets rank 1. Ties are not broke
on which the following functional dependencies hold: but ranks are skipped: if exactly two customers
have the largest balance they each get rank 1, and (a) All queries return identical row sets for any database
rank 2 is not assigned. (b) Query 2 and Query 4 return identical row sets for
all databases but there exist databases for which
Query 1: SELECT A.customer, count(B. Query 1 and Query 2 return different row sets.
customer) FROM account A, account B (c) There exist databases for which Query 3 returns
WHERE A.balance ⇐ B.balance group by strictly fewer rows than Query 2
A.customer (d) There exist databases for which Query 4 will
Query 2: SELECT A.customer, 1+count(B. encounter an integrity violation at runtime.
customer) FROM account A, account B
WHERE A.balance < B.balance group by (GATE 2006: 2 Marks)
A.customer Solution: The output of Query 2, Query 3 and
Consider these statements about Query 1 and Query 2. Query 4 will be identical. Query 1 produces dupli-
cate rows. But row set produced by all the queries
I. Q
uery 1 will produce the same row set as Query will be same. For example,
2 for some but not all databases.
II. Both Query 1 and Query 2 are correct imple- Table: Enrolled Table: Enrolled
mentation of the specification. Student Course Student Amount
III. Q uery 1 is a correct implementation of the
specification but Query 2 is not. A C1 A 100
IV. Neither Query 1 nor Query 2 is a correct imple- B C1 B 100
mentation of the specification. A C2 D 200
V. Assigning rank with a pure relational query
C C1
takes less time than scanning in decreasing
balance order assigning ranks using ODBC.
Output of Query 1: A B
Which two of the above statements are correct? Output of Query 2: A B
(a) II and V (b) I and III Output of Query 3: A B
(c) I and IV (d) III and V
Output of Query 4: A B
(GATE 2006: 2 Marks) Ans. (a)
Solution: Both query1 and query2 perform the 23. Consider the relation enrolled (student, course) in
same task, but none of them sort the customer which (student, course) is the primary key, and the
according to the decreasing balance. So, option (c) relation paid (student, amount) in which student
is correct. is the primary key. Assume no null values and no
Ans. (c) foreign keys or integrity constraints. Assume that
amounts 6000, 7000, 8000, 9000 and 10000 were
22. Consider the relation enrolled (student, course) in
each paid by 20% of the students. Consider these
which (student, course) is the primary key, and
query plans (Plan 1 on left, Plan 2 on right) to “list
the relation paid (student, amount) where student
all courses taken by students who have paid more
is the primary key. Assume no null values and no
than x”.
foreign keys or integrity constraints. Given the fol-
lowing four queries: A disk seek takes 4 ms, disk data transfer band-
Query 1: SELECT student FROM enrolled WHERE width is 300 MB/s and checking a tuple to see if
student in (SELECT student FROM paid) amount is greater than x takes 10 µs. Which of the
following statements is correct?
Query 2: SELECT student FROM paid WHERE
student in (SELECT student FROM enrolled) (a) P lan 1 and Plan 2 will not output identical row
Query 3: SELECT E.student FROM enrolled sets for all databases.
E, paid P WHERE E.student = P.student (b) A course may be listed more than once in the
Query 4: SELECT student FROM paid WHERE output of Plan 1 for some databases.
exists (c) For x = 5000, Plan 1 executes faster than Plan
2 for all databases.
(SELECT * FROM enrolled WHERE enrolled. (d) For x = 9000, Plan 1 executes slower than Plan
student = paid.student) 2 for all databases.
Which one of the following statements is correct? (GATE 2006: 2 Marks)
Project on course
Solution: Plans need to load both tables’ courses The closure of {AF}+ is {A F D E}. It cannot
and enrolled. So, disk access time is the same for drive all the members of set given in option (c). So,
both plans. option (c) is false.
Plan 2 does lesser number of comparisons com- Ans. (c)
pared to Plan 1.
25. Information about a collection of students is given
So, join operation will require more comparisons
by the relation studinfo(studId, name, sex). The
as the second table will have more rows in Plan 2
relation enroll(studId, courseId) gives which stu-
compared to Plan 1.
dent has enrolled for (or taken) what course(s).
The joined table of two tables will have more rows,
Assume that every course is taken by at least
so, more comparisons are needed to find amounts
one male and at least one female student. What
greater than x.
does the following relational algebra expression
Plan 1 executes faster than Plan 2. First tuples are
represent?
filtered then joined, whereas in plan 2 first tuples
are joined and then filtered, which takes more time. Π courseId (Π studId (s sex ="female"(studInfo)) × Π courseId (enroll))
Π courseId (Π studId (s sex ="female"(studInfo)) × Π courseId (enroll))
Ans. (c)
(a) C ourses in which all the female students are
24. The following functional dependencies are given:
enrolled.
AB → CD, AF → D, DE → F, C → G, F → E, G (b) Courses in which a proper subset of female
→A students are enrolled.
(c) Courses in which only male students are
Which one of the following options is false? enrolled.
(a) {CF}+ = {ACDEFG} (d) None of the above
(b) {BG}+ = {ABCDG} (GATE 2007: 2 Marks)
(c) {AF}+ = {ACDEFG} Solution:
(d) {AB}+ = {ABCDFG}
(i) Select studId of all female students and select
(GATE 2006: 2 Marks) all courseId of all courses.
(ii) The query performs a Cartesian product of the
Solution: Closure of AF or AF+ = {ADEF}, above select two columns in Step 1.
closure of AF does not contain C and G. (iii) It subtracts enroll table from the result of Step 2.
This will remove all the (studId, courseId) pairs, Solution: Q1 selects an employee but does not
which are present in enrol table. compute the result as after the `Where not exists’,
If all female students have registered in courses, it does not have statement to produce the result.
then this course will not be there in the subtracted Q2 selects an employee who gets higher salary
result. than anyone in the department 5. It correctly finds
So, the complete expression returns courses in employees who get higher salary than anyone in
which a proper subset of female students is enrolled. the department 5.
Ans. (b) Ans. (b)
26. Consider the relation employee (name, sex, 28. Which one of the following statements is FALSE?
supervisorName) with name as the key. super-
(a) Any relation with two attributes is in BCNF.
visorName gives the name of the supervisor of
(b) A relation in which every key has only one
the employee under consideration. What does
attribute is in 2NF.
the following Tuple Relational Calculus query
(c) A prime attribute can be transitively dependent
produce?
on a key in a 3NF relation.
{e.name|employee(e) ∧{(∀x) (d) A prime attribute can be transitively depen-
[¬employee(x) ∨ x.supervisorName ≠ dent on a key in a BCNF relation.
e.name ∨ x.sex = “male”)]}/ inserted on (GATE 2007: 2 Marks)
the left of expression
Solution: According to the definition of 3NF, a
(a) Names of employees with a male supervisor.
prime attribute can be transitively dependent on a
(b) Names of employees with no immediate male
key. Option (d) is incorrect because this does not
subordinates.
satisfy the condition of BCNF.
(c) Names of employees with no immediate female
Ans. (d)
subordinates.
(d) Names of employees with a female supervisor. 29. The order of a leaf node in a B+ tree is the maxi-
(GATE 2007: 2 Marks) mum number of (value, data record pointer) pairs
Solution: The query selects all those employees it can hold. Given that the block size is 1 KB, data
whose immediate subordinate is “male”. record pointer is 7 bytes long, the value field is
Ans. (b) 9 bytes long and a block pointer is 6 bytes long,
what is the order of the leaf node?
27. Consider the table employee (empId, name, depart- (a) 63 (b) 64
ment, salary) and the two queries Q1, Q2 below. (c) 67 (d) 68
Assuming that department 5 has more than one (GATE 2007: 2 Marks)
employee, and we want to find the employees who
get higher salary than anyone in the department Solution: Let the order of the leaf node be n.
5, which one of the statements is TRUE for any Block size = 1 KB = 1024 bits
arbitrary employee table? 6 + 7n + (n − 1)9 = 1024
Q1: SELECT e.empId 1 n = 64
FROM employee e Ans. (b)
WHERE not exists (SELECT * FROM
employee s WHERE s.department = “5”
30. Consider the following schedules involving two
and s.salary >= e.salary) transactions. Which one of the following statements
is TRUE?
Q2: SELECT e.empId
FROM employee e S1: r1(X ); r1(Y ); r2(X ); r2(Y ); w2(Y ); w1(X )
WHERE e.salary > Any S2: r1(X ); r2(X ); r2(Y ); w2(Y ); r1(Y ); w1(X )
(SELECT distinct salary FROM em-
ployee s WHERE s.department = “5”) (a) Both S1 and S2 are conflict serializable.
(b) S1 is conflict serializable and S2 is not conflict
(a) Q1 is the correct query. serializable.
(b) Q2 is the correct query. (c) S1 is not conflict serializable and S2 is conflict
(c) Both Q1 and Q2 produce the same answer. serializable.
(d) Neither Q1 nor Q2 is the correct query. (d) Both S1 and S2 are not conflict serializable.
(GATE 2007: 2 Marks) (GATE 2007: 2 Marks)
( ))
Schedule S2
T1 T2
(
IV. ΠP ΠP ,Q (R ) − ΠP ,Q (R ) − ΠP ,Q (S )
(a) I only (b) II only Solution: Collection is in BCNF as there is only one
(c) III only (d) III and IV only functional dependency Title Author → Catalog_no
(GATE 2008: 1 Mark) and {Author, Title} is key for collection.
Book is not in BCNF because Catalog_no is not a Book is in 2NF because every non-prime attribute
key and there is a functional dependency Catalog_ of the table is dependent on either the key [Title,
no → Title Author Publisher Year. Author] or another non-prime attribute.
Book is not in 3NF because non-prime attributes Ans. (c)
(Publisher Year) are transitively dependent on key Linked Answer Questions 35 and 36: Consider the
[Title, Author]. following ER diagram:
M1 M2 M3 P1 P2 N1 N2
M R1 P R2 N
36. Which of the following is a correct attribute set And the schedule T2T1 has the following sequence
for one of the tables for the correct answer to the of operations:
above question? R2[x]R2[y]W2[y]R1[x]W1[x]W1[y]
(a) {M1, M2, M3, P1}
The schedule S2 is conflict-equivalent to T2T1 and
(b) {M1, P1, N1, N2}
S3 is conflict-equivalent to T1T2.
(c) {M1, P1, N1}
Only S2 and S3 are conflict serialzable schedules.
(d) {M1, P1}
To test for conflict serializability, make the wait
(GATE 2008: 2 Marks)
for graph. If there is a cycle in graph, it means not
Solution: conflict serializable.
For R1 correct attribute set: M1, M2, M3, P1 Ans. (b)
For R2 correct attribute set: N1, N2, P1, P2 with P1
38. The following key values are inserted into a B+ tree
as primary key and N1 as weak entity set.
in which order of the internal nodes is 3, and that of
Ans. (a)
the leaf nodes is 2, in the sequence given below. The
37. Consider two transactions T1 and T2, and four order of internal nodes is the maximum number of
schedules S1, S2, S3, S4 of T1 and T2 as given below: tree pointers in each node, and the order of leaf
nodes is the maximum number of data items that
T1: R1[x]W1[x]W1[y] can be stored in it. The B+ tree is initially empty.
T2: R2[x]R2[y]W2[y] 10, 3, 6, 8, 4, 2, 1
0 `AC’ 8200
T1 T2 T3
1 `AC’ 8201
Read(X )
2 `SC’ 8201
Read(Y )
5 `AC’ 8203
Read(Y )
1 `SC’ 8204
Write(Y )
3 `AC’ 8202
Write(X )
What pids are returned by the following SQL query Write(X )
for the above instance of the tables?
Read(X )
SELECT pid
FROM Reservation WHERE class = ‘AC’ AND Write(X )
Which one of the schedules below is the correct multiple accounts or joint accounts. This attri-
serialization of the above? bute stores the primary account number.
(a) T1 → T3 → T2 (b) T2 → T1 → T3 IV. Name: Name of the Student.
(c) T2 → T3 → T1 (d) T3 → T1 → T2 V. Hostel_Room: Room number of the hostel.
(GATE 2010: 2 Marks) Which of the following options is INCORRECT?
(a) BankAccount_Number is a candidate key.
Solution: T1 can complete before T2 and T3 as
(b) Registration_Number can be a primary key.
there is no conflict between Write(X) of T1 and
(c) UID is a candidate key if all students are from
the operations in T2 and T3 which occur before
the same country.
Write(X) of T1.
(d) If S is a superkey such that S ∩ UID is NULL
then S ∪ UID is also a superkey.
T3 can complete before T2 as the Read(Y) of T3
does not conflict with Read(Y) of T2. Similarly,
(GATE 2011: 1 Mark)
Write(X) of T3 does not conflict with Read(Y) and
Write(Y) operations of T2. Solution: When two students hold joint account
After topologically sorting, the sequence is T1 → in that case BankAccount_Num will not uniquely
T3 → T2. determine other attributes.
Ans. (a) Ans. (a)
46. The following functional dependencies hold for 48. Database table by name Loan_Records is given
relations R(A, B, C) and S(B, D, E) below:
B → A,
Borrower Bank_Manager Loan_Amount
A→C
Ramesh Sunderajan 10000.00
The relation R contains 200 tuples and the rela-
tion S contains 100 tuples. What is the maximum Suresh Ramgopal 5000.00
number of tuples possible in the natural join R S? Mahesh Sunderajan 7000.00
(a) 100 (b) 200
(c) 300 (d) 2000 What is the output of the following SQL query?
(GATE 2010: 2 Marks) SELECT count(*) FROM(
(SELECT Borrower, Bank_Manager FROM
Solution: B is a candidate key of R.
Loan_Records) AS S NATURAL JOIN
So, all 200 values of B must be unique in R. (SELECT Bank_Manager, Loan_Amount
There is no functional dependency given for S. FROM Loan_Records) AS T );
For the maximum number of tuples in output,
(a) 3 (b) 9
there are two ways.
(c) 5 (d) 6
(i) All 100 values of B in S are same and there is (GATE 2011: 2 Marks)
an entry in R, which matches with this value.
So, we are having 100 tuples in output.
Solution: Output as Table S:
(ii) All 100 values of B in S are different and these
values are present in R also. So, we are having Borrower Bank_Manager
100 tuples.
Ans. (a) Ramesh Sunderajan
Suresh Ramgopal
47. Consider a relational table with a single record
for each registered student with the following Mahesh Sunderajan
attributes:
Output as Table T:
I. Registration_Number: Unique registration
number for each registered student. Bank_Manager Loan_Amount
II. UID: Unique Identity number, unique at the Sunderajan 10000.00
national level for each citizen.
III. B ankAccount_Number: Unique account Ramgopal 5000.00
number at the bank. A student can have Sunderajan 7000.00
Natural Join of S and T: 50. Which of the following statements are TRUE
about an SQL query?
Borrower Bank_Manager Loan_Amount
I. An SQL query can contain a HAVING clause
Ramesh Sunderajan 10000.00 even if it does not have a GROUP BY clause.
Ramesh Sunderajan 7000.00 II. An SQL query can contain a HAVING clause
only if it has GROUP BY clause.
Suresh Ramgopal 5000.00 III. A ll attributes used in the GROUP BY clause
Mahesh Sunderajan 10000.00 must appear in the SELECT clause.
IV. Not all attributes used in the GROUP BY
Mahesh Sunderajan 7000.00 clause need to appear in the SELECT clause.
(a) I and III (b) I and IV
Ans. (c)
(c) II and III (d) II and IV
49. Consider a database table T containing two col- (GATE 2012: 1 Mark)
umns X and Y each of type integer.
Solution:
After the creation of the table, one record (X = 1,
Y = l) is inserted in the table. I. HAVING clause can also be used with aggregate
function. If we use a HAVING clause without
Let MX and MY denote the respective maximum
a GROUP BY clause, the HAVING condi-
values of X and Y among all records in the table at
tion applies to all rows that satisfy the search
any point in time. Using MX and MY, new records
condition.
are inserted in the table 128 times with X and Y
values being MX + 1, 2 * MY + 1, respectively. II. To verify S,
It may be noted that each time after the insertion, CREATE TABLE temp (id INT, name
values of MX and MY change. VARCHAR(100));
What will be the output of the following SQL INSERT INTO temp VALUES (1, "abc");
query after the steps mentioned above are carried INSERT INTO temp VALUES (2, "abc");
out?
INSERT INTO temp VALUES (3, "bcd");
SELECT Y FROM T WHERE X = 7;
INSERT INTO temp VALUES (4, "cde");
(a) 127 (b) 255 SELECT Count(*) FROM temp GROUP BY name;
(c) 129 (d) 257
(GATE 2011: 2 Marks) Result:
count(*)
Solution: 2
The value of X is calculated using MX + 1 and Y
1
is calculated using 2 × MY + 1
1
For example, X = 1 and Y = 1
Ans. (b)
at the next step, X = MX + 1 = 1 + 1 = 2,
Y = 2 × MY + 1 = 2 × 1 + 1 = 3 51. Given the basic ER and relational models, which of
the following is INCORRECT?
52. Which of the following is TRUE? Solution: Two schedules are conflict-serializable
if the actions belong to different transactions, one
(a) Every relation is 3NF is also in BCNF.
of the actions is a write operation, the actions
(b) A relation R is in 3NF if every non-prime attri-
access the same object (read or write).
bute of R is fully functionally dependent on
every key of R. Two schedules are conflict-equivalent if both sched-
(c) Every relation in BCNF is also in 3NF. ules involve the same set of transactions, and the
(d) No relation can be in both BCNF and 3NF. order of each pair of conflicting actions in both
(GATE 2012, 1 mark) schedules is the same.
Take the following schedule:
Solution: Any relation that is in `x’NF, will auto-
matically be in `x-1’ NF. Now, let us see the order of S: r1(P); r1(Q); r2(Q); r2(P); w1(Q);w2(P)
NF as we discussed in the text, it is — 1NF, 2NF, 3NF,
BCNF, 4NF and 5NF. Therefore, option (c) is correct.
Ans. (c)
53. Suppose R1(A, B) and R2(C, D) are two relation
schemas. Let r1 and r2 be the corresponding rela- T1 T1
tion instances. B is a foreign key that refers to C in
R2. If data in r1 and r2 satisfy referential integrity
constrains, which of the following is ALWAYS
TRUE?
(a) ΠB(r1) − ΠC(r2) = ∅
Cycle exists between two transactions, so T1 and
(b) ΠC(r2) − ΠB(r1) = ∅
T2 are not conflict serializable.
(c) ΠB(r1) − ΠC(r2) A schedule is conflict-serializable when the schedule
(d) ΠB(r1) − ΠC(r2) ≠ ∅ is conflict-equivalent to one or more serial sched-
(GATE 2012: 2 Marks) ules. There are two possible serial schedules–T1
followed by T2 and T2 followed by T1. In both,
Solution: B is a foreign key in r1, which refers to one of the transactions reads the value written by
C in r2. another transaction as a first step.
r1 and r2 satisfy referential integrity constraints. Ans. (b)
So, every value that exists in column B of r1 must Common Data Questions 55 and 56: Consider the
also exist in column C of r2. following relations A, B and C:
Ans. (a)
A
54. Consider the following transactions with data Id Name Age
items P and Q initialized to zero:
12 Arun 60
T1: read (P); 15 Shreya 24
read (Q); 99 Rohit 11
if P=0 then Q:=Q+1;
write (Q). B
T2: read (Q); Id Name Age
read (P) 15 Shreya 24
if Q=0 then P:=P+1; 25 Hari 40
write (P). 98 Rohit 20
Any non-serial interleaving of T1 and T2 for con-
99 Rohit 11
current execution leads to
(a) a serializable schedule C
(b) a schedule that is not conflict-serializable
(c) a conflict-serializable schedule Id Phone Area
(d) a schedule for which precedence graph cannot 10 220 02
be drawn
99 2100 01
(GATE 2012: 2 Marks)
55. How many tuples does the result of the following 57. An index is clustered, if
relational algebra expression contain? Assume that
(a) It is on a set of fields that form a candidate key.
the schema of A ∪ B is the same as that of A.
(b) It is on a set of fields that include the primary key.
(A ∪ B) A.Id> 40 C.Id <15 C (c) The data records of the file are organized in the
same order as the data entries of the index.
(a) 7 (b) 4 (d) The data records of the file are organized not in
(c) 5 (d) 9 the same order as the data entries of the index.
(GATE 2012: 2 Marks)
(GATE 2013: 1 Mark)
59. How many candidate keys does the relation R have? (c) In 3NF, but not in BCNF
(d) In BCNF
(a) 3 (b) 4
(c) 5 (d) 6 (GATE 2013: 2 Marks)
(GATE 2013: 2 Marks)
Solution: A → BC, B → CFH and F → EG are
Solution: Candidate keys AD, BD, ED and FD
partial dependencies.
Ans. (b)
So, they are in 1NF but not in 2NF.
60. The relation R is Ans. (a)
(a) In 1NF, but not in 2NF
(b) In 2NF, but not in 3NF
PRACTICE EXERCISES
Set 1
(c) If we modify the conceptual schema, then there 20. The value of particular field should be less than 50
is no need to modify the external schema. if it is
(d) If we modify the middle level schema, then
(a) An integrity constraint.
any one schema (internal/external) may be
(b) A referential constraint.
modified.
(c) A primary key constraint.
13. Relational algebra can be defined as (d) A normalization constraint.
(a) Read and destroy 22. The cardinality ratio and participation
(b) Read and develop of a table helps us to implement either the cross
(c) Rapid application development referencing design or mutual referencing design.
(d) Rapid application design
(a) Functionality (b) Constraints
16. In DML, RECONNCT command cannot be used (c) SQL queries (d) Domains
with
23. Consider the following schemas:
(a) CLEAN set. (b) FIXED set.
(c) OPTIONAL set. (d) USER set. Branch = (Branch-name, Assets, Branch-city)
Customer = (Customer-name, Bank name,
17. The UWA is used to communicate the contents of
Customer-city)
individual records between
Borrow = (Branch-name, loan number,
(a) Host program and present record. customer account-number)
(b) Host program and host record. Deposit = (Branch-name, Accountnumber,
(c) Host program and DBMS. Customer-name, Balance)
(d) Host program and host language.
Using relational algebra, the query that finds cus-
18. Referential integrity is concerned with tomers having deposits have accountnumber >
23456 is
(a) Primary key. (b) Foreign key.
(c) Alternate key. (d) Project_Join key. (a) Πcustomer-name (s account-number > 23456 (Deposit)
(b) s customer-name (s account-number > 23456 (Deposit)
(c) Πcustomer-name (s account-number > 23456 (Borrow)
19. Match the following:
(d) s customer-name (Π account-number > 23456 (Borrow)
List — I List — II
I. DDL A. LOCK TABLE 24. What deletes the entire file except the file structure?
II. DML B. COMMIT (a) DELETE
III. TCL C. Natural Difference (b) DELETE RECORDS
(c) ZAP
IV. BINARY D. REVOKE
(d) RETAIN STRUCT
Operation
25. The function of a Transaction Manager is to
Codes:
I. Maintain a log of transactions.
I II III IV II. Maintain database images before and after a
(a) A D B C transaction.
(b) A B D C III. Maintain appropriate concurrency control.
(c) D B A C IV. Avoid deadlocks.
(d) D A B C
(a) I, II, III and IV (b) II and III 32. A network schema is used to
(c) I, III and IV (d) I, II and III
(a) restrict to many-to-many relationship.
(b) permit many-to-many relationship.
26. The term granularity relates to
(c) permit to store data in a database.
(a) the size of table. (d) help in storing one-to-many relationship.
(b) the size of data item.
(c) the size of tuple. 33. Storing under unified scheme at a single site is
(d) the size of primary key called as
(a) data mining. (b) universal database.
27. Match the following: (c) data warehousing. (d) advanced database.
34. The task of correcting and cleaning data is called as
I. OLAP A. Back propagation
(a) data organization. (b) pre-processing.
II. OLTP B. Data warehouse
(c) data mining. (d) data structuring.
III. Decision tree C. RDBMS
35. A clustering index consists of .
IV. Neural network D. Classification
(a) declared and ordered primary key
(b) both grouped and ordered primary key and for-
I II III IV eign key
(c) ordered foreign key
(a) D C A B
(d) grouped and ordered candidate key and alter-
(b) B C D A nate key
(c) A B C D
36. Consider the following schema:
(d) D B A C
Emp(Empcode, Name, Sex, Salary, Deptt)
A simple SQL query is executed as follows:
28. Which of the following statements is TRUE?
SELECT Deptt FROM Emp
(a) A prime attribute can be transitively depen-
dent on a key in 5NF relation. WHERE sex = ‘M’
(b) A prime attribute can be transitively depen- GROUP by Dept
dent on a key in 3NF relation.
HAVING avg (Salary) > {select avg (Salary)
(c) A prime attribute can be transitively depen-
from Emp}
dent on a key in PJNF relation.
(d) A prime attribute cannot be transitively depen- The output will be
dent on a key in BCNF relation.
(a) a verage salary of male employee is the average
29. Which normal form is considered as adequate for a salary of the organization.
simple database design? (b) average salary of male employee is less than the
average salary of the organization.
(a) 2NF (b) 3NF
(c) average salary of male employee is equal to the
(c) BCNF (d) PJNF
average salary of the organization.
30. Which of the following is not a type of DBMS? (d) average salary of male employees in a depart-
ment is more than the average salary of the
(a) Hierarchical (b) Network organization.
(c) Relational (d) Graphical
37. Consider a relation X(p, q, r), and the domains are
31. Manager’s salary details are to be hidden from represented by their atomic values. Further, the
Employee Table. This technique is called as functional dependency p → q, q → r is observed,
the relation is in
(a) internal level datahiding.
(b) physical level datahiding. (a) 1NF, not in 2NF
(c) external level datahiding. (b) 2NF, not in 3NF
(d) logical level datahiding. (c) 3NF
(d) 1NF, not in 3NF
(a) In First normal form but not in Second normal 5. Consider the scheme R = (S T U V) and the
form. dependencies S → T, T → U, U → V and V → S.
(b) In Second normal form but not in Third normal Let R = (R1 and R2) be a decomposition such that
form. R1 ∩ R2 = f. The decomposition is
(c) In Third normal form. (a) Not in 2NF
(d) None of the above. (b) In 2NF but not 3NF.
2. Let R(a, b, c) and S(d, e, f) be two relations in (c) In 3NF but not 2NF.
which d is the foreign key of S that refers to the (d) In both 2NF and 3NF.
primary key of R. Consider the following four oper- 6. Consider a schema R(A, B, C, D) and functional
ations on R and S: dependencies A → B and C → D. Then the decom-
I. Insert into R II. Insert into S position of R into R1(AB) and R2(CD) is
III. Delete from R IV. Delete from S (a) Dependency preserving and lossless join.
Which of the following is true about the referential (b) Lossless join but not dependency preserving.
integrity constraint above? (c) Dependency preserving but not lossless join.
(d) Not dependency preserving and not lossless join.
(a) None of I, II and IV can cause its violation.
(b) All of I, II, III and IV can cause its violation. 7. Relation R with an associated set of functional
(c) Both I and IV can cause its violation. dependencies, F, is decomposed into BCNF. The
(d) Both II and III can cause its violation. redundancy (arising out of functional dependencies)
in the resulting set of relation is
3. There are five records in a database
(a) Zero.
Name Age Occupation Category (b) More than zero but less than that of an equiva-
lent 3NF decomposition.
Rama 27 CON A
(c) Proportional to the size of F+.
Abdul 22 ENG A (d) Indeterminate.
8. From the following instance of a relation scheme 13. Which of the following relational calculus expres-
R(A, B, C), we can conclude that sions is not safe?
(b) disk access is much slower than memory access. student names are of length 8 bytes, disk blocks are
(c) d
isk data transfer rates are much less than of size 512 bytes and index pointers are of size 4
memory data transfer rates. bytes. Given this scenario, what would be the best
(d) disks are more reliable than memory. choice of the degree (i.e., the number of pointers
per node of the B+ tree?
18. A B+ tree index is to be built on the name attri-
bute of the relation STUDENT. Assume that all
Set 1
1. (a) 8. (d) 15. (c) 22. (b) 29. (b) 36. (d)
2. (b) 9. (a) 16. (b) 23. (a) 30. (d) 37. (a)
3. (d) 10. (b) 17. (c) 24. (c) 31. (c) 38. (b)
4. (d) 11. (c) 18. (b) 25. (d) 32. (b)
5. (a) 12. (b) 19. (d) 26. (b) 33. (c)
6. (d) 13. (a) 20. (a) 27. (b) 34. (b)
7. (b) 14. (a) 21. (a) 28. (d) 35. (a)
Set 2
1. (a) The relation has the following functional Candidate keys for this relation are {S, T, U, V }
dependencies: As given R1 ∩ R2 = f
a→c So, R1 and R2 might have two attributes. A rela-
b→d tion with two attributes satisfies both 2NF and
{a b} is key for the given relation. 3NF conditions.
Here prime attributes are a and b. 6. (c) Dependency is preserved because there is no
loss of functional dependencies.
c and d are dependent on partial keys, this is not Since R1(AB) and R2(CD) do not have any common
allowed in 2NF. So, the given relation is in 1 NF. attribute. So, it is lossy join.
2. (d) Insertion into S and deletion from R can cause 7. (a) Redundancy is zero when we decompose into
inconsistency. So, II and III can cause violation. BCNF.
3. (c) The index is built from field occupation where 8. (b, c) F: X → Y implies that for any two tuples if
column is sorted in alphabetic order CON, DOC, t1[X] = t2[X], they must have t1[Y] = t2[Y].
ENG, SER and MUS. Index is assigned according
9. (d) DISTINCT key word is used to remove dupli-
to alphabetic position.
cate rows along with SELECT keyword. SQL does
4. (a) 2NF, (b) 3NF not permit attribute names to be repeated in the
same relation.
5. (d) The relation schema R = (S T U V) has follow-
ing functional dependencies 10. (a) Cross join with an empty relation produces the
S→T overall result as empty. Therefore, S is not empty. `r’
should not have any duplicates to have same rows as `r’.
T→U
U→V 11. (a) s c (s c (R)) and (s c (s c (R))) will not be equiv-
1 1 2 1
12. (c) Tuple calculus expression select tuples where Leaf nodes are linked together in B+ trees, hence
A = 10 and B = 20 of relation R. So, the equiva- range queries are faster.
lent relational algebra expression is s(A = 10(r) ∩
sB = 20(r). 17. (b) In B+ trees, disk access is slow and numbers of
hits are less. B+ trees have very high fan-out (typi-
13. (c) The query −t|¬ (t ∈ R1) is syntactically cor- cally on the order of 100 or more), which reduces
rect. But it requests for all tuples that are not in the number of I/O operations required to find an
R1. That set of such t tuples is obviously infinite, element in the tree.
in the context of such as the infinite domain set of 18. (43) Let n be the degree.
all integers. Therefore, this is unsafe query.
Given k key size (length of the name = 8 byte attri-
14. (c) Both relational algebra and relational calculus bute of student)
has same expressive powers. Every query written
in relation algebra can be expressed in relational Disk block size, B = 512 bytes
calculus. Index pointer size, b = 4 bytes
15. (d) Given schedule has W1(A), R2(A) and W2(B), Degree of B+ tree can be calculated if we know
R1(B) conflicting pairs. So the schedule is not seri- the maximum number of key an internal node can
alizable. Hence, it cannot occur in scheme using have; the formula for that is
2PL protocol. (n − 1)k + n × b = Block size
16. (b) Most database systems use indexes built on (n − 1)8 + n × 4 = 512
some form of a B+ tree due to its many advan- 12n = 520
tages, in particular its support for range queries. n = 520/12 = 43
4
Number of questions
Marks 1
2 Marks 2
Total number of questions
1
0
2015 2014 2013 2012 2011 2010
Year Concept
2015 Function point metric, Seeded faults, McCabe's complexity, COCOMO
Model, SRS document, Code walkthrough and inception, Testing
2014 Cohesion and coupling
2013 Cohesion and coupling
2012 IEEE floating-point representation
2011 Lines of code, COCOMO model, SRS, black-box testing
2010 Software development life cycle
Syllabus: Information gathering, Requirement and feasibility analysis, Data flow diagrams, Process specifications,
Input/output design, Process life cycle, Planning and managing the project, Design, Coding, Testing, Implementation,
Maintenance.
Software engineering is the study of design, develop- Information system consists of components such
ment and maintenance of software. Real-time problems as storing and processing data. All business firms
are so large and complex that they cannot be solved and organisations rely on them to carry out and
by single developer. Software engineering defines teams manage their operations, interact with their custom-
and its quality. It is concerned about all the aspects of ers and suppliers, and compete in the market. For
software development from feasibility of software to its example, these organisations could reach their cus-
maintenance. The team in software engineering consists tomers through Internet for the processing of finan-
of developers, testers, designers, customers, managers, cial accounts and manage their human resources.
etc. Every team member works to fulfil task assigned Other users can rely on information system through
to him. online services such as social networking, auctions,
Executive
information
systems
Decision
support systems
Management
reporting systems
Management Support
banking, entertainment, shopping, etc. Information communications among employees. OIS is also known as
and knowledge have become vital economic resources. office automation. With the help of OIS, employees or
Information systems support operations, knowledge users can perform tasks electronically using computers
work and management in organisations (Fig. 9.1). and other electronic devices, instead of manually pro-
cessing data. For example, a registration department can
9.2.1 Components of Information Systems post the class schedule via email to students, whereas
in a manual system, the registration department would
The main components of information systems are computer have to photocopy the schedule and mail it to each stu-
hardware and software, telecommunications, databases and dent’s house.
data warehouses, human resources and procedures (Fig. 9.2).
OIS supports a large range of business office activities
The hardware, software and telecommunications constitute
such as creating and distributing graphics and/or docu-
information technology (IT), which is now ingrained in the
ments, sending messages, scheduling and accounting. All
operations and management of organisations.
levels of users utilise and benefit from the features of
an OIS.
Environment
Effector The software used in OIS includes word processing,
(output) spreadsheets, databases, presentation graphics, email,
Processor Memory Web browsers, Web-page authoring, personal informa-
Receptor tion management and groupware. OIS uses communica-
(input)
tion technology such as voice mail, fax, video conferencing
and electronic data interchange (EDI) for the electronic
Figure 9.2 | Structure of an information system.
exchange of text, graphics, audio and video. The hard-
ware includes computers, modems, video cameras, speak-
ers and microphones, scanners and fax machines.
9.2.2 Types of Information Systems
Office information system (OIS) uses hardware, soft- A transaction processing system (TPS) captures and pro-
ware and network to enhance the workflow and facilitate cesses data generated during an organisation’s day-to-day
transactions. A transaction is a business activity such as 1. Internal sources of data: A data from an organ-
a deposit, payment, order, reservation, etc. isation such as manufacturing, inventory, sales or
Transaction processing includes the following activities: financial data.
2. External source of data: A data from other
1. Recording a business activity such as registration, organisations such as population trends, interest
order, payment, etc. rates, construction rates, price of raw material, etc.
2. Confirming an action or triggering a response such
as printing documents, sending a response, gener- DSS may also include query language, statistical analy
ating pay cheque, issuing receipts, etc. sis capabilities, spreadsheets and graphics for the
3. Maintaining data such as adding new data, modi- extraction and evaluation of data, if it is very large.
fying data or deleting data. DSS includes some kind of capabilities for the creation
of a model of the factors affecting the decision. In these
For the processing of business data, TPS was the first models the user can change the values of all or few
computerised system developed as a function called data parameters and project the results. There are lots of
processing. It is an existing manual system, which allows application software packages which are used for the
faster processing, reduced clerical costs and improved decision-making process such as executive information
customer service. system (EIS), which supports the information needs of
Online transaction processing (OLTP) is an example of executive management. EIS uses data in the form of
TPS. The administrative users use OLTP to enter data and charts or tabular forms to show trends, ratios and other
take print out of the entered data in the form of receipts. managerial statistics.
Decision support system (DSS) is a computer-based As technology widens its horizons, it becomes difficult
information system designed to support decision- to classify a system belonging uniquely to one of the
making activities such as business or organisation. above five types of information system. Today’s applica-
A variety of DSSs exist having support of wide range tions supporting processing of transactions, management
of decisions. It uses data from two sources: internal of information and decision-making need an integrated
and external. information system.
Types of Information Systems 2. Software does not wear out, but deteriorates. In
hardware design, at early development, the failure
The following are six major types of information sys-
rate is high due to undiscovered errors. After cor-
tems corresponding to each organisational level (the four
recting the defects, the failure rate is decreased and
levels shown in Fig. 9.1):
the component starts running at a steady state.
1. Transaction processing systems (TPS) to serve the However, over a period of time, due to external
operational level of an organisation. conditions, such as temperature, air, dust and envi-
2. Knowledge work systems (KWS) to keep an orga- ronmental maladies, the failure rate again increases
nization up-to-date in knowledge in terms of tech- and the hardware starts to wear out. When it
nology, science, art and social thoughts. undergoes wear out, we can replace the component
3. Office automation systems (OAS) to serve the with a new one. In software design, at early devel-
knowledge level of an organisation. opment, the failure rate is high due to undiscov-
4. Decision-support systems (DSS) to analyze the ered errors. After correcting the defects the failure
existing information to forecast the effects of their rate is decreased and the software starts running
decisions in the near future. within the constrained level. After a period of time,
5. Management information systems (MIS) to serve another change is injected in the existing soft-
the management level of the organisation. ware due to which the failure rate again increases,
6. Executive support systems (ESS) to serve the stra- and after correction the failure rate decreases. So,
tegic level of an organisation. during the lifetime of software, when the require-
ments keep changing, the software undergoes dete-
9.3 SOFTWARE rioration (Fig. 9.3).
3. The industry is moving towards component-based
development, but software is still being custom-
Software is a program, and when it is executed the built. As components are error-free codes, they are
desired functionality and performance must be satis- reusable. With component-based development for
fied. Software is a data structure used to manipulate developing some hardware, time and development
the information. It is a document that describes the cost are reduced as the design consists of IC num-
operation and use of a program. Finally, it is a logical bers which are already available. So, at the time
entity rather than physical entity. of implementation we can directly use them. For
softwares also, we should use reusable components.
9.3.1 Characteristics of Software Software development also uses component names
such as:
The following are the characteristics of a software:
1. Off the shelf: This component is used in the proj-
1. Software is developed or engineered, but not manu- ect from the third party, that is, the component is
factured in classical sense. Although the develop- not available in the library of the developing organ-
ment approach for both the hardware design and isation.
software design is same, but after implementation 2. Fully experienced and partially experi-
of the hardware design we get the physical com- enced: This component is derived based on the pre-
ponent and after implementation of the software vious project, so can be directly used in the current
design we get the logical component. In both cases project.
quality is an important factor, that means when 3. New component: This component is developed
the output is operational the associated functional- from the base line, means from the initial stage or
ity must be satisfied. scratch.
F
a Deteriorate change
i Reengineer the software
l
u Wear out
r
e
r Steady change
a
t
e Actual curve
Time Time
Figure 9.3 | Wear out and deterioration of software.
9.3.2.2 Software Process
Process model
Conventional Evolutionary
(Static) (Dynamic)
Component-based
Waterfall Prototype RAD Incremental Spiral
development
Time
Figure 9.8 | RAD model.
Planning
Customer
communication Risk analysis
Entry point
Engineering
Customer
evaluation
1. In the spiral model, the entry point is the customer 5. In the spiral model, customer satisfaction is high,
communication to finalise the SRS. In the planning and at the same time the designer is able to design
stage, we can prepare the estimation model to esti- efficient design templates and deliver the code on
mate the cost schedule and performance. budget. So, this model is called win-win spiral
2. In the risk analysis stage, we can identify the tech- model.
nical and management risks. In the engineering
stage, we can select the effective process model to
develop the software. 9.4.2.3 Component-Based Development Model
3. In the construction and release stage, the proce
dural description is converted into machine- In the component-based model, the components are used
readable format and test cases also implemented. in the project development (Fig. 9.11).
Later, the software is delivered to the customer. After designing the project, before constructing the
4. In the customer evaluation stage, the software final software check the availability of the components
undergoes operation. During operation the cus- in the library. If the component is available, use it in
tomer identifies the change requirement and posts the current project; if not, construct the new compo-
it to the development team. The change is reflected nent and place it in the library, then move to the next
in the next release of the software. iteration.
Planning
Customer
communication
Risk analysis
Customer Indentified
evaluation candidate
component
Engineering
Look up
Construction and release component
in library
Construct Put the Build the Extract
the nth component in component if component if
iteration the library not available available
9.5 MEASUREMENT OF METRICS 2. Project metrics: It describes the project char-
acteristics and execution. For example, effort cost
and schedule; effort in terms of man-months. Effort
Measurement is a quantitative indication which shows of a project is 3 man-months means one developer
the size or complexity of a process. works 3 months to develop the software.
3. Process metrics: It describes the characteristics
9.5.1 Metrics of the process model to develop and maintain the
software (Fig. 9.12). For example, calculates the
Metrics is a quantitative measure of an attribute of the defect rate and defect removal efficiency.
project or process. All the estimation models are empirical modules, that
Measurement is divided into two types: means, the models are developed based on the past expe-
1. Direct measurement: In the direct measure- rience without proof. Even though these models lead to
ment, the value is calculated based on direct success scenario of the software development.
approach. Example: Size of a project can be calcu-
lated based on the LOC (line of code).
2. Indirect measurement: Indirect measurement Size
SRS estimation Some other
means the value can be calculated based on the parameters
other parameters. Example: Size of a project can be
calculated by using the function point.
Effort
Software supports with four kind of P’s: estimation
1. People Schedule Cost
2. Product estimation estimation
3. Project
E = A + B * (ev)c
4. Process LOC (KLOC)
Size
On the basis of the four P’s, the software metrics are
classified into the following three types: A = Adding factor PP
B = Scaling factor Empirical
1. Product metrics: This metrics describes the constants
C = Exponential factor
characteristics of the product such as size, com-
plexity, performance and quality level. Figure 9.12 | Process metrics – an illustration.
The size of a project can be calculated using the follow- Attribute Value Weighing Factor
ing two ways: Simple Average Complex
1. Line of code (LOC)
2. Function point analysis (FPA) I/P files 3 4 5
O/P files 4 5 7
9.5.2.1 Line of Code
Enquiries 3 4 6
If we want to estimate the size of a project, there is a
need of taking the past project experience. Assume the
Files 7 10 15
LOC into three categories:
1. Optimistic LOC (Sopt) External
5 7 10
2. Most likely LOC (Sm) interfaces
3. Permissible LOC (Spess)
Count value = Values × Weighing factor
After identifying three levels of the source code, we can
FP = Count value × VAF
estimate the size of the project by using this following
formula: Apart from the functional requirements, some other
parameters are also involved in the project. So, we need
Sopt + 4Sm + Spess
S= to consider the effects of these factors on the project.
∑
6
VAF = 0.65 + 0.01 × Fi
P =1 to 14
Problem 9.1: Consider a three-dimensional (3D) geo- where VAF is value adjustment factor.
metric application. The range of LOC estimated is
Sopt = 5600, Sm = 7900 and Spess = 9600. What is the Note: On the basis of the knowledge of the previous
expected size? project, the weighing table is maintained as a simple/
average/complex case. Calculate the count value by pro-
Solution: viding the multiplication between attribute values with
5600 + 31600 + 9600 46800 the corresponding complexity levels. Default value of
S=
6
=
6
= 7800
∑ FFi == 42
∑ 42, if value adjustment complexity values
i
P =1 to 14
So, the estimated size = 7800 LOC = 7.8 KLOC P =1 to 14
are not given, use the default value.
Problem 9.3: Consider 3D geometry application. Problem 9.5: Software development is expected to
The size of the project can be measured in terms of involve eight parameters of efforts. Calculate the
FP with the following parameters: following using SEL and WF models.
USER (a) The number of lines of code that can be produced.
I/O 3 × 4 = 12 (b) The duration of the development
(c) The productivity in LOC per person-year
O/P 2 × 5 = 10 (d) The average manning (person)
Enquiries 2 × 4=8
Solution:
Files 1 × 10 = 10
By SEL method:
External interfaces 4 × 7 = 28
(a) E = 1.4 (KLOC)0.93
Assume the project complexity is average and value 8P − 4 = 1.4 (KLOC)0.93
adjustment complexity is 1. Calculate the FP.
(8 × 12) 96
Solution: (KLOC)0.93 = =
1.4 1. 4
FP = 68 × (0.65 + 0.01 × 14)
96 1/ 0.93
68 × (0.65 + 0.14) KLOC =
1.4
= = 90.0414
= 68 × (0.79) So, LOC = 90041
= 53.73 (b) D = 4.1 (KLOC)0.36 = 4.6 × (90.0414)0.26
= 4.6 × 3.22 = 14.8 ∼ 15 months
90041
Problem 9.4: The effort of the above project is (c) Productivity = = 11255
8
5 person-months. What is the productivity? (d) Manning = Effort/Duration = (8 × 12)/15 person-
Solution: months = 6.4 persons
Given that 5 persons-month ⇒ 53.72
By WF method:
(a) E = 5.2 (KLOC)0.91
5 person-months means that the work done by 5 persons
in 1 month. 8P − 4 = 5.2 (KLOC)0.91
Person 5 → 53.72 (8 × 12) 96
(KLOC)0.91 = = = 18.46
Person 1 → 53.72/5 = 10.74 5. 2 5. 2
96 1/ 0.91
Productivity = 10.74 KLOC =
5.2
= 24.63
or we can say, 10.74 functional point developed by
1 person-month. So, LOC = 24630
(b) D = 4.6 (KLOC)0.26 = 4.6 × (24.63)0.26
= 4.6 × 2.3 = 10.6 ∼ 11 months
9.5.3 Effort and Schedule (Duration) Estimation 24630
(c) Productivity = = 3078.75
8
Effort and duration can be estimated by using different (d) Manning = Effort/Duration = (8 × 12)/11 person-
empirical model structure. months = 8.7 persons
1. According to the software engineering laboratory
(SEL) empirical model, the effort can be estimated
9.5.3.1 Constructive Cost Model
as follows:
E = 1.4 (KLOC)0.93 person-months (units) By using constructive cost model (COCOMO), we can
The duration can be calculated as calculate the effort and time, this model is also an empiri-
D = 4.6 (KLOC)0.26 months (units) cally derived model. Therefore, the structure of the effort
calculation is
2. According to Walstonald-Felio method (W-F) (IBM):
Effort = c1 × EAF ×(size)c2 (person-months)
The effort can be calculated as
E = 5.2 (KLOC)0.91 person-months Duration = c3 ×(Effort)c4 (months)
Duration can be calculated as where c1 and c3 are the empirically derived scaling fac-
D = 4.1 (KLOC)0.36 tors and c2 and c4 are empirically derived exponential
n = n1 + n2 = 13 + 5 = 18
9.6.1.2 Types of Risk
N = N1 + N2 = 31 + 23 = 54 1. Project risk: This risk threatens the project plan,
1 therefore schedule will be extended and develop-
D= ment cost will increase.
L
2. Technical risk: These risks threaten the quality
2n2 2×5 1 and timeliness of developing the software. When
L= = = = 30.30
n1 × N 2 13 × 23 0.033 this risk becomes real, we cannot develop the proj-
ect within the quality levels.
V = n⋅log2n = 54⋅log218 = 54 × 5 = 270
3. Business risk: These risks threaten the viability
of software risk. Under this category, four different
V 270
E= = = 8181.81 risks are defined:
L 0.033 •• Market risk: Build an excellent product but no users.
•• Strategic risk: Build a product that is no longer
fit into the business objective.
9.6 RISK ANALYSIS •• Management risk: Lack of high-level support to
inject the changes.
•• Budget risk: No synchronisation between cost
Risk means extreme condition when the product becomes and price.
a failure. Risk is associated with two characteristics: 4. Known risk: Uncover errors after a careful evalu-
ation of the project.
1. Uncertainty: It means may/may not occur, 100%
5. Predictable risk: It is extrapolation from the
probable risk.
past project experience.
2. Loss: It means when the risk becomes real there is
6. Unpredictable risk: It acts as a joker in the deck.
a loss, otherwise no loss.
7. Generic risk: It describes the characteristics of the
project such as planning, performance, cost, effort, etc.
9.6.1 Risk Identification 8. Product-specific risk: These risks describe the
characteristic of the product such as the size, qual-
After identifying the extreme condition, there is a need ity, performance, etc.
of analyses of the risk in terms of level of uncertainty
and degree of loss. The level of uncertainty is maintained Risk Components
in the following four ways: There are four different components present in the soft-
1. Negligible ware to maintain the probability of probable risk:
2. Marginal 1. Performance: It indicates the level of uncertainty
3. Critical to meet its specification.
4. Catastrophic 2. Cost: It indicates the level of uncertainty to main-
tain project budget.
We are going to identify critical and catastrophic risk
3. Support: It indicates the level of uncertainty to
condition and try to manage them in the software execu-
easily correct, adopt or enhance the existing software.
tion with the help of risk strategy.
4. Schedule: It indicates the level of uncertainty to
maintain the schedule and quality of the software.
9.6.1.1 Risk Strategy
9.6.2 Risk Projection or Risk Estimation
There are two types of risk strategies:
1. Reactive risk strategy: It means monitoring To estimate the cost of the risk, there is a need to iden-
the project for the probable risk. When the risk is tify the probability of the risk becoming real and the
present during execution of the project, it imple- associated impact.
ments the plans to control the risks. It is also called
as fire-lighting mode. Risk Name Category Probability Impact
2. Proactive risk strategy: It means risk plans and
actions are developed before the implementation of
the software. So, by using these strategies we can By using the above database we can estimate the risk
avoid the risk, if possible, or manage the risk, if not cost by using the following formula:
possible. By combining both proactive and reac- Risk exposure (RE) = P × C
tive strategies, the risk mitigation, monitoring and where P is the probability of risk becoming real and C
management (RMMM) plan is prepared. is the cost.
Requirement Requirement
development management
process process
SRS Elicitation Change-
Analysis control
Specification Version
control
Validation Req tracing
Validation
Data level testing
Design Elaboration
Architecture level
Interface level Integration testing
Component level Top down
Bottom up
Regresser
Smoke
Logical component
Functional
Behavioural
Test Transition Development test Black box
cases Grey box
Error free
α - testing — Graph based
β - testing logical component — Boundary value analysis
Corrective — Equivallence partioning
Maintenance Adaptive Structural
Perfective White box
Preventive Glass box
— Basis path
Recovery testing
System Security testing — Control structure
testing Stress testing
Performance testing
Figure 9.13 | The software development life cycle.
User Quality
requirement attribute
SRS
After reviewing the SRS document twice or thrice, diagram. E-R diagram consists of object, attribute
the customer and the developer sign on the SRS docu- and relationship.
ment. This document (SRS) is then forwarded to the Object is defined as a representation of com-
design face. posite elements. Composite elements must have
multiple properties. The property of the object
9.7.1.3 Elicitation is called as attribute. The relationship can be
maintained between the object by using two
In this state, by using different techniques, elicit the concepts.
requirement from different sources towards goal of •• Cardinality: It indicates in how many ways
the project. The different elicitation techniques are as one object can be communicated with another
follows: object. There are three types of cardinality:
1: 1
1. CORE (Controlled Requirement Expres
sion): In CORE technique, elicit the requirement 1: n
by using view point analysis. In this analysis, M: N
•• Modelling: It indicates “is the communication
maintain the sequence of data object, action and
consequence.
link between the object mandatory or optional”.
2. IBIS (Issue-based Information System): In
IBIS technique, elicit the requirement based on the 1 = Mandatory
question and answer analysis. This technique main-
tains the issue, position and justification. Modality
3. FODA (Future-oriented Domain Analy
sis): In FODA technique, elicit the requirement
0 = Optional
by creating different models to specify the struc-
ture of the end product. This technique maintains
the sequence context model – function model – Note: On the basis of the data model, the data
dynamic model (behaviour or data model). structure is created in the data level design.
4. QFD (Quality Function Deployment): In 2. Functional and information flow model:
QFD technique, elicit the requirement towards The functional and information flow model
quality deployment. describes the functionality of the end product, that
5. JAD (Joint Application Development): In means, what is the behaviour of the end product
JAD technique, elicit the requirement by conduct- with a proper set of input. The functionality can be
ing a quick meeting between customer, end user, represented by using the data flow diagram (DFD).
analyst and developers. Data flow diagram is also called as Bubble chart
6. Prototype: In prototype technique, elicit the or directed graph. Different levels of abstraction of
requirement by creating the quick design mock-ups the end product can be represented using different
and taking continuous feedback from the customer. levels of DFD.
•• Level-Zero DFD: It describes the high-level
Note: The output of the elicitation stage is data abstraction of the functionality by maintaining
dictionary, it consists of data objects related to the the single process bubble with multiple input-
software. and output-directed edges (Fig. 9.16).
9.7.1.4 Analysis
Input Slow Output
This stage describes the structure of the end product process
in terms of data object, functionality, information
flow and behaviour with respect to external event, by
creating three types of model: Figure 9.16 | Level-zero DFD.
1. Context or data model (E-R diagram)
2. Functionality and information flow model In this level of DFD, only the functionality is
(described by DFD) represented with absence of information flow.
3. Dynamic or behavioural model (described by STD) •• Level-1 DFD: It describes the functionality
1. Context or data model (E-R Diagram): This and information flow of the software by main-
model describes the structure of the end product in taining different process bubbles. It describes
terms of data object and relationship. Data model the project in low-level abstraction. Low-level
can be represented by using the entity, relationship abstraction is easy to code (Fig. 9.17).
9.7.2 Design
1 2
The design stage represents the project in the low-level
5 abstraction, that means, the customer description is
4 translated into technical description. To do this process,
3 the following four levels of design take place (Fig. 9.20):
1. Data level: Data level design describes the data
Figure 9.17 | Level-1 DFD. structure. It takes help from the E-R diagram and
data dictionary.
3. Dynamic model: It describes the behaviour of 2. Architecture level: It defines the structure of
the system with respect to external events. This the end product (skeleton). It takes help from the
model is represented using the state transition dia- DFD of the last stage.
gram (Fig. 9.18). 3. Interface level: Interface level design describes
the number of interfaces required in the system. It
Transition takes help from the state transition diagram.
4. Component level: It represents the procedural
State 1 State 2 description to implement the functionality in the
Figure 9.18 | State transition diagram.
software. It can take help from process specification
and control specification (Fig. 9.21).
•• Specification: In this stage, the models are
refined into control specification and process
specification
(a) Control specification: It shows the 4 Component
behaviour of the product. level
(b) Process specification: It refines the
mathematical expression, algorithm and 3 Interface level
constraints.
•• Validation: In this stage, various validation 2 Architecture level
criteria are implemented to validate the require-
ment towards the right condition. The following 1 Data level
reviews are done to validate the software:
Figure 9.20 | Levels.
(a) Configuration review
(b) Quick review
(c) Detailed review
“Validation means the black-box testing is per- Process specification
formed at the requirement stage itself”. 4
After validating the SRS document, it is forwarded
to the design stage. The output of the requirement
development process is shown in Fig. 9.19.
1 2
Process specification
Data
dictionary DFD
3
State transition
diagram
Data
dictionary 4
DFD
Process specification
State transition
diagram Figure 9.21 | Component.
Process specification
9.7.2.1 Designing Concept
High
Integration cost
Cost
Software cost
Cost module
Low
Low High
Number of Modules
Figure 9.22 | Number of modules versus cost of software development.
1. Abstraction: There are different levels of abstrac- 1. Logical cohesion: Logical cohesion exists when
tion available to represent the statement. In high- a module that performs tasks are related logically.
level abstraction, the statement is represented in 2. Temporal cohesion: Temporal cohesion exists
concrete manner, that means, internal structure when a module contains tasks that are related in
is not provided. In low-level abstraction, all state- such a way that all the tasks must be executed
ments are defined in a single format with all details within the same time limit.
of implementation. 3. Procedure cohesion: The procedural cohesion
2. Refinement: It is a top-down strategy to sim- exists when the processing elements of a module
plify the statement structure. Refinement is also are related and must be executed in a specific
called as elaboration. After refinement of the order.
statement, we can get the direct implementation 4. Communication cohesion: The communication
statement. cohesion exists when all the processing elements
3. Modularity: Software development process uses concentrate on one area of a data structure.
the modularity concept to reduce the develop-
ment cost and time. Suppose the complexity of the
problem is denoted by C(P) and the effort of the
9.7.2.3 Coupling
problem is denoted by E(P). The following formats
Coupling is a measure of relative independence among
represent the advantage of C.
modules, that is, it is a measure of interconnection
C(P1) E(P1) among modules. Following types of coupling exist:
C(P2) E(P2) 1. Data coupling: Data coupling exists when a
C(P1) > C(P2) module accesses another module through an argu-
C(P1) > E(P2) ment or through a variable.
C(P1 + P2) > C(P1) + C(P2) 2. Stamp coupling: Stamp coupling exists when a
E(P1 + P2) > E(P1) + E(P2) module accesses another module through a data
structure.
When the number of modules is less, the cost
3. Control coupling: Control coupling exists when
module for developing the module is high, and at
the control is passed between modules using a con-
the same time the integration cost is low.
trol variable.
When the number of modules increases, the cost 4. External coupling: External coupling exists
per module is low but integration cost is high. This when modules are connected to an environment
relationship is shown in Fig. 9.22. external to the software.
Modules should be designed in such a way that each 5. Common coupling: Common coupling exists
module has specific functional requirements. Functional when the modules share the same global variable.
independence is measured using two terms cohesion and 6. Content coupling: Content coupling exists when one
coupling. module makes use of data or control information main-
tained in another module. Also, content coupling occurs
9.7.2.2 Cohesion when branches are made into the middle of a module.
It is a measure of relative functional strength of a module. Note: Effective modular design must have high cohesion
Following types of cohesion exist: and low coupling.
Data Structure
Control Hierarchy
It shows the logical connection between the data objects.
It indicates the control sequence in the software project.
It represents the interaction between different modules
(Fig. 9.23). Software Procedure
It describes the customer requirements into programming
M1 language statements. Information hiding is used in effec-
tive modularity. It shows the independence of the module.
After the designing stage, we have the software design
M2 M3 document (SDD). The SDD represents the programming
description language oriented customer objective.
M4 M5 M6
9.7.3 Coding
M8 M9
M7 It translates the procedural-oriented description into
M10 machine-readable format by using appropriate program-
ming language.
Fan-in ⇒ number of control in Fan-in (M7) = 3
Fan-out ⇒ number of control out (M2) = 1 9.7.4 Testing
Fan-out (M7) = 0
The objective of testing to measure to what extent the
(M2) = 3 developer logical program becomes a failure. In the test-
Figure 9.23 | An illustration of control hierarchy. ing phase, different test cases are developed and imple-
mented to cover the structural and functional errors.
The control hierarchy defines various parameters of the Functional testing includes conducting tests to check the
software design: functionality of the product with all proper sets of input,
1. Depth: It is a measure of the number of levels which also covers errors. Structural testing includes con-
present in the software design. ducting tests to examine the internal structure of the
2. Width: It is a measure of the maximum number developed product to cover the error.
of modules present at any one of the level.
3. Fan-in: It is a measure of the number of modules
already controlling the given module.
9.7.4.1 Structural Testing
4. Fan-out: It is a measure of the number of modules
This testing is also called as white-box or glass-box test-
controlled by the given module.
ing. The objective of white-box testing is as follows:
5. Structural partitioning: It indicates the control
sequence of development, maintenance, and inte- 1. Guarantee the all the statements present in the
gration of the software. It is divided into the fol- developed program exercise at least once.
lowing two types: 2. Exercise logical expression towards true and false side.
•• Horizontal 3. Exercise all the loop boundaries within the opera-
•• Vertical tional limit and beyond the operational limit.
4. Exercise the data structure present in the program
Horizontal partitioning allows the level by level development towards validity.
and integration, that means, control flow is assigned to levels.
To reach the objectives of white-box testing, two differ-
Vertical portioning means the control sequence is for-
ent test operations are performed:
warded from root to leaf node, that is, depth-wise develop-
ment and integration take place. Because of dependencies, 1. Basis path testing
this approach is not suggested to software development. 2. Central structural testing
Basis Path Testing By using the cyclomatic complexity we can measure the
logical complexity of the program.
This test is used to cover all the paths present in the
development code. This testing guarantees that all the
statements in the program must be exercised at least Cyclomatic Complexity
once to identify the path in the program, convert the
program into graphical notation. On the basis of the It is defined as the quantitative measure of logical com-
control flow between the statements, the program is con- plexity of the program. Cyclomatic complexity can be
verted into graph notation called as flow graph or pro- calculated in three ways:
gram graph. Flow graph is a directed graph. 1. By using the number of edges and number of nodes
Each node in the flow graph represents the program we can measure the cyclomatic complexity as
statement. The sequence of the statement is represented V(G) = E − N + 2P
by using the following (Fig. 9.24):
E = Number of edges
1 2 N = Number of nodes
1. The if condition is represented by Fig. 9.25. 2. By using the region we can estimate the cyclomatic
complexity as
2 V(G) = Number of regions present in the graph
P1
1 2 3 R4
2,3 P2
Figure 9.27 | DO—WHILE statement. 6 P3 4.5
4. The switch case statement is represented by Fig. 9.28.
2 7 R1 8 R2 R3
1 3 6
9
4 10
5 11
Figure 9.28 | SWITCH-CASE statement. Flow graph depicting relation among different nodes.
= 11 − 9 + 2 = 4
1
Number of regions = 4 = R1, R2, R3, R4
So, cyclomatic complexity = 4
2
Cyclomatic complexity P + 1 = 3 + 1 = 4
R4
Path 1: 1-11 10 3
Path 2: 1-2-3-4-5-10-1-11
R5
12 11
Path 3: 1-2-3-6-8-9-10-1-11 4
13
Path 4: 1-2-3-6-7-9-10-1-11
5
R1
R2
Problem 9.11: Consider the following program and 6
calculate the cyclomatic complexity.
R3
8 7
1
9
10
7 1
8
9 11 2
Solution: 4
6
Rule 3: P + 1 = 4 + 1 = 5
Rule 2: Number of regions = 5 7 8 5
Rule 1 = E − n + 2 = 13 − 10 + 2 = 5 9
Path 1: 1-2-45-6-7-8-10 10
Path 2: 1-2-4-5-7-8-10
Path 3: 1-2-3-5-7-8-10 and more Flow chart depicting various operations of a program.
Cyclomatic complexity = N
umber of if statement
+1=3+1=4 1
The cyclomatic complexity of the program can be
calculated by using the graph matrices. a
P=4
3
Graph Matrices e
b
The flow graph is converted into graph matrices by
maintaining rows and columns. The number of rows and f
columns depends on the number of nodes in the flow 4 d
5
graph. Based on the communication link between the
nodes in the flow graph, the entry is reflected in corre- c
sponding position in the graph matrices. g
If any row consists of more than one entry that node 2
is called as predicate node, then take the weightage of
predicate node by subtracting one from the total. After
subtraction operation, add all the row values, it gives the Link
predicate nodes. Use the formula to calculate cyclomatic Mode 1 2 3 4 5
complexity V(G) = P + 1. 1 a
2
Example 9.1
3 d b
1 2 3 4 5 Connection 1 2 3 4 5
4 c f
1 b c 1 Matrix 1 0 1 0 1 1
g e h
5
2 a d 2 1 0 1 0 0
3 e f 3 0 1 0 1 0 A flow graph and graph matrix. Solution:
4 g 4 0 0 1 0 0 So, cyclomatic complexity = P + 1 = 5
5 h 5 1 0 0 0 0
Control Structure Testing
3−1=2 1 2 3 4 5 This testing is used to cover the error from the control
structure present in the program. There are three types
2−1=1 1 1 1 1 of expressions:
2−1=1 2 1 1 1. Arithmetic (= , − , *, /) = value
2. Relational (>, <, <=, >=, =, ≠) = True/False
1−1=0 3 1 1
(Boolean)
1−1=0 4 1 3. Boolean (&&, , ¬, /) = True/False (Boolean data type)
P = 04 5 1 Examples:
A graph matrix and its allocation. 1. a + b
2. (a + b) > (c + d) = T/F
3. ((a + b) > (c + d) && ((a × b) < ((c * d)) = T/F
After evaluating the control expression, the output may
Problem 9.14: Consider the following flow graph and be a Boolean data, that is, true/false.
create the graph matrix and calculate the cyclomatic In the control structure testing, examine all the logi-
complexity. cal expression towards true/false. During that evalua-
Note: If there are more than two outgoing nodes, tion, it covers the Boolean operator error, Boolean data
then the predicate formula will not work. or parenthesis symbol error, relational operator error
and arithmetic operator errors.
In this testing, two test cases are developed to cover 9.7.4.3 Comparison Testing
all the errors described in the above list.
1. Branch testing When the software is developed in the real-time applica-
2. Domain testing tion domain, there is a predefined value estimated based
on the product behaviour. After developing the soft-
ware, compare the behaviour of the new product with
9.7.4.2 Black-Box Testing
predefined values and remove the errors.
In this testing, various input test cases are developed
and the product functionality is examined, whether it 9.7.4.4 Life Cycle Testing
satisfies all possible inputs. This testing is also called
behavioural testing or grey-box testing. In this strategy, Various testing plans are implemented at each stage of
various testing techniques are used to cover the func- the software development life cycle.
tional errors such as
1. Equivalence partition
9.7.4.5 Validation Testing
2. Boundary value analysis
This testing is performed at requirement stage by con-
3. Comparison
ducting the black-box test plans on a prototype model.
1. Equivalence partition: In this testing, the input
domain is divided into equivalent partitions. Each 9.7.4.6 Integration Testing
position is called as class. One partition is the exact
valid range and another partition is above the This testing is performed at the design stage. Software
upper limit and another is below the lower limit. development uses the modularity concept and develops
the module independently. After developing the module,
Example 9.2 there is a need of integration to generate the final system.
In that regard, integration testing is required to check
If the input box consists of 1 to 1000 values, no need
the functionality. Integration testing is not focused on
of preparing the thousand (1000) test cases. By using
internal details of the modules.
equivalence partition, the above input box is divided
into three test cases. 1. Top-down integration: This testing focuses on
Test case is prepared between the ranges of {1—1000}
the function of the module integration either dep-
thwise or breadthwise. Depth-wise integration is a
Test case is prepared {>1000} vertical partitioning and widthwise integration is
Test case is prepared {<1} horizontal partitioning.
2. Bottom-up integration: In this testing, the
integration starts from leaf node and forwards to
2. Boundary value analysis: Most of the problems the root node. In this testing, the stubs and drivers
are possible at the extreme edges that are boundaries are maintained.
of input domain. Boundary value analysis focuses on
identified errors at the boundary rather than finding
9.7.4.7 Regression Testing
those existing at the centre of the input domain.
When a new module is added, there is a possibility of intro-
Example 9.3 ducing new functionality to the existing system. Due to the
new functionality, there is a possibility of errors occurring
Test case for input box accepting number (1–1000) in the existing system. To cover those errors, all test cases
using boundary value analysis is are re-conducted. This is called regression testing.
(a) Test cases with data exactly the input boundary of
input domain, that is, 1 and 1000. 9.7.4.8 Smoke Testing
(b) Test data with values at just below the extreme
edges of input domain, that is, 0 and 999. This testing is used in the wrap-shrink component devel-
(c) Test data with values just above the input boundary opment. Wrap-shrink component development means
of input domain, that is, 2 and 1000. within the short period of time the software is imple-
Note: When the product supports the N variable that mented. To perform this we use modularity and by
is tested using the boundary value analysis, that is, adding or deleting the existing module we can develop
4N + 1 test cases. the final product. In this way, smoke testing is used to
cover the error.
This testing is performed at the coding stage. After After developing the product, various system-level test-
developing the module, use the white-box test plan to ing operations are performed to check the reliability of
cover the structural errors in the module. the product.
IMPORTANT FORMULAS
SOLVED EXAMPLES
1. The most important feature of spiral model is Solution: Cyclomatic complexity is a software metric
developed by Thomas J. McCabe. It is used to indi-
(a) Requirement analysis
cate the complexity of a program. It directly measures
(b) Risk management
the number of linearly independent paths through a
(c) Quality management
program’s source code. It is also known as conditional
complexity. It is defined as V (G) = e − n + 2.
(d) Configuration management
Solution: Risk management helps in developing a Ans. (b)
cost effective project where risks are analyzed and 3. Testing of software with actual data and in actual
risk strategies are decided upon. environment is called
Ans. (b)
(a) Alpha testing (b) Beta testing
2. Cyclomatic complexity of a flow graph G with n
(c) Regression testing (d) None of the above
vertices and e edges is
Solution: Users or an independent test team at the
(a) V(G) = e + n − 2 (b) V(G) = e − n + 2
developer’s site performs alpha testing. Alpha testing
(c) V(G) = e + n + 2 (d) V(G) = e − n − 2
is often employed as a form of internal acceptance 8. Superficially the term “object-oriented”, means
testing, before the software goes to beta testing. that, we organise software as a
Beta testing is performed after alpha testing for (a) c ollection of continuous objects that incorpo-
user acceptance testing. rates both data structure and behaviour.
Regression testing focuses on finding defects (b) collection of discrete objects that incorporates
after a major code change has occurred. both discrete structure and behaviour.
(c) collection of discrete objects that incorporates
Ans. (b)
both data structure and behaviour.
4. Object Request Broker (ORB) is (d) collection of objects that incorporates both dis-
crete data structure and behaviour.
I. A software program that runs on the client as
well as on the application server. Solution: This is the definition of object-oriented
II. A software program that runs on the client side according to software engineering.
only. Ans. (c)
III. A software program that runs on the application 9. Which one of the following is not a key process
server, where most of the components reside. area in CMM level 5?
(a) I, II and III (b) I and II (a) Defect prevention
(c) II and III (d) I only (b) Process change management
(c) Software product engineering
Solution: ORB manages interaction between clients
(d) Technology change management
and servers.
Ans. (d) Solution: The key process areas at level 5 covers
the following issues–Defect Prevention, Technology
5. Key process areas of CMM level 4 are also classi- Change Management and Process Change
fied by a process which is Management. Software Product Engineering has
(a) CMM level 2 (b) CMM level 3 been addressed in level 3.
(c) CMM level 5 (d) All of the above Ans. (c)
Solution: There are five maturity levels in CMM 10. Which of the following system components is
model–(1) Initial, (2) Managed, (3) Defined, (4) responsible for ensuring that the system is working
Quantitatively Managed and (5) Optimizing. The to fulfil its objective?
organization at level 2 includes level 1. So, level 4 (a) Input (b) Output
is included in level 5. (c) Feedback (d) Control
Ans. (c)
Solution: The components of the control system
6. Validation means ensure smooth functioning of the system to achieve
its objective.
(a) Are we building the product right Ans. (d)
(b) Are we building the right product
(c) Certification of fields 11. Modifying the software to match changes in the
(d) None of the above ever-changing environment is called
12. Consider the following flow graph and calculate 14. Which of the following metric does not depend on
the cyclomatic complexity and also identify the the programming language used?
independent path.
(a) Line of code (b) Function count
(c) Member of token (d) All of the above
1
Solution: Line of code and tokens depends upon
the programming language used for developing
2 programs.
Ans. (b)
3 4
15. If in a software project the number of user input,
user output, enquiries, files and external interfaces
are (15, 50, 24, 12, 8), respectively, with complex-
5 6 7 8 ity average weighing factor. Find productivity if
effort = 70 person-month.
9 10 Solution:
15 × 4 = 60
50 × 5 = 50
11 24 × 4 = 96
12 × 10 = 120
8 × 7 = 56
Solution: Cyclomatic complexity = E − N + 2
= 12 − 11 + 2 = 3 Count value = 382
OR Productivity = Count value × (0.65 + 42 × 0.01)
Number of regions = 3 = 382 × (0.65 + 0.42)
So, cyclomatic complexity = 3 = 382 × (1.07)
Ans. (3)
= 408.74
13. Software measurement is useful to Ans. (408.74)
(a) indicate quality of the product 16. 58000 LOC gaming software is developed, with the
(b) track progress effort of 3 person-year. What is the productivity of
(c) form a baseline for estimation and prediction person-month?
(d) all of the above.
(a) 1.9 KLOC (b) 1.6 KLOC
Solution: Software measurement is useful to indicate (c) 4.8 KLOC (d) 4.2 KLOC
quality of the product, track progress, assess productiv-
ity and form a baseline for estimation and prediction. Solution: 58000/36 = 1611.1 = 1.6 KLOC
Ans. (d) Ans. (b)
Which of the test suites given below ensures cover- five years. Various parameters for the company are
age of statements S1, S2, S3 and S4? given in the table below.
(a) T1, T2, T3 (b) T2, T4 Parameter Language L1 Language L2
(c) T3, T4 (d) T1, T2, T4
(GATE 2010: 2 Marks) Man years LOC/10000 LOC/10000
needed for
Solution: development
T1 covers S1
Development cost $$1000000 $$750000
T2 covers S3
per man year
and T4 covers S2 and S4.
So, the test suit is T1,T2 and T4 Maintenance 5 years 5 years
Ans. (d) time
Cost of $$1000000 $$50000
7. Which one of the following is NOT desired in a
maintenance per
good SRS document?
year
(a) Functional requirements
(b) Non-functional requirements Total cost of the project includes cost of develop-
(c) Goals of implementation ment and maintenance. What is the LOC for L1
(d) Algorithms for software implementation for which the cost of the project using L1 is equal
(GATE 2011: 1 Mark) to the project using L2?
Solution: SRS document is prepared at the starting (a) 4000 (b) 5000 (c) 4333 (d) 4667
phase of software development cycle and only client (GATE 2011: 1 Mark)
software requirement is noted down. Algorithm
for software implementation is designed in later Solution: Number of lines of code for L1 = l
stages after the requirement phase. Number of lines of code for L2 =2l
Ans. (d) Total cost for L1 = Total cost for L2
l
8. A company needs to develop digital signal pro- × 1000000 + 5 × 100000
10000
cessing software for one of its newest inven-
2l
tions. The software is expected to have 40000 = × 750000 + 5 × 50000
lines of code. The company needs to determine 10000
the effort in person-months needed to develop Solving, we get l = 5000
this software using the basic COCOMO model. Ans. (b)
The multiplicative factor for this model is given 10. The following is the comment written for a C
as 2.8 for the software development on embed- function:
ded systems, while the exponentiation factor is
given as 1.20. What is the estimated effort in /*This function computes the roots of a
person-months? quadratic equation a.x^2 + b.x + c = 0.
The function stores two real roots in
(a) 234.25 (b) 932.50 *root1 and *root2, and returns the
(c) 287.80 (d) 122.40 status of validity of roots. It handles
(GATE 2011: 1 Mark) four different kinds of cases.
(i) When coefficient a is zero irre-
Solution: Estimated effort in person-months =
spective of discriminant
a1(KLOC)a2
(ii) When discriminant is positive
Given that a1 = 2.8 and a2 = 1.20
(iii) When discriminant is zero
So, KLOC = 40
(iv) When discriminant is negative.
2.8 (40)1.20 = 234.25 person-months
Only in cases (ii) and (iii), the stored
Ans. (a)
roots are valid. Otherwise 0 is stored
9. A company needs to develop a strategy for software in the roots. The function returns 0 when
product development for which it has a choice the roots are valid and –1 otherwise.
of two programming languages L1 and L2. The The function also ensures root1 > = root2.
number of lines of code (LOC) developed using L2 int get_QuadRoots (float a, float b, float
is estimated to be twice the LOC developed with c, float *root1, float *root2);
L1. The product will have to be maintained for */
Which one of the following options provide the set of Common Data Questions 12 and 13: The proce-
non-redundant tests using equivalence class partitioning dure given below is required to find and replace
approach from input perspective for black box testing? certain characters inside an input character
string supplied in array A. The characters to be
(a) T1, T2, T3, T6
replaced are supplied in array oldc, while their
(b) T1, T3, T4, T5
respective replacement characters are supplied in
(c) T2, T4, T5, T6
array newc. Array A has a fixed length of five
(b) T2, T3, T4, T5
characters, while arrays oldc and newc contain
(GATE 2012: 2 Marks) three characters each. However, the procedure is
Solution flawed.
T1 and T2 checks the same condition of a = 0
void find_and_replace (char *A, char *oldc, char *newc){
T3 and T4, both have D = 0 for (int i=0; i<5; i++)
These should not be in the same set. for (int j=0; j<3; j++)
Ans. (c) if (A[i] == oldc[j]) A[i] = newc[j];
}
11. The following figure represents access graphs of
two modules M1 and M2. The filled circles rep- The procedure is tested with the following four test
resent methods and the unfilled circles represent cases.
attributes. If method m is moved to module M2
(1) oldc = “abc”, newc = “dab”
keeping the attributes where they are, what can
(2) oldc = “cde”, newc = “bcd”
we say about the average cohesion and coupling
(3) oldc = “bca”, newc = “cda”
between modules in the system of two modules?
(4) oldc = “abc”, newc = “bac”
Module M1 Module M2 (GATE 2013: 2 Marks)
Solution Solution
The test cases 3 and 4 are the only cases that Test case oldc = “abc”, newc = “bac” will
capture the flaw. The code does not work prop- detect the fault.
erly when an old character is replaced by a new Ans. (c)
character, and the new character is again replaced 14. In the context of modular software design, which
by another new character. This does not happen one of the following combinations is desirable?
in test cases (1) and (2), it happens only in cases
(3) and (4). (a) High cohesion and high coupling
(b) High cohesion and low coupling
Ans. (b)
(c) Low cohesion and high coupling
(d) Low cohesion and low coupling
13. If array A is made to hold the string “abcde”,
which of the above four test cases will be successful Solution: Cohesion measures the internal depen-
in exposing the flaw in this procedure? dency within module, whereas coupling presents
intermodule dependence. So, there should be high
(a) None (b) 2 only cohesion and low coupling.
(c) 3 and 4 only (d) 4 only
Ans. (b)
PRACTICE EXERCISES
Set 1
1. SRS is also known as specification of 5. All activities lying on critical path have slack time
equal to
(a) White box testing (b) Stress testing
(c) Integrated testing (d) Black-box testing (a) 0 (b) 1
(c) 2 (d) None of above
2. SRD stands for
6. A software agent is defined as
(a) Software requirements definition
(b) Structured requirements definition I. Software developed for accomplishing a given task.
(c) Software requirements diagram II. A computer program which is capable of acting
(d) Structured requirements diagram on behalf of the user in order to accomplish a
given computational task.
3. The symbol represents III. An open source software for accomplishing a
given task.
(a) mandatory 1 cardinality (a) I (b) II
(b) mandatory many cardinality (c) III (d) All of the above
(c) optional 0 or 1 cardinality
(d) optional zero-many cardinality 7. In terms of a system, finished products and infor-
mation are examples of:
4. Software testing is
(a) Input (b) Output
(a) The process of establishing that errors are not (c) Feedback (d) Control
present.
8. For a well-understood data processing applica-
(b) The process of establishing confidence that a
tion, it is best to use
program does what it is supposed to do.
(c) The process of executing a program to show (a) the waterfall model
that it is working as per specifications. (b) the prototyping model
(d) The process of executing a program with the (c) the evolutionary model
intent of finding errors. (d) the spiral model
15. The model in which the requirements are imple- 25. Coupling and cohesion can be represented using a
mented by category is
(a) Cause-effect graph (b) Dependence matrix
(a) Evolutionary development model (c) Structure chart (d) SRS
(b) Waterfall model
(c) Prototyping 26. KPA in CMM stands for
(d) Iterative enhancement model (a) Key Process Area
16. Which one of the following is a fault base testing (b) Key Product Area
technique? (c) Key Principal Area
(d) Key Performance Area
(a) Unit testing (b) Beta testing
(c) Stress testing (d) Mutation testing 27. In the spiral model, `risk analysis’ is performed
17. Which of the following statements is true? (a) In the first loop
(a) Abstract data types are the same as classes. (b) In the first and second loop
(b) Abstract data types do not allow inheritance. (c) In every loop
(c) Classes cannot inherit from the same base class. (d) Before using spiral model
(d) Object have state and behaviour.
28. Each time a defect gets detected and fixed, the
18. If every requirement can be checked by a cost- reliability of a software product
effective process, then the SRS is
(a) Increases (b) Decreases
(a) Verifiable (b) Traceable (c) Remains constant (d) Cannot say anything
(c) Modifiable (d) Complete
35. Which one of the following is not a definition of error? 38. Software Configuration Management is the disci-
pline for systematically controlling
(a) It refers to the discrepancy between a com-
puted, observed or measured value and the (a) T
he changes due to the evolution of work prod-
true, specified or theoretically correct value. ucts as the project proceeds.
(b) It refers to the actual output of software and (b) The changes due to defects (bugs) being found
the correct output. and then fixed.
(c) It refers to a condition that causes a system to fail. (c) The changes due to requirement changes
(d) It refers to human actions that result in soft- (d) All of the above
ware containing a defect or fault. 39. Which one of the following is not a step of require-
36. Match the following: ment engineering?
(a) Requirement elicitation
List − I List − II (b) Requirement analysis
a. Data coupling i. M
odule A and Module (c) Requirement design
B have shared data (d) Requirement documentation
b. Stamp coupling ii. D
ependency between 40. __________ is a process model that removes defects
modules is based before they can precipitate serious hazards.
on the fact they (a) Incremental model
communicate by only (b) Spiral model
passing of data (c) Clean room software engineering
(Continued) (d) Agile model
(a) Helping us understand the nature of the soft- 9. Black-box testing is also called
ware product (a) Specification-based testing
(b) Convincing the customer that we know what (b) Structural testing
we are doing (c) Verification
(c) Filling texts on software engineering (d) Unit testing
(d) Managing the various activities necessary to (e) Stress testing
get the job done
10. Project risk factor is considered in?
7. A process view in software engineering would con-
(a) Spiral model
sider which of the following
(b) Waterfall model
(a) Product performance (c) Prototyping model
(b) Staffing (d) Iterative enhancement model
Set 1
1. (d) 10. (a) 19. (c) 28. (a) 37. (b) 46. (d)
2. (b) 11. (d) 20. (a) 29. (b) 38. (d) 47. (d)
3. (d) 12. (a) 21. (b) 30. (a) 39. (c) 48. (b)
4. (d) 13. (a) 22. (d) 31. (d) 40. (c) 49. (d)
5. (a) 14. (b) 23. (d) 32. (c) 41. (b)
6. (b) 15. (a) 24. (d) 33. (c) 42. (b)
7. (b) 16. (d) 25. (b) 34. (d) 43. (b)
8. (a) 17. (b) 26. (a) 35. (c) 44. (b)
9. (b) 18. (a) 27. (c) 36. (b) 45. (d)
5. (d) The aim of software engineering is to produce 8. (a) Product quantity would not include during the
software that is fault-free, delivered on time, deliv- management of a software project.
ered within budget and satisfies users’ needs.
9. (a) Black-box testing is another name for specifica-
6. (d) A systematic approach to software devel- tion-based testing.
opment, as epitomised by the various life-cycle
10. (a) Aim of risk analysis phase in the spiral model
models, is useful in managing the various activities
is to eliminate the high-risk problems before they
necessary to get the job done.
threaten the project operation or cost.
7. (b) Staffing is the apt choice among the given
7
Number of questions
6
5
4 Marks 1
Marks 2
3
Total number of questions
2
1
0
2015 2014 2013 2012 2011 2010
Year Concept
2015 TCP, IP Header, File organization, Symmetric key cryptography, LAN, Stop and
wait protocol, CSMA/CD, Go-back-N sliding window protocol, IPv4 Addressing,
Data transmission, HTTP, UDP datagram, Routing table
2014 IPv4 addresses, MTU, OSI layer, Frames, IP header, Classless inter-domain routing
2013 Transport layer protocols, Data link layer concepts, IPv4 concepts
2012 IPv4 concepts, Transport layer concepts, Application layer concepts, IP address,
Propagation delay in networks
2011 Firewall concept, Distance vector, Application layer concepts, HTML
2010 IP address concepts, Application layer, Router concepts
COMPUTER NETWORKS
Syllabus: Computer networks: ISO/OSI stack, LAN technologies (Ethernet, token ring), Flow-and error-control
techniques, Routing algorithms, Congestion control, TCP/UDP and sockets, IP(v4), Application-layer protocols
(ICMP, DNS, SMTP, POP, FTP, HTTP); Basic concepts of hubs, switches, gateways and routers; Network security:
basic concepts of public key and private key cryptography, digital signature, firewalls.
Computer network is a collection of autonomous devices Two or more devices connecting to each other through
interconnected via a medium. The medium may be a any medium forms a network. To connect the devices
guided medium, a wireless medium or a satellite commu- there are two possible ways:
nication. To understand data communication, different
layered architectures have been presented. All layers exe- 1. Point-to-point connection: This provides a
cute different protocols to communicate the data success- dedicated link between the two devices.
fully from one end to the other. The intermediary devices 2. Multipoint connection: This is also known as
such as switch, hub, router or gateway help in advancing multi-drop connection. In this case, channel capac-
this communication. Although the security requirements ity is shared by more than two devices. In general,
may be different depending upon the application, network this is used in practice. All the five examples in
security cannot be left aside when studying networks. Fig. 10.1 are multipoint.
In a bus network, all the devices are connected with one 10.2.1.5 Tree Topology
cable. The major benefit here is, we require less cable
length than star topology and expansion is quite easy A tree topology maintains the devices connected to a
with the help of repeaters. central hub as well as to some secondary hubs, which are
The issues associated with bus topology are as follows: again connected to the central hub. It allows an isolated
network which prioritizes communication from different
1. Only one device can send the data at one time. All
computers. It also faces the same problem as in a star
the devices will listen at that time.
topology; failure of central hub will crash the entire net-
2. The data communication is only in one direction.
work. Also, it has high cabling cost.
3. In a bus topology, if the network shuts down then
there is problem in identifying the culprit device.
10.3 LAN TECHNOLOGIES
10.2.1.2 Ring Topology
In a ring topology, each deviceis connected exactly to two LAN (local area network) is an integral part to create
devices to form the ring. Repeaters are used to regener- a network. There are many LAN technologies such as
ate and retransmit each bit. Data travels around the net- Ethernet, token ring, token bus, FDDI (fiber distributed
work in one direction. Data travels in the form of token. data interface) and ATM LAN. Table 10.1 shows the
Additional components do not affect the performance of comparison of Ethernet, token bus and token ring.
the network. Even if the load on the network increases,
the performance of a ring topology is better than a bus
10.3.1 Ethernet
topology. The problems may be listed as follows:
1. Failure of one computer in the ring may lead to Xerox Corporation, Digital Equipment Corporation and
entire communication loss. Intel Corporation developed Ethernet LAN technology
2. Network scaling is difficult. in 1976. Ethernet is based on the IEEE 802.3 specifica-
tion. It is a linear-bus logical topology. Ethernet is the
For example, token ring is defined by IEEE 802.5
most widely used LAN technology in the world. It has
standard.
passed four generations: Standard Ethernet (10 t Mbps),
Fast Ethernet (100 Mbps), Gigabit Ethernet (1 Gbps)
10.2.1.3 Star Topology and Ten-Gigabit Ethernet (10 Gbps). Earlier Ethernet
designed were of two types: Thicknet (thick coaxial
In a star topology, a hub is placed at the central location main trunk cable of 10 mm and Thinnet (thin coaxial
and all the devices are connected to the hub. All com- cable: RG-58 of 5 mm). In 1990, unshielded twisted-pair
munication is possible through the hub only. If the hub (10Base-T) Ethernet came into existence. Ethernet uses
10.4 ISO/OSI STACK The following are some issues listed which may create
problems before/during transmission:
1. Compatibility of mechanical and electrical interfaces
There are two reference models based on the network 2. Working of physical transmission media
architectures. One is the International Standards 3. Deciding on the number of bits per second to be sent
Organization/Open Systems Interconnection (Reference 4. Finding out whether transmission is simplex or duplex
Model 1984) (ISO/OSI) model and other is the 5. Establishing and terminating the initial communi-
Transmission Control Protocol/Internet Protocol (TCP/ cation when both sender and receiver are finished
IP) model. The layered architecture of both the models
Message size (bits)
has been compared in Fig. 10.2. Transmission time =
Bandwidth (bits/s)
OSI Model TCP/IP Model Distance
Propagation time =
Velocity
Application
Problem 10.1: Message size = 1 Kb
Presentation Application
Bandwidth = 1 Mbps
103 × 23 bits
= 8 × 10−3 s
1 Kb
Session Transmission time = =
1 Mbps 106 bits/s
Transport Tranport If link utilisation is 50%, what is the relation between
transmission time and propagation time?
Network Internet Solution:
Link utilization of sender
Data link
Transmission time
Network interface =
Physical Transmission time + 2× Propagation time
1 Transmission time
=
Figure 10.2 | OSI and TCP/IP model. 2 Transmission time + 2 × Propagation time
The ISO/OSI model has seven layers while TCP/IP ransmission time + 2 × Propagation time
T
has only four layers. Session and presentation layers are = 2 × Transmission time
completely missing. Characteristics of the session layer Therefore, Transmission time = 2 × Propagation time
are provided by the transport layer, and the application Transmission time = 2× Propagation time
layer bears the accountability of the presentation layer.
or Transmission time = Round-trip time
Host to network is the lowest layer in the TCP/IP model
and its duty is to send IP packets to the network. In the Let L be the message, B the bandwidth and R the
ISO/OSI model, the network layer provides connection- round-trip time (RTT),
oriented as well as connectionless services, but the TCP/ LB
Link utilization of sender =
IP model provides only connectionless services. (L B) + R
If M is a message and H is the header that is added at L
every layer and N layers are present in hierarchy, then h=
L + BR
the fraction of header that is passed in the total content
is calculated as follows: In the above expression,
M 1. if L = BR, h = 50%
Fraction of data =
NH + M 2. if L > BR, h > 50%
3. if L < BR, h < 50%
10.4.1 ISO/OSI Model 4. if L >>> BR, h ∼− 100%
5. if L <<< BR, h ∼− 0%
10.4.1.1 Layer 1: Physical Layer
10.4.1.2 Layer 2: Data Link Layer
The major responsibilities of physical layer are transmission
of raw bit stream and to form the physical interface between Data link layer provides reliable transfer of informa-
two communicating devices. The conversion of analog to tion between two adjacent nodes. Also, it provides
digital and digital to analog is performed at this layer. frame-level error control and flow control. It provides
communication between machines on the same network. frame, the frame is resent, the copy is held and the timer
Communication between two devices can be simplex, is restarted. For the corrupted and lost ACK frame,
half-duplex, or full-duplex. In simplex, the communica- sequence numbers can be used. In the data frame, a field
tion is unidirectional. In half-duplex, each device can is added for the sequence number. The sequence num-
both transmit and receive, but not at the same time. bers are based on modulo-2 arithmetic. This protocol
In full-duplex, both devices can transmit and receive is also having acknowledgement numbers, which speci-
simultaneously. fies the sequence number of the next frame expected by
This layer is responsible for encoding and decoding i.e. the receiver. A data frame uses a sequence number and
converting bits to signals at sender site and recovering an ACK frame uses an acknowledgement number. The
bits from received signals at receiver side; frame cre- control variable of sender keeps the sequence number for
ation i.e. deciding a minimum unit for sending bits; error the next frame to be sent (0 or 1). The control variable
detection and/or correction of frames through parity or of receiver keeps the number of the next frame expected.
CRC and flow control using ARQ, sliding WINDOW When a frame is sent, the value of the control variable
etc. The functionality of data link layer is as follows: of sender is incremented. When a frame is received, the
value of the control variable of receiveris incremented.
1. Encoding: Signals propagate over a physical The stop-and-wait ARQ protocol is very inefficient if
medium — (1) modulate electromagnetic waves (var the channel is thick (large bandwidth) and long (long
ying voltage); (2) encode binary data onto signals round-trip delay). Other drawback of this protocol is
(e.g., 0 as low signal or non-return to zero, NRZ, that it does not support pipelining, as it does not sup-
and 1 as high signal or non-return to zero inverted, port multiple frames.Pipelining helps in improving the
NRZI); make a transition from current signal to efficiency of the transmission, if the number of bits in
encode a 1 or stay at the current signal to encode transition is large with respect to the bandwidth-delay
a 0;(3) Manchester (transmit XOR of the NRZ- product.
encoded data and the clock only 50% efficient). For stop-and-wait ARQ,
In Manchester encoding, a clock signal and data
signal are mixed together by XORing operation. The Message size
Transmission time =
clock makes a clock transition in every bit time, so Bandwidth
it runs at twice the bit rate. When it is XORed with
0 then it makes low-to-high transition, it acts as a Distance of the link
Propagation delay =
clock and when it is XORed with 1 then it makes Velocity
high-to-low transition, it acts as a data signal.
2. Framing: The basic data unit at the date link Link utilisation of sender or throughput is given by
layer is called a `frame’ which is a collection of bits
Transmission time
in sequence boundary. In order to mark boundaries h=
of frame starting and ending characters are used. Transmission time + 2× Propagation delay
3. Flow control: It is a mechanism which informs
the sender about the amount of data transmission • G
o-back-N automatic repeat request: This protocol
before receiving an acknowledgement from the helps in improving the efficiency of transmission by
receiver. As the receiving device has limited speed filling the pipe. It supports multiple frames during
for processing the incoming data and limited wait for acknowledgement. The sequence numbers
memory to store data, so it informs the sending are modulo 2m, where m is the size of the sequence
device by sending few frames and stop. The receiv- number field in bits. Sliding window defines the
ing device has a buffer, a block of memory, for stor- range of sequence numbers related to the sender
ing extra incoming data before processing. and receiver. When the timer expires, the sender
4. Error control: It is a mechanism which informs resends all outstanding frames. Stop-and-wait
the sender about the retransmission of the damaged ARQ is a special case of Go-Back-N ARQ in which
and lost frames during transmission. It is a method the size of the send window is 1. It supports one
of error detection and error correction. Automatic receiver window size. This protocol is very inef-
repeat request (ARQ) is a process in which when- ficient for a noisy link. In noisy link, frames are
ever an error is detected, the receiving device sends resending again and again, which uses bandwidth
the request for retransmission to sender. and slow down the transmission. For Go-Back-N
5. Techniques of flow and error control: ARQ,
•• Stop-and-wait automatic repeat request: In this
protocol, the sender starts the timer and keeps the Sender window size < 2m
copy of the sent frame. If the timer expires and Maximum sequence number = 2m − 1
there is no acknowledgement (ACK) for the sent where m is the number of segment bits.
If maximum sequence number is s, then the number •• Subtract the remainder from S(x) using
of sequence bits = log (s+1) modulo-2 subtraction.
Message size The result is the checksummed message.
Transmission time = 9. Decoding a CRC procedure:
Bandwidth
•• Let n be the length of the checksummed mes-
Distance
Propagation delay = sage in bits.
Velocity •• Divide the checksummed message by the code
Link utilisation of sender or throughput is given by polynomial using modulo-2 division. If the
Transmission time
h= remainder is zero, there is no error detected.
Transmission time + 2× Propagation delay 10. Choosing a CRC polynomial: The longer the
Total bits polynomial, the smaller the probability of unde-
Number of frames (Window size) = tected error.
Frame size
•• Selective repeat automatic repeat request: In
this protocol, the damaged frame is resent in Problem 10.3: If the frame is 1101011011 and generator
the network. It is efficient for noisy links, but it is x4 + x + 1, what would be the transmitted frame?
requires complex processing at the receiver end. Solution: The polynomial x4 + x + 1 corresponds to
Sender window size divisor 10011(k = 5 bits)
= Receiver window size ≤ 2m − 1 Data word (1101011011) of N = 10 bits is augmented
If Q is the size of the window, then the number with (k − 1) zero’s.
of sequence bits = log2Q + 1
Dividend = 11010110110000
Total bits 110000101
Number of frames (Window size) =
Frame size
10011 11010110110000
Problem 10.2: Calculate the link utilisation for stop-and- 10011
wait flow control mechanism if the frame size is 4800 010011
bits, bit rate is 9600 bps and distance between devices 10011
is 2000 km. Given propagation speed is 200000 km/s. 0000010110
10011
Solution:
0010100
4800 9600
h= = 0.96 10011
(4800 9600) + 2 × (2000 200000) 001110
After dividing the message 1101011011 by 10011 the
6. Parity bits: Append a single parity bit to a remainder is 1110, which is CRC. The transmitted
sequence of bits data is data + CRC, which is 1101011011 + 1110 =
•• If using `odd’ parity, the parity bit is calculated 11010110111110.
as making the total number of 1’s in the bit
sequence odd; Data link layer is divided into two sublayers:
•• If using `even’ parity, the parity bit makes the 1. Multiple access control sublayer: It provides
total number of 1’s in the bit sequence even controlled access to shared transmission media.
For example, if −Q is for even parity, what’s the 2. Logical link control sublayer: It is responsible
parity bit for 00010101? The problem with parity for error and flow control.
bit is that it only detects when there are an odd
number of bit errors. When multiple users share a common communication
7. Polynomial codes: It can detect errors on large link, multiple access protocols are used to coordinate
chunks of data; has low overhead; is more robust access to common link (Fig. 10.3).
than parity bit; and requires the use of a code 1. Random Access Protocols: The following
polynomial. are the different random access protocols used for
8. Cyclic Redundancy Check (CRC): Example accessing the shared transmission channel:
of a polynomial code •• Pure ALOHA: It says that whenever a station
Procedure: is having the data, they can send immediately.
•• Let r be the degree of the code polynomial. The time at which the collision occurs is called
Append r zero bits to the end of the transmitted vulnerable time.
bit string. Call the entire bit string S(x). Tp = Maximum propagation time
•• Divide S(x) by the code polynomial using Distance between two stations
modulo-2 division. =
Veelocity
Multiple access
protocols
Suppose Tfr is the average transmission time for a 10.4.1.3 Layer 3: Network Layer
frame, then
Frame size The network layer is responsible for host-to-host delivery
Tfr = =G and path selection between endsystems (routing). The
Bandwidth
fragmentation, reassembly and translation between dif-
Throughput (S) = G × e−2G ferent network types are also performed at this layer. In
Vulnerable time = 2 × Tfr other words, communication between nodes is possible in
Smax = 18.4% different networks through this layer.
•• Slotted ALOHA: It says that if stations are Packet delivery can be accomplished by using either
ready with the data, they have to wait for the a connection-oriented or a connectionless network ser-
required time slot and can transmit data exactly vice. In a connection-oriented protocol, the connection
at that timeslot. is established before sending the packets so route is
established before and all the packets have to follow that
Throughput (S) = G × e−G route. Example: Frame relay and ATM uses this service.
Vulnerable time = Tfr In connectionless protocols, the network layer protocol
Smax = 36.8% treats each packet independently. The packets in a mes-
•• CSMA (Carrier Sense Multiple Access): If a sage may or may not follow the same path to their des-
station is ready with the data, it senses the tination. For example, Internet uses this type of service.
channel, and if channel found idle, data is trans- Switching can be broadly divided into three catego-
mitted, otherwise the station has to wait for ries: circuit switching, packet switching and message
random amount of time. switching, as shown in Table 10.2.
The Internet is a global system of computer net- Table 10.3 | Comparison between TCP and UDP
works that are interconnected worldwide and all use the
standard Internet protocol suite (TCP/IP) for linking. Attribute TCP UDP
1. Internet as a Datagram Network: The Connection Connection Connectionless
Internet, at the network layer, is a packet-switched Management oriented
network. Switching can be generally divided into End-to-End Dedicated No dedicated
three broad categories: circuit switching, packet Connection connection connection
switching, and message switching. Packet switch-
ing uses either the virtual circuit approach or the Reliability Reliable Unreliable
datagram approach. Transmission Byte oriented Message oriented
The Internet chooses the datagram approach Acknowledgement Yes No
to switching in the network layer, and uses
Retransmission Automatically If needed
the universal addresses defined in the network
layer to route packets from the source to the Congestion Yes No
destination. Switching at the network layer in the Control
Internet uses the datagram approach to packet Flow Control Yes No
switching.
Fault Tolerance No No
2. Why Internet Uses Connectionless Network?
Delivery of a packet can be accomplished by using Data Delivery Strictly Unordered
either a connection-oriented through TCP or a ordered
connectionless network service through UDP (see Security Yes Yes
Table 10.3). In a connection-oriented service, the Overhead Low Very low
source has to make a connection with the desti-
nation before sending a data packet. Only after Transmission High Very high
establishing connection, a sequence of packets Speed
from source to the destination can be sent on the Data Quantity Small to very Small to moderate
same path that is established before in a sequential Suitability large amount amount of data
order. The connection is terminated only when all of data
the packets of a particular message have been suc-
cessfully. But the communication at the network
layer in the Internet is connectionless. The reason 10.5 ROUTING ALGORITHMS
is that Internet is made of so many heterogeneous
networks that it is almost impossible to create a
connection between every source and destination When the router receives a packet, which route this packet
pair without knowing the nature of the networks should follow to reach to destination is an important con-
in advance. cern. This is one of the major responsibilities of network
layer. This decision is taken by router on the basis of path between a pair of routers, the shortest path
the routing table maintained by it. The algorithm which is chosen. The shortest path is chosen based on the
decides the suitable route is known as routing algorithm. number of hops or geographical distance in kilome-
The desirable properties of any routing algorithm are tres. Dijkstra’s and Bellman—Ford’s algorithm are
correctness, fairness, stability, robustness and optimality. the most famous shortest path algorithms.
These algorithms can be broadly divided into two
Example 10.1
categories:
Flooding: This is again a non-adaptive algorithm and it
1. Adaptive algorithms: The dynamic rout-
sends a copy of the packet to every outgoing line except
ing decision depends on the topology and traf-
the line on which it was received. This guarantees the
fic. Furthermore, adaptive algorithms have been
packet delivery to destination but a large number of
divided into three forms:
•• Centralised: The decision is taken on the basis
packet copies will be generated. Sometimes this count
approaches to infinity and the only solution is to discard
of global information and this is performed by a
the packet using any of the following approach:
centralised node.
•• Isolated: The routing decision is taken based (a) Using a hop counter to avoid forwarding of packets as
on local information. Generally, routers do not number of hops reaches the diameter of the network.
share information with their neighbours. (b) Keeping track of flooded packets.
•• Distributed: A combination of local and global (c) Selective forwarding by forwarding only those
information. relevant packets which approaches to the right
2. Non-adaptive algorithms: The static routing destination.
decision is taken in advance and it is downloaded
by the routers, that is, never change once initial To avoid looping, a sequence number may be added
route has been selected. to each packet’s header. This helps in discarding those
packets whose sequence number is lower than the one
The properties of non-adaptive routing algorithms are already received.
as follows:
Note: Although flooding is inefficient in most applica-
1. Optimality principal: It states that “if router J tions, an exception may be in the case of military appli-
is on the optimal path from router I to router K, cation where large number of routers are placed and
then the optimal path from J to K also falls along robustness is highly desirable.
the same route”. In other words, suppose r1 is the
route from I to J and r2 is rest of the route. Then, if
2. Flow-based routing: This is a non-adaptive
any route is better than r2, it could have improved
algorithm which uses topology and traffic condi-
the overall optimal route. Hence, r1r2 is optimal.
tion for deciding the route. If traffic on some route
2. Sink tree: A set of all optimal routes from any
is more than average, then the route should be
source to a fixed destination form a tree called sink
avoided to achieve optimal path.
tree. Sink trees may be more than one with the
If line capacity and flow is given, delay can be
same path length. The concern of sink tree here is
determined easily using the following formula:
to help routers find the best path.
1
T =
In addition to adaptive and non-adaptive categorisa- mC − l
where 1/m is mean packet size in bits, l is the mean
tion, routing algorithm can be simply categorised into
the following:
number of packets arrived per second and C is the
1. Static routing (shortest path, flooding) line capacity in Kbps.
2. Flow-based routing
3. Dynamic routing (distance vector, link state routing) Example 10.2
4. Hierarchical routing
Distance Vector/Distributed Bellman—Ford/
5. Routing for mobile hosts
Ford—Fulk‑erson Routing Algorithm: This is one
6. Broadcast routing
of the popular examples of dynamic routing algorithm.
7. Multicast routing
Each router maintains a table (called vector) which
These are explained as follows: helps in finding the best-known distance to any destina-
1. Shortest path routing: This non-adaptive tion. It also indicates preferred outgoing line to be used
approach is based on the simplest and most widely to reach the destination. The performance metric can be
used principle. Each node is treated as a router the number of hops, time delay or number of packets in
and each arc as communication link. To find a the queue.
B ∞ ∞ ∞
From
Node C table
C ∞ ∞ ∞
Cost to
Node B table A B C
Cost to A 0 2 7
A B C From B 3 0 1
A ∞ ∞ ∞ C 3 1 0
From B 2 0 1
∞ ∞ ∞
Step 3: In similar fashion, algorithm proceeds until all
C nodes have updated tables.
Cost to Cost to
A B C A B C
A ∞ ∞ ∞ A 0 2 3
From B ∞ ∞ ∞ From B 2 0 1
C 7 1 0 C 3 1 0
IP Header Data
20−60 bytes
65535 bytes
Options
(if any, variable length, padded with 0’s, 40 bytes maximum length)
always set to `0’. The next bit is DF (do not fragment). 13. Options: These options may contain values for
If DF = 0, fragmentation can be done if required and various options such as strict source routing, secu-
buffered at the router of the receiver until all frag- rity, record route, time stamp, etc.
ment comes, and if DF = 1, fragmentation should
not be done. The third bit is MF (more fragment). If Problem 10.4: If the total length bits are 0000000000
MF = 1, then datagram is not the last fragment. If 111111 and header length is 1001, calculate the size of
MF = 0, then datagram is the last fragment. the packet, header and payload.
7. Fragment offset: This offset tells the exact
position of the fragment in the original IP packet. Solution:
8. Time to live: It controls the maximum number of (a) Packet size = Decimal equivalent of 00000000
routers visited by the datagram. To avoid looping 00111111 = 63 bytes
in the network, every datagram is sent with some- (b) Header size (1001) = 9 × 4 = 36 bytes
time-to-live(TTL) value set. Each router receiv- (c) Packet size = Header + Payload
ing the datagram decreases TTL by 1 and when it
becomes 0, the datagram is discarded. Payload = Packet size − Header = 63 − 36
9. Protocol: It tells the higher-level protocol which = 27 bytes
uses the service of the IP layer. For example, pro-
tocol number for ICMP is 1, IGMP is 2, TCP is 6
and UDP is 17. Problem 10.5: Suppose a router receives an IP packet
10. Header checksum: This field stores checksum value containing 600 data bytes and has to follow a packet
of the entire header excluding data which is used to maximum transferable unit of 200 bytes. Assume
check if the packet has been received error-free. that the IP header is 20 bytes long; specify the
11. Source address: 32-Bit address of the sender of relevant values in each fragment header.
the packet. Solution: IP packet = 600 bytes
12. Destination address: 32-Bit address of the MTU = 200 bytes
receiver of the packet.
ting the rightmost 32 −n bits to Os in the binary Number of hosts in each subnet = 212 − 2
notation.
2. The last address in the block can be found by set- = 4094 subnets
ting the 32 −n rightmost bits in the binary nota-
tion of the address to Is.
3. The number of addresses in the block is the dif- Problem 10.9: If IP address of a system is
ference between the first and the last address,that 199.11.171.189 and subnet mask is 255.255.255.224,
is, 232−n. calculate the subnet id.
Solution:
Problem 10.6: Determine the subnet identifier, broad- IP address 199.11.171.189
cast address and number of valid host addresses Subnet mask 255.255.255.224
having a host with IP address of 196.142.4.29/24.
Subnet id 199.11.171.160
Solution:
160 is equivalent to 101 00000
Subnet identifier: 196.142.4.0
Subnet Host
Broadcast address: 196.142.4.255
Bits Bits
Number of subnets = 2 − 2 = 6
Number of valid host addresses: 254 3
Number of hosts = 25− 2 = 30
The subnet mask of /24 in CIDR notation corresponds
to 255.255.255.0. The above subnet mask has last
eight bits set to zero (32 − 24 = 8), which means that First subnet id is 199.11.171.32 (001 00000).
we have 28 IP addresses available. Total number of Second subnet id is 199.11.171.64 (010 00000).
valid hosts in a network is obtained by subtracting
Last subnet id is 199.11.171.192 (110 00000).
two addresses: subnet address and broadcast address
28− 2 = 256 − 2 = 254. First host of first subnet is 199.11.171.33 (001 00001).
Last host of last subnet is 199.11.171.222 (110 11110).
Problem 10.7: If the IP address of a system is Supernetting is a part of classless addressing. In class-
131.121.61.189, calculate the netid, first host, last less addressing, the addresses should be contiguous in a
host and directed broadcast address. block. The first address should be exactly divisible by
Solution: the number of addresses in a block. In supernet, bits are
borrowed from netid.
IP address (class B) 131.121.61.189
Rules of supernetting:
Net mask 255.255.0.0
1. The number of blocks must be power of 2.
Net id 131.121.0.0
2. The blocks must be continuous in the address
First host 131.121.0.1 space.
Directed broadcast address 131.121.255.255 3. The third octet of the first address in the super-
block must be exactly divisible by the number of
(All host bits 1)
blocks.
Problem 10.10: A company needs 1000 addresses, Table 10.5 | Different types defined
which of the following set of class C block can be used for ICMP messages
to form a supernet? Category Type Message
(a) 198.47.32.0, 198.47.33.0, 198.47.34.0 Error 3 Destination
(b) 198.47.31.0, 198.47.32.0, 198.47.33.0, 198.47.34.0 Reporting Unreachable
(c) 198.47.32.0, 198.47.42.0, 198.47.52.0, 198.47.62.0 Message
(d) 198.47.32.0, 198.47.33.0, 198.47.34.0, 198.47.35.0
4 Source Quench
Solution: (d) 11 Time Exceeded
(a) Not in power of 2 or blocks are 3. 12 Parameter Problem
(b) Third octet of first address is not divisible by 4. 5 Redirect Message
(c) Blocks are not contiguous.
Query 0 Echo Reply
Message
8 Echo Request
Problem 10.11: Which of the following can be 9 Router solicitation
the beginning address of a block that contains 16 10 Router advertisement
addresses?
13 Timestamp Request
(a) 205.16.37.32
14 Timestamp Reply
(b) 190.16.42.44
(c) 17.17.33.82 17 Address Mask Request
(d) 123.45.24.52 18 Address Mask Reply
Solution:
(a) 32 is divisible by 16. 10.7 LAYER 4: TRANSPORT LAYER
Extra options are specified in the rest of the header. Congestion occurs when packets overload the subnet
ICMP places the message to be sent in the data section. (means the number of packets sent to the network is greater
Different types defined for ICMP messages are shown in than the capacity of the network), which results in perfor-
Table 10.5. mance degrade. The following are causes for congestion:
1. A sudden stream of packet from various sources that is, a fast sender does not send at a rate faster than
reaching the same destination. the rate at which the receiver receives.
2. Slow links.
3. Slow processor.
4. Suppose an intermediate router has no free buffer, 10.9 USER DATAGRAM PROTOCOL AND
it will discard the packet. As the ender will not TRANSMISSION CONTROL PROTOCOL
receive any acknowledgement, it will resend the
packet and hence congestion will be there.
Congestion is unavoidable, but it is necessary to control
10.9.1 User Datagram Protocol
it. It can be handled with the following approach:
UDP, or User Datagram Protocol, is an unreliable and
1. Congestion information may be forwarded to connectionless protocol used by applications that trans-
the concerned node so that it is possible to limit mit small amount of data at one time and do not require
senders (prevent one sender from overflowing the receipt of acknowledgement of data, for example, audio
receiver) or reroute the packets. or video broadcasting (Fig. 10.8).
2. Resource availability may reduce the congestion.
3. Prevent additional packets from entering the con-
gestion region. Source port (16 bits) Destination port (16 bits)
To control congestion in network traffic, leaky bucket Length (16 bits) Checksum (16 bits)
algorithm is used. This algorithm shapes the bursty traf-
fic into fixed rate traffic. It does so by averaging the data Data
(4 bits) (6 bits)
Option + padding
Data
In TCP, sequence number is attached for every byte in 3. While the server is waiting, a client instantiates a
the segment. The initial sequence number for the first Socket object, which specifies the server name and
data byte will be a random number that is generated by port number for the connection.
a random number generator in the range of 0 to 232 − 1. 4. The constructor of the Socket class attempts to
Acknowledgement number will always be the sequence connect client to the specified server and port
number of the next expected data. The control bits and number. Once the communication is established,
their description is given in Table 10.6. the client has a Socket object for communicating
Table 10.6 | Control bits
with the server.
5. The accept() method returns a reference to a new
Control Bits Description socket on the server that is connected to the socket
of the client.
U (URG) Urgent pointer field significant
A (ACK) Acknowledgement field
TCP Server
significant
socket( )
P (PSH) Push function
R (RST) Reset the connection Well-known bind( )
port
S (SYN) Synchronize sequence numbers listen( )
F (FIN) No more data from sender TCP Client
accept( )
socket( )
Block until connection
10.10 SOCKETS connect( ) Connection establishment from client
(TCP three-way handshake)
write( ) Data (request)
A socket is an endpoint for communication between client read( )
process and server process across a network. A socket Process request
address is the combination of an IP address and a port
Data (reply) write( )
number. This address is used to send data packet to a read( )
particular process running on a machine. When two pro-
End-of-file notification read( )
grams are executed, a client process and a server process close( )
are created, and these processes communicate with each
close( )
other by reading from, and writing to, sockets.
There are few system calls, such as those given below: Figure 10.10 | Socket creation.
1. Socket() returns a socket descriptor (like file
descriptor), which is an integer value.
2. Bind() binds an address to a socket descriptor cre-
10.11 LAYER 5: SESSION LAYER
ated by socket.
3. Listen() announces willingness to accept The services provided by session layer are as follows:
connections.
4. Accept() blocks the caller until a connection 1. Establishes, manages and terminates a communi-
attempt arrives. cation session with remote systems
5. Connect() actively attempts to establish a 2. Allows two machines to enter into a dialog (com-
connection. munication may be half duplex or full duplex)
6. Send() sends some data over the connection. 3. Adds checkpoints or synchronisation points to a
7. Receive() receives some data from the connection. data stream.
8. Close() releases the connection. 4. Groups several user-level connections into a single
`session’.
The following steps (as shown in Figure 10.10) occur
when establishing a TCP connection between two com- Some protocol suites do not include the session layer.
puters using sockets: Checkpoint: If we need a file of 1000 pages, it is sug-
1. The server instantiates a ServerSocket object, gested to insert checkpoints after every 100 pages to
which denotes the port number on which the com- ensure that each 100-page unit is received and acknowl-
munication is to take place. edged independently. Meanwhile, if a crash occurs during
2. The server invokes accept() method of the the transmission of page 324, the only pages that need
ServerSocket class, which waits until a client con- to be resent after system recovery are pages 301 to 324.
nects to the server at the given port. The pages from 1 to 300 need not be resent.
10.12 LAYER 6: PRESENTATION and inverse domains. Generic domain specifies the
LAYER registered hosts according to their generic behaviour.
For example, com, org, edu, gov, net, etc. The coun-
try domain uses two character country abbrevia-
The following are the major concerns of presentation tions, for example, `in’ for India. The inverse domain
layer: is used to map an address to a name (Fig. 10.11).
1. Syntax and semantics of the information exchanged
between two systems.
2. Support to different encoding schemes used by dif- Application
ferent machines. It converts the sender-dependent
format into a common format and the receiver con- Telnet DHCP
verts it back to the receiver-dependent format. HTTP RIP
3. Data encryption–to ensure privacy, sensitive infor- FTP OSPF
mation should be encrypted. Information encrypted
SMTP TFTP
to some other form is unreadable to others.
4. Data compression–to reduce the size of informa- SNMP
tion to carry. In the case of multimedia, for exam- BOOTP
ple, text, audio and video, compression is a very Transport
useful tool.
TCP UDP
Note: Many protocol suites do not include a presentation
layer. Internet
ARP ICMP
RARP IGMP
10.13 LAYER 7: APPLICATION LAYER
Network interface
Ethernet
The major responsibility of application layer is to imple- Token ring
ment communication between two applications of the
FDDI ISDN
same type. There is a common misconception that every
user application runs on application layer, but it runs X.25 PPP
only on those applications which interact with the com-
munication system. For example, FTP, HTTP, SMTP/ Figure 10.11 | TCP/IP protocols.
POP3/IMAP (email)are all application layer protocols,
but a designing software or text editor cannot be consid- 3. Simple Mail Transfer Protocol (SMTP):
ered as an application layer protocol.The following are SMTP is a message transfer agent (MTA), which
popular application layer protocols: is used to transfer mail. A system should have
1. Internet Control Message Protocol (ICMP): client MTA for sending a mail and server MTA
ICMP provides error reporting and query manage- for receiving a mail. SMTP is used two times,
ment mechanism for host, which lacks in IP. ICMP between the sender and the sender’s mail server
messages are of two types: error-reporting messages and between the two mail servers. The main task
and query messages. The header of ICMP is of 8 of SMTP is to push the message from the client to
bytes and data section is of variable size. First byte the server.
is ICMP type, second byte is code, the next two 4. Post Office Protocol (POP): POP3 (version 3)
bytes specify the checksum field and rest of the is a message access protocol which is used to extract
header is specific for each message type. the message for client to the server. It has two
2. Domain Name System (DNS): DNS is a sup- modes: the delete mode and the keep mode. In the
porting program, which is used by other programs delete mode, the mail is deleted from the mailbox
used by the users, such as email. DNS is a client— after each retrieval, while in the keep mode, the
server program used to find the IP address of an mail remains in the mailbox after retrieval.
email recipient. In DNS client—server application, a 5. File Transfer Protocol (FTP): It is used for
sender sends an email to the email address of the transferring files from one system to another. FTP
receiver. The DNS client sends the request to the establishes two connections between hosts, one for
DNS server to map the email address to IP address. data transfer and the other for control information.
DNS has three different domains: generic, country FTP uses TCP Port 21 for the control connection
and TCP Port 20 for the data connection. The FTP Thus, repeater connects two segments of network
client has three components: user interface, client cable. It works at the physical layer of the OSI
control process and the client data transfer process. model.
The server has two components: the server control 2. Bridge: There is a limited number of stations
process and the server data transfer process. The that can be connected with a single LAN. So, a
control connection remains open for entire FTP bridge is used to connect multiple LANs of the
session, whereasthe data connection remains open same type. It operates on a physical layer and data
for each file transfer. link layer. A bridge checks the physical address
6. Hypertext Transfer Protocol (HTTP): HTTP contained in the frame. It also uses table for fil-
is used to access data on the World Wide Web tering frames. It partitions the collision so that
(WWW). It works as a combination of FTP and performance increases.
SMTP. Unlike FTP, HTTP does not have any con- 3. Hub: It is a network device which is used to con-
trol connection and uses only one TCP connection. nect various computers together. Hub is the central
Unlike SMTP, it does not store and then forward connection for all the computers, which connect
the messages. It immediately sends the messages. through Ethernet. Hub can receive and send the
HTTP uses a TCP Port 80. information but cannot perform both tasks at the
same time. This makes it slower than a switch. It
is less expensive and less complex.
10.14 DEVICES 4. Switch: Switch is a small network device used
to connect one or more computers through LAN.
It is mostly used in home networks. Switches and
The devices used for internetworking at different layers hubs are used in the same network. Hubs increase
are specified in Fig. 10.12. the network by providing more ports, and switches
divide the whole network into smaller networks.
Switching reduces the amount of unnecessary traf-
fic when every port sends the same information.
Application Switch also reduces the possibility of collision in
network.
5. Router: A router is a networking device which
takes packets from one network and after analy-
Content Service Switch sis sends that packet to another network. When a
Layer 4-7 Switches data packet comes to the router, the router reads
the destination address of the packet and sends it
to the respective router which contains that desti-
nation address (listed in its routing table). A router
is more intelligent than a hub because a hub only
sends the information between the devices but the
Network router analyses the packet and then forwards it
Router, Layer-3 Switch to the other network. It controls the traffic on the
network.
6. Gateway: It is a networking system capable of
Data Link
interconnecting one or more networks that has dif-
Switch, Bridge
ferent base protocol. Gateway serves as an entry
Physical and exit point. Gateway, sometime called as proto-
Hub, Repeater col converter, is used in different layers. For exam-
ple, a gateway can be used to convert a TCP/IP
10.15.1.1 Symmetric Cryptography
where M = output rate, C = capacity of router,
S = time of burst traffic and r = token rate
In symmetric key cryptography, a single shared key is
used for both encryption and decryption as shown in
10 + 6 × 10 ⋅ S = (8 × 10 ) ⋅ S
6 6 6
Fig. 10.13. Symmetric cryptographic primitives use block
106 (1 + 6S ) = (8 × 106 ) ⋅ S ciphers, stream ciphers, cryptographic hash functions,
1 + 6S = 8S and message authentication codes (MACs). Block cipher
2S = 1 Þ S = 0.5 s
uses a deterministic algorithm and operates on a block
(fixed length of bits) with unaltered transformation.
Stream cipher encrypts each bit individually to generate
cipher text. Hash functions or one-way hash functions
are used to map an arbitrary-length message string to
10.15 NETWORK SECURITY fixed-size message string. The final value is called hash
value.
IMPORTANT FORMULAS
SOLVED EXAMPLES
1. Which layer is responsible for delivery from process (c) Time stamping only
to process? (d) Time stamping, loose source routing, strict
source routing
(a) Network (b) Transport
(c) Physical (d) Data link Solution: It may contain values for various
options, such as strict source routing, security,
Solution: Transport layer is responsible for end-
record route, time stamp, etc.
to-end process communication.
Ans. (d)
Ans. (b)
7. In a fully connected mesh network with d devices
and c connections, there are ________ physical
2. The minimum size of an Ethernet frame is
(a) 18 bytes (b) 64 bytes channels to link all devices.
(c) 46 bytes (d) 56 bytes
(a) d*(d − 1)/2 (b) d*(d + 1)/2*c
Solution: Ethernet header = 18 Bytes [Dest. Mac (c) (2*d + 2*c)/2 (d) 2*(d + 1) + c
(6) + Source Mac (6) + Length (2) + CRC (4)]
Solution: The total number of wired links required
Minimum Data Portion = 46 Bytes and Minimum
to establish a fully connected mesh network of d
Ethernet Frame Size = 64 Bytes
nodes can be calculated as c = d (d − 1)/2.
Ans. (b) Ans. (a)
3. Using a 7-bit sequence number, what is the maxi- 8. In IPv4 header, the ______ field is used to determine to
mum size (in bits) of the sender and receiver which datagram a newly arrived fragment belongs to.
window using stop-and-wait protocols?
(a) Identification (b) Datagram-id
(a) 1 and 1 (b) 1 and 7 (c) Fragment offset (d) Time to live
(c) 7 and 1 (d) 7 and 7
Solution: Identification field is used to identify
Solution: Stop-and-wait protocol uses 1 bit sequence. original IP packet the fragments belong to.
Ans. (a) Ans. (a)
4. Vulnerable time in CSMA is 9. Encryption and decryption is the responsibility
(a) It is double the transmission time as it includes of _______ layer.
the sensing time (a) Session (b) Data link
(b) Half of average frame transmission time as (c) Application (d) Network
collisions are less
(c) 2 × (the average frame transmission time) Solution: Application layer is responsible for the
(d) None of the above encryption and decryption processes.
Ans. (c)
Solution: Vulnerable time for CSMA is the prop-
agation time Tp needed for a signal to propagate 10. Maximum throughput of an ALOHA network is
from one end of the medium to the other. (a) 18.4% (b) 35.8% (c) 36.8% (d) 50%
Ans. (d)
Solution: When G = 1, the throughput is increased
5. Which class of IP addresses is used for multicasting? to the maximum value of 36.8%.
(a) Class E (b) Class C Ans. (c)
(c) Class A (d) Class D 11. A terminal multiplexer has six 1200 bps terminals and
Solution: Class D is used for multicasting. Refer `N’ 300 bps terminals connected to it. The outgoing
Table 10.4. line is 9600 bps. What is the maximum value of N?
Ans. (a) (a) 4 (b) 6 (c) 8 (d) 12
6. The options field of IPv4 is used for? Solution: Since, there are six 1200 bps terminals.
(a) Time stamping, strict source routing So, 6 × 1200 + n × 300 = 9600 ⇒ n = 8
(b) Loose source routing, strict source routing Ans. (c)
12. The total number of wired links required to establish 16. Consider the network with subnet mask
a fully connected mesh network of 9 nodes will be 153.224.0.0/13. Determine the last host address in
the network.
(a) 36 (b) 56 (c) 72 (d) 64
(a) 153.208.255.255
Solution: Total number of wired links required
(b) 153.224.255.254
to establish a fully connected mesh network of n
nodes can be calculated as c = n (n − 1) / 2
(c) 153.231.255.255
(d) 153.231.255.254
So, for 9 nodes, total links are 36.
Solution: Network id: 153.224.0.0
Ans. (a)
Subnet mask: 255.248.0.0
13. Considering a classful addressing, the IP address By doing XOR between network id and subnet
128.252.144.84 denotes mask, one gets network id= 153.231.0.0
We have 19 bits for host, so first host address is
(a) 0.0.0.0 as network ID and 128.252.252.84 as
153.224.0.1 and last host is 153.231.255.254
node ID
Ans. (d)
(b) 128.0.0.0 as network ID and 128.252.127.84 as
node ID 17. Consider a token ring LAN of length 12 km and
(c) 128.252.0.0 as network ID and 128.252.144.84 having 40 stations, signal propagation speed is
as node ID 8 ns/m and data rate is 100 Mbps. An average
(d) 128.252.144.0 as network ID and 128.252.144.84 frame contains 220 bytes. Delay occurred at each
as node ID station is equivalent to 10-bit delay. What is the
utilisation of token ring approximately?
Solution: The IP belongs to class B. The network
id is 128.252.0.0 and the node id is 128.252.144.84. (a) 10% (b) 12%
Ans. (c) (c) 15% (d) 21%
14. In Ethernet CSMA/CD, the special bit sequence Solution: Transmission time of a frame
transmitted by media access management for colli- 220 × 8
sion handling is called Tt = s = 17.6 µs
100 × 106
(a) Hamming code (b) CRC
Propagation time around the ring
(c) Jam (d) Preamble
Tp = 10 × 12000 ns = 120 µs
Solution: Hamming code is a set of error-correc-
tion code, which is used to detect and correct bit Delay at each station = 1/10 ms, so delay at 40 sta-
errors. CRC (cyclic redundancy check) is used to tions will be = 4 ms
detect data transmission errors. Now,
Preamble is used in network communications for Tt
synchronizing transmission time between systems. Utilisation (U) =
Tt + Tp + delay at all stations
Ans. (c)
17.6 17.6
= =
15. A Gateway operates at ______________ layers. 17.6 + 120 + 4 141.6
(a) A
ll layers except physical and application = 0.124 × 100 = 12.4% ≈ 12%
layer Ans. (b)
(b) All the seven layers
18. A CSMA/CD-based network has transmission rate
(c) Only on session, transport and network layers
120 Mbps, length 1 km and speed of signal is 109
(d) Same layers on which the switch and bridge
m/s. What should be the minimum frame size?
operates
(a) 120 B (b) 240 B
Solution: Hubs, repeaters(or active hubs) operate
(c) 400 B (d) 440 B
at physical layer (1); Bridges, Switches operates
at data link layer (2) ; Routers operate at net- Solution: We know that
work layer (3) ; content-switches (or web-switches Length of packet Distance
or application-switches) operates at layers 4 to 7. = 2×
Bandwidth Velocity
Gateway operates at all the seven layers—physical,
1000
data link, network, transport (4), session (5), pres- So, packet length = 2× ×120 ×106 ⇒ 240 bytes
entation (6), application (7). 109
Ans. (b) Ans. (b)
19. How many 8-bit characters can be transmitted per 22. In the IPv4 addressing format, the number of net-
second over a 7800 baud serial communication link works allowed in class C addresses is
using asynchronous mode of transmission with one
(a) 221 (b) 222
start bit, 8 data bits and 1 parity bit?
(c) 223 (d) 224
(a) 600 (b) 660
(c) 780 (d) 1200 Solution: In class C,
Net id = 24 bit
Solution: Total number of bits = 10 Host id = 8 bits
Modulation rate = 7800 baud Out of 24 bits 3 bits are reserved for representation
Numbers of 8-bit characters are transmitted = class `C’, that is, 110.
7800 So, C class has 221networks.
= 780 Ans. (a)
10 bits
Ans. (c)
23. Consider a 2.5 Mbps token ring LAN and frame
20. In a token ring network, the transmission speed is size of 180 bytes. If the ring latency is 210 ms, then
20 bps and the propagation speed is 180 m/ms. The what will be the effective data rate of the LAN?
1-bit delay in this network is equivalent to:
(a) 1.40 Mbps (b) 1.56 Mbps
(a) 20 m of cable (b) 18 m of cable (c) 1.76 Mbps (d) 1.84 Mbps
(c) 9 m of cable (d) 5 m of cable
Solution:
Solution: Given that R = 2×107 bps, B = 1 bps, Data size 180 × 8
V = 180 m/ms; d =? Transmission time = = = 0.00057 s
Bandwidth 2.5×106
d
B = R× Ring latency = Round-trip time = 210 ms = 0.00021 s
v
Bv 1 ´ 180 Total time required to transfer 1440 bits =
d= = = 9 m of cable
2 × 107 × 10−6
(0.00057 + 0.00021) = 0.00078 s
R
So, effective data rate = 1440/0.00078 = 1.84 Mbps
Ans. (c)
Ans. (d)
21. Determine the maximum length of the cable (in
km) for transmitting data at a rate of 400 Mbps 24. How many number of parity bit is required in ham-
in an Ethernet LAN with frames of size 8000 bits. ming code if message size is 8 bit?
Assume the signal speed in the cable to be 250000
km/s. (a) 2 (b) 4
(c) 6 (d) 8
(a) 4 b) 5 (c) 5.5 (d) 7
Solution:
Solution: Maximum length of cable can be calcu-
Given message (m) = 8-bit
lated by the given problem:
To calculate the number of parity bits, we have
8000 bits 2×L the formula:
=
400 × 10 bits/s
6
25 × 104 km/s 2r ≥ m + r + 1, by putting r = 1, 2, 3, 4…
L = 5 km r = 4 will satisfy the given equality.
Ans. (b) Ans. (b)
1. Which of the following assertions is FALSE about (c) IP ensures that a packet is discarded if it is
the Internet Protocol (IP)? unable to reach its destination within a given
number of hops.
(a) It is possible for a computer to have multiple
(d) The packet source cannot set the route of an
IP addresses.
outgoing packet; the route is determined only
(b) IP packets from the same source to the same des-
by the routing tables in the routers on the way.
tination can take different routes in the network.
(GATE 2003: 1 Mark)
Solution: The packet source can set the route of Since Tx = N/B, where N is the number of bits to
an outgoing packet; the route is determined only be transmitted.
by the routing tables in the routers on the way. Number of bits transmitted, N = Round-trip delay
Ans. (d) × B = 2 × 105× 107 = 200 bytes
Ans. (c)
2. Which of the following functionalities must be
implemented by a transport protocol over and 5. Host A is sending data to host B over a full duplex
above the network protocol? link. A and B are using the sliding window protocol
for flow control. The send and receive window sizes
(a) Recovery from packet losses are 5 packets each. Data packets (sent only from A
(b) Detection of duplicate packets to B) are all 1000 bytes long and the transmission
(c) Packet delivery in the correct order time for such a packet is 50 ms. Acknowledgement
(d) End-to-end connectivity packets (sent only from B to A) are very small
(GATE 2003: 1 Mark) and require negligible transmission time. The
propagation delay over the link is 200 ms. What
Solution: End-to-end delivery is the responsibil- is the maximum achievable throughput in this
ity of a transport layer. communication?
Ans. (d) (a) 7.69 × 106 bps (b) 11.11 × 106 bps
3. The subnet mask for a particular network is (c) 12.33 × 106 bps (d) 15.00 × 106 bps
255.255.31.0. Which of the following pairs of IP (GATE 2003: 2 Marks)
addresses could belong to this network? Solution:
(a) 172.57.88.62 and 172.56.87.233 Throughput = 1 window/RTT
(b) 10.35.28.2 and 10.35.29.4 Round-trip time (RTT) = Transmission time + 2 ×
(c) 191.203.31.87 and 191.234.31.88 Propagation time
(d) 128.8.129.43 and 128.8.161.55 = 50 ms + 2 × 200 ms = 450 ms
(GATE 2003: 2 Marks)
As the size of window is 5 packets and 1 packet
Solution: The two addresses should belong to the contains 1000 bytes.
same network, if binary AND operation of both The total size of the packet in bytes is 5 × 1000 =
addresses with net mask should be same. 50000 bytes
5000 bytes
The last octets of IP addresses of 0 is 000 0000. Therefore, throughput = −6
= 11.11 × 106 bps
The last octets of IP address of 43 and 55 and their 450 × 10 s
AND with net mask gives the same result. 5000 bytes
= 11.11 × 106 bps
450 × 10−6 s
Ans. (d)
Ans. (b)
4. A 2-km long broadcast LAN has 107 bps bandwidth
and uses CSMA/CD. The signal travels along the 6. Choose the best matching between Groups 1 and 2.
wire at 2 × 108 m/s. What is the minimum packet
size that can be used on this network? Group: 1 Group: 2
(a) 50 bytes (b) 100 bytes
(c) 200 bytes (d) None of the above P. Data link layer 1. Ensures reliable
(GATE 2003: 2 Marks) transport of data over
a physical point-to-
Solution: Given that length L = 2 km = 2000 m point link
Bandwidth, B = 107 bps Q. Network layer 2. Encodes/decodes
Signal travels S = 2 × 108 m/s data for physical
L 2 × 103
transmission
1
So, propagation delay (Tp ) = = = 5s
2 × 10
S 8 R. Transport layer 3. Allows end-to-end
10
In CSMA/CD network, communication between
two processes
Round-trip delay = 2 ×Tp = 2 × 105 s
The minimum packet size must take round-trip 4. Routes data from one
delay to transmit. network node to the
So, transmission delay (Tx) = Round-trip delay next
(a) P:1, Q:4, R:3 (b) P:2, Q:4, R:1 So, A’s collision counter will be reinitialized to 0.
(c) P:2, Q:3, R:1 (d) P:1, Q:3, R:2 At the second collision, A and B will have first and
(GATE 2004: 1 Mark) second collisions, respectively.
So, A will have to select random number from [0,1].
Solution: Data link layer provides reliable trans-
But, B will have to select from [0, 1, 2, 3].
mission of data over a physical link.
On mapping, the following are total favouring cases
Network layer provides routing of data packets in
to A’s winning.
the network.
Transport layer is responsible for end-to-end 0—1 0—2 0—3 1—2 1—3
process communication. So, the probability is 5/8 = 0.625.
Ans. (a) Ans. (b)
7. Which of the following is NOT true with respect to 10. The routing table of a router is shown below:
a transparent bridge and a router?
Destination Subnet Mask Interface
(a) Both bridge and router selectively forward data
128.75.43.0 255.255.255.0 Eth0
packets.
(b) A bridge uses IP addresses while a router uses 128.75.43.0 255.255.255.128 Eth1
MAC addresses. 192.12.17.5 255.255.255.255 Eth3
(c) A bridge builds up its routing table by inspect-
Default Eth2
ing incoming packets.
(d) A router can connect between a LAN and a WAN.
On which interface will the router forward pack-
(GATE 2004: 1 Mark)
ets addressed to destinations 128.75.43.16 and
Solution: Both router and bridge selectively for- 192.12.17.10, respectively?
ward data packets and both can connect between
(a) Eth1 and Eth2 (b) Eth0 and Eth2
a LAN and WAN. Routing and bridge builds their
(c) Eth0 and Eth3 (d) Eth1 and Eth3
routing table by inspecting incoming packets but
(GATE 2004: 2 Marks)
router and bridge both use MAC address. So,
option (b) is correct. Solution: On performing AND operation between
Ans. (b) incoming IP address and subnet-mask and compar-
ing the result with the destination.
8. How many 8-bit characters can be transmitted per
second over a 9600 baudserial communication link using If there is a match between multiple destinations,
asynchronous mode of transmission with one start bit, then select the destination with the longest length
eight data bits, two stop bits, and one parity bit? subnet mask.
128.75.43.16 matches with 128.75.43.0 and
(GATE 2004: 1 Mark) 128.75.43.0. But the packets addressed to
(a) 600 (b) 800 (c) 876 (d) 1200 128.75.43.16 will be forwarded to Eth1.
Solution: If a result is not matching with any of the given
destinations, then the packet is forwarded to the
9600 default interface (here Eth2).
= 800
1+ 8 + 2 +1 Therefore, the packets addressed to 192.12.17.10
Ans. (b) will be forwarded to Eth2.
9. A and B are the only two stations on an Ethernet. Ans. (a)
Each has a steady queue of frames to send. Both A Common Data Questions 11 and 12: Consider
and B attempt to transmit a frame, collide, and A three IP networks A, B and C. Host HA in net-
wins the first back-off race. At the end of this suc- work A sends messages each containing 180 bytes
cessful transmission by A, both A and B attempt of application data to a host HC in network C. The
to transmit and collide. The probability that A TCP layer prefixes a 20-byte header to the mes-
wins the second back-off race is sage. This passes through an intermediate network
(a) 0.5 (b) 0.625 (c) 0.75 (d) 1.0 B. The maximum packet size, including 20-byte IP
header, in each network is
(GATE 2004: 2 Marks)
A: 1000 bytes
Solution: At the first collision, both A and B will B: 100 bytes
have first collision but A wins. C: 1000 bytes
The network A and B are connected through a 1 15. The maximum window size for data transmission
Mbps link, while B and C are connected by a 512 using the selective reject protocol with n-bit frame
Kbps link (bps = bits per second). sequence numbers is
(a) 2n (b) 2n−1 (c) 2n − 1 (d) 2n−2
1 Mbps 512 Mbps (GATE 2005: 1 Mark)
Network A Network B Network C
Solution: For selective reject protocol, window
11. Assuming that the packets are correctly delivered, size = 2n/2 = 2n−1
how many bytes, including headers, are delivered Ans. (b)
to the IP layer at the destination for one applica- 16. In a network of LANs connected by bridges,
tion message, in the best case? Consider only data packets are sent from one LAN to another
packets. through intermediate bridges. Since more than
(a) 200 (b) 220 one path may exist between two LANs, packets
(c) 240 (d) 260 may have to be routed through multiple bridges.
(GATE 2004: 2 Marks) Why is the spanning tree algorithm used for
bridgerouting?
Solution: When all the 3 packets are delivered,
the bytes are (a) For shortest path routing between LANs
(b) For avoiding loops in the routing paths
(80 + 20) + (80 + 20) + (40 + 20) = 260 (c) For fault tolerance
Ans. (d) (d) For minimising collisions
12. What is the rate at which application data is trans- (GATE 2005: 1 Mark)
ferred to host HC? Ignore errors, acknowledgements
Solution: Spanning tree protocol is used for bridge
and other overheads.
routing to avoid loops in routing paths.
(a) 325.5 Kbps (b) 354.5 Kbps Ans. (b)
(c) 409.6 Kbps (d) 512.0 Kbps
17. An organisation has a class B network and wishes
(GATE 2004: 2 Marks)
to form subnets for 64 departments. The subnet
Solution: Actual data sent =180 out of 260 mask would be
So, data rate (180/260) × 512 Kbps = 354.461 Kbps
(a) 255.255.0.0 (b) 255.255.64.0
Ans. (b)
(c) 255.255.128.0 (d) 255.255.252.0
13. Packets of the same session may be routed through (GATE 2005: 1 Mark)
different paths in:
Solution: For class B, 16 bits are reserved as
(a) TCP, but not UDP (b) TCP and UDP network bits. To allocate 64 subnets, 6 bits are
(c) UDP, but not TCP (d) Neither TCP nor UDP added to network bits. 22 bits are for network.
(GATE 2005: 1 Mark) Subnet mask is created by assigning 1 to all net-
work bits.
Solution: Packets travel on network layer. TCP
So, mask is 255.255.252.0.
and UDP are transport layer protocols.
Ans. (d)
Ans. (b)
14. The address resolution protocol (ARP) is used for: 18. In a packet-switching network, packets are routed
from source to destination along a single path
(a) finding the IP address from the DNS. having two intermediate nodes. If the message size
(b) finding the IP address of the default gateway. is 24 bytes and each packet contains a header of 3
(c) finding the IP address that corresponds to a bytes, then the optimum packet size is
MAC address.
(d) finding the MAC address that corresponds to (a) 4 (b) 6 (c) 7 (d) 9
an IP address. (GATE 2005: 2 Marks)
(GATE 2005: 1 Mark) Solution: Optimal packet size is 9.
Solution: ARP works to find the physical address 9−3 = 6 B will be transferred in one packet, so the
of a machine. total message will travel in 4 packets.
Ans. (d) Ans. (d)
19. Suppose the round-trip propagation delay for a and netmask 255.255.192.0. Which one of the fol-
10 Mbps Ethernet having 48-bit jamming signal is lowing statements is true?
46.4 s. The minimum frame size is
(a) C 1 and C2 both assume they are on the same
(a) 94 (b) 416 network.
(c) 464 (d) 512 (b) C2 assumes C1 is on the same network, but C1
(GATE 2005: 2 Marks) assumes C2 is on a different network.
Solution: RTT = 46.4 × 10−6s
(c) C1 assumes C2 is on the same network, but C2
assumes C1 is on a different network.
10 Mb are sent in = 1 s (d) C1 and C2 both assume they are on different
1 will be sent = 1/107 networks.
48 bits will be in = 4.8 × 10−6 s (GATE 2006: 2 Marks)
Total delay = 46.4 + 4.8 = 51.2 Solution: Network address of C1: 203.197.2.53
Minimum frame size = 51.2 × 10 = 512 bits AND 255.255.128.0 = 203.197.0.0
Ans. (d)
Network address of C2: 203.197.75.201 AND
20. For which one of the following reasons does Internet
255.255.192.0 = 203.197.0.0
Protocol (IP) use the time- to-live (TTL) field in
C1 assumes C2 is on the same network, but C2
the IP datagram header?
assumes C1 on different network.
(a) Ensure packets reach destination within that Ans. (c)
time
23. Station A needs to send a message consisting of
(b) Discard packets that reach later than that
9 packets to station B using a sliding window
time
(window size 3) and Go-Back-N error control strat-
(c) Prevent packets from looping indefinitely
egy. All packets are ready and immediately avail-
(d) Limit the time for which a packet gets queued
able for transmission. If every 5thpacket that A
in intermediate routers
transmits gets lost (but no packets from B ever get
(GATE 2006: 1 Mark) lost), then what is the number of packets that A
Solution: Decrementing TTL will prevent packet will transmit for sending the message to B?
from looping. (a) 12 (b) 14 (c) 16 (d) 18
Ans. (b) (GATE 2006: 2 Marks)
21. Station A uses 32 byte packets to transmit mes- Solution:
sages to Station B using a sliding window protocol. A B
The round trip delay between A and B is 80 milli- 1 → 1
seconds and the bottleneck bandwidth on the path 2 → 2
between A and B is 128 kbps. What is the optimal 3 → 3
window size that A should use? 4 → 4
(a) 20 (b) 40 5 → lost
(c) 160 (d) 320 6 → discard
7 → discard
→
(GATE 2006: 2 Marks)
8 5
Solution: Round-trip delay = 80 ms 9 → 6
10 → lost 7
11 →
Bandwidth = 128 Kbps
discard 8
12 →
In 1 s = 128 Kbits are sent
In 80 ms = 128K × 80 × 10−3 = 128 × 80 bits
discard 9
13 → 7
128 × 80 14 → 8
Window size =
32 × 8
= 40 15 → lost 9
Ans. (b) 16 → 9
Ans. (c)
22. Two computers C1 and C2 are configured as fol-
lows. C1 has IP address 203.197.2.53 and netmask Linked Answer Questions 24 and 25: Consider the
255.255.128.0. C2 has IP address 203.197.75.201 diagram shown below where a number of LANs
B1 Hosts Port
H1 H2 1 2 H3 H4 H1, H2, H3, H4 3
H5, H7, H9, H10 1
2 3
4 H7, H8, H11, H12 4
B5 3 B3
1 4 1 2 (GATE 2006: 2 Marks)
Solution: Forwarding table for B3 is
H5 H6 H7 H8
2 2 1 Hosts Port
1 B4 B2
H1, H2, H3, H4 3
3 3
H5, H6, H9, H10 1
H9 H10 H11 H12
H7, H8, H11, H12 2
24. For the given connection of LANs by bridges, which
one of the following choices represents the depth Ans. (a)
first traversal of the spanning tree of bridges? 26. In Ethernet, when Manchester encoding is used,
the bit rate is
(a) B1, B5, B3, B4, B2 (b) B1, B3, B5, B2, B4
(c) B1, B5, B2, B3, B4 (d) B1, B3, B4, B5, B2 (a) Half the baud rate (b) Twice the baud rate
(c) Same as the baud rate (d) None of the above
Solution: DFS (depth first search) is analgo- (GATE 2007: 1 Mark)
rithm for traversing tree or graph. One starts at
the root (selecting some arbitrary node as the root Solution: For transmission of digital information,
in the case of a graph) and explores as far as pos- Manchester coding is used to convert digital infor-
sible along each branch before backtracking. So mation into electrical signals for transmission. It
depth first search traversal is B1, B5, B3, B4, B2. uses two baud for one bit.
(GATE 2006: 2 Marks) Ans. (b)
Ans. (a) 27. Which one of the following uses UDP as the trans-
port protocol?
25. Consider the correct spanning tree for the previous
question. Let host H1 send out a broadcast ping (a) HTTP (b) Telnet
packet. Which of the following options represents (c) DNS (d) SMTP
the correct forwarding table on B3? (GATE 2007: 1 Mark)
(a) Solution: DNS uses services of UDP protocol.
Hosts Port Ans. (c)
H1, H2, H3, H4 3 28. There are n stations in a slotted LAN. Each station
H5, H6, H9, H10 1 attempts to transmit with a probability p in each
time slot. What is the probability that ONLY one
H7, H8, H11, H12 2
station transmits in a given time slot?
200 m can be travelled in 1 ms. Therefore, in 0.1 ms, For maximum utilisation: nK = 2LtR + K
20 m can be travelled. 2LtR + K
Therefore, n=
Ans. (c) K
Number of bits needed for n frames is log n.
30. The address of a class B host is to be split into
Ans. (c)
subnets with a 6-bit subnet number. What is the
maximum number of subnets and the maximum 33. Match the following:
number of hosts in each subnet?
Column I Column II
(a) 62 subnets and 262142 hosts
(b) 64 subnets and 262142 hosts (P) SMTP (1) Application layer
(c) 62 subnets and 1022 hosts (Q) BGP (2) Transport layer
(d) 64 subnets and 1024 hosts (R) TCP (3) Data link layer
(GATE 2007: 2 Marks)
(S) PPP (4) Network layer
Solution: Here 16 + 6 = 22 bits are reserved for
(5) Physical layer
the network
Number of hosts = 232−22 = 210 − 2 = 1022 (a) P − 2, Q − 1, R − 3, S − 5
Number of subnets = 26 − 2 = 62 (b) P − 1, Q − 4, R − 2, S − 3
Ans. (c) (c) P − 1, Q − 4, R − 2, S − 5
31. The message 11001001 is to be transmitted using (d) P − 2, Q − 4, R − 1, S − 3
the CRC polynomial x3 + 1 to protect it from (GATE 2007: 2 Marks)
errors. The message that should be transmitted is Solution:
(a) 11001001000 (b) 11001001011 SMTP: Application layer for mail transfer
(c) 11001010 (d) 110010010011 BGP: Network layer routing protocol
(GATE 2007: 2 Marks) TCP: Transport layer transmission protocol
PPP: Data link layer protocol for direct connections
Solution: The polynomial is equivalent to 1001. Ans. (b)
Divide 1001 with 11001001000 and find the remainder.
34. What is the maximum size of data that the appli-
Remainder is 011. Append 011 at the end of input
cation layer can pass on to the TCP layer below?
string, it will be 11001001011.
Ans. (b) (A) Any size
32. The distance between two stations M and N is (B) 216 bytes − size of TCP header
L km. All frames are K bits long. The propaga- (C) 216 bytes
tion delay per kilometre is t s. Let R bits/s be the (D) 1500 bytes
channel capacity. Assuming that processing delay (GATE 2008: 1 Mark)
is negligible, the minimum number of bits for the Solution:
sequence number field in a frame for maximum util- There is no limit of data passing at application layer.
isation, when the sliding window protocol is used, is Ans. (a)
35. Which of the following system calls results in the Subsequently, the client process P executes a
sending of SYN packets? socket() system call followed by connect() system
call to connect to the server process S. The server
(a) Socket (b) Bind
process has not executed any accept() system call.
(c) Listen (d) Connect
Which one of the following events could take place?
(GATE 2008: 1 Mark)
(a) Connect() system call returns successfully
Solution: Connect() is called by the client and con- (b) Connect() system call blocks
nection is established using three-way hand shake. (c) Connect() system call returns an error
Ans. (d) (d) Connect() system call results in a core dump
(GATE 2008: 2 Marks)
36. In the slow start phase of the TCP congestion con-
trol algorithm, the size of the congestion window Solution: The accept() call does not execute.
So, connect() call did not get response for a time
(a) does not increase. stamp to wait, therefore, connect() system call
(b) increases linearly. returns an error.
(c) increase quadratically. Ans. (c)
(d) increase exponentially.
(GATE 2008: 2 Marks) 40. In the RSA public key cryptosystem, the private
and public keys are (e,n) and (d,n), respectively,
Solution: In the slow start phase of the TCP con- where n = p*q and p and q are large primes. Besides,
gestion control algorithm, the size of the congestion n is public and p and q are private. Let M be an
window increases exponentially. integer such that 0 < M < n and f (n) = (p − 1)
Ans. (d) (q − 1). Now, consider the following equations:
37. If a class B network on the Internet has a subnet I. M′ = Memod n
mask of 255.255.248.0, what is the maximum M = (M′)d mod n
number of hosts per subnet? II. ed ≡ 1 mod n
III. ed ≡ 1 mod f (n)
IV. M′ = Memod f (n)
(a) 1022 (b) 1023
M = (M′)dmod f (n)
(c) 2046 (d) 2047
(GATE 2008: 2 Marks)
Which of the above equations correctly represent
Solution: Number of network bits = 21 the RSA cryptosystem?
What is the maximum duration for which the com- 41. While opening a TCP connection, the initial
puter can transmit at the full 10 Mbps? sequence number is to be derived using a time-of-
(a) 1.6 s (b) 2 s day (ToD) clock that keeps running even when the
(c) 5 s (d) 8 s host is down. The low order 32 bits of the coun-
(GATE 2008: 2 Marks) ter of the ToD clock is to be used for the initial
sequence numbers. The clock counter increments
Solution: Data transfer of token bucket = 10 Mbps once per millisecond. The maximum packet life-
Rate of transfer = 2 Mbps time is given to be 64s.
Initially filled capacity = 16 Mbits Which one of the choices given below is closest to the
minimum permissible rate at which sequence num-
b 16
Maximum burst time = = =2 s bers used for packets of a connection can increase?
M − r 10 − 2
Ans. (b) (a) 0.015/s (b) 0.064/s
39. A client process P needs to make a TCP connec- (c) 0.135/s (d) 0.327/s
tion to a server process S. Consider the following (GATE 2009: 2 Marks)
situation: the server process S executes a socket(), Solution: The frames from the sending station are
a bind() and a listen() system call in that order, numbered sequentially.
following which it is pre-empted. Ans. (b)
42. Let G(x) be the generator polynomial used for CRC (c) It can be used to optimise throughput.
checking. What is the condition that should be sat- (d) It can be used to prevent packet looping.
isfied by G(x) to detect odd number of bits in error? (GATE 2010: 1 Mark)
(a) G(x) contains more than two terms. Solution: Value of TTL is decremented to pre-
(b) G(x) does not divide 1+xk, for any not exceed- vent packet looping.
ing the frame length k. Ans. (d)
(c) 1+x is a factor of G(x). 46. Which one of the following is not a client—server
(d) G(x) has an odd number of terms. application?
(GATE 2009: 2 Marks)
(a) Internet chat (b) Web browsing
Solution: To detect odd number of errors, (x + 1) (c) E-mail (d)Ping
must be present. (GATE 2010: 1 Mark)
Ans. (c)
Solution: ping is a command not an application.
Linked Answer Questions 43 and 44: Frames of 1000 It is to check connectivity and there is no need to
bits are sent over a 10 bps duplex link between two communicate with server.
hosts. The 6 propagation time is 25 ms. Frames are Ans. (d)
to be transmitted into this link to maximally pack
47. Suppose computers A and B have IP addresses
them in transit (within the link).
10.105.1.113 and 10.105.1.91, respectively, and
43. What is the minimum number of bits (l) that will they both use the same net mask N. Which of the
be required to represent the sequence numbers dis- values of N given below should not be used if A and
tinctly? Assume that no time gap needs to be given B should belong to the same network?
between transmissions of two frames.
(a) 255.255.255.0 (b) 255.255.255.128
(a) l = 2 (b) l = 3 (c) 255.255.255.192 (d) 255.255.255.224
(c) l = 4 (d) l = 5 (GATE 2010: 2 Marks)
(GATE 2009: 2 Marks)
Solution: To belong to a different network, A and
Solution: Transmission delay of link = 1000/106 B should have different network address.
= 1 ms
Perform logical AND operation on all the options.
Propagation delay = 25 ms For example,option (d)
Maximum 25 frames can be sent, for 25 frames bits IP address of A: 10.105.1. 01110001
required are 5. Network mask: 255.255.255. 11100000
Ans. (d) Network address of A: 10.105.1. 01100000
44. Suppose that the sliding window protocol is used IP address of B: 10.105.1. 01011011
with the sender window size of 2l, where l is the Network mask: 255.255.255. 11100000
number of bits identified in the earlier part and l Network address of A: 10.105.1. 01000000
acknowledgements are always piggy backed. After Both have different network addresses.
sending 2l frames, what is the minimum time the Ans. (d)
sender will have to wait before starting transmis-
Linked Answer Questions 48 and 49: Consider a net-
sion of the next frame? (Identify the closest choice
work with 6 routers R1 to R6 connected with links
ignoring the frame processing time.)
having weights as shown in the following diagram
(a) 16 ms (b) 18 ms (c) 20 ms (d) 22 ms
(GATE 2009: 2 Marks) R2 7 R4
weight of the respective connecting link. After all Following will be distance vectors of all nodes
the routing tables stabilise, how many links in the R1 (2, 3, 9, 10, 11)
network will never be used for carrying any data? Links used: R1—R2, R1—R3, R2—R4, R4—R5, R4—R6
(a) 4 (b) 3 (c) 2 (d) 1 R2 (2, 7, 8, 9)
(GATE 2010: 2 Marks) Links used: R2—R3, R2—R4, R4—R5, R4—R6
R3 (9, 9, 11)
Solution: We can check one by one all shortest Links used: R3—R2, R2—R4, R3—R5, R4—R6
distances. When we check for all shortest distances R4 (1, 2)
for Ri we don’t need to check its distances to R0 Links used: R4—R5, R4—R6
to Ri−1 because the network graph is undirected. R5 (3)
Following will be distance vectors of all nodes. Links used: R5—R4, R4—R6
Shortest distances from R1 to R2, R3, R4, R5 and R6 If we mark all the used links one by one, we can see
R1 (5, 3, 12, 12, 16) that following links are never used.
Links used: R1—R3, R3—R2, R2—R4, R3—R5, R5—R6 R5—R6
Ans. (b)
Shortest distances from R2 to R3, R4, R5 and R6
R2 (2, 7, 8, 12) 50. A layer-4 firewall (a device that can look at all pro-
Links used: R2—R3, R2—R4, R4—R5, R5—R6 tocol headers up to the transport layer) CANNOT
Shortest distances from R3 to R4, R5 and R6 (a) B lock entire HTTP traffic during 9:00PM and
R3 (9, 9, 13) 5:00 AM
Links used: R3—R2, R2—R4, R3—R5, R5—R6 (b) Block all ICMP traffic
Shortest distances from R4 to R5 and R6 (c) Stop incoming traffic from a specific IP address
R4 (1, 5) but allow outgoing traffic to the same IP address
Links used: R4—R5, R5—R6 (d) Block TCP traffic from a specific user on a
Shortest distance from R5 to R6 multi-user system during 9:00 PM and 5:00 AM
R5 (4) (GATE 2011: 1 Mark)
Links used: R5—R6 Solution: As Layer-4 firewall cannot block traf-
If we mark all the used links one by one, we can see fic of Layer-5 (application layer) and HTTP is an
that following links are never used. application layer protocol. So, option (a) is correct.
R1—R2 R4—R6 Ans. (a)
Ans. (c)
51. Consider different activities related to email.
49. Suppose the weights of all unused links in the pre- m1: Send an email from a mail client to a mail
vious question are changed to 2 and the distance server
vector algorithm is used again until all routing tables m2: Download an email from mailbox server to a
stabilise. How many links will now remain unused? mail client
(a) 0 (b) 1 (c) 2 (d) 3 m3: Checking email in a web browser
(GATE 2010: 2 Marks) Which is the application level protocol used in each
Solution: After the weights of unused links() are activity?
changed to following graph. (a) ml: HTTP m2: SMTP m3: POP
(b) ml: SMTP m2: FTP m3: HTTP
R2 7 R4 (c) ml: SMTP m2: POP m3: HTTP
(d) ml: POP m2: SMTP m3: IMAP
2 (GATE 2011: 1 Mark)
2
2 1 Solution:
R1 R6
m1: SMTP is responsible for mail transfer.
m2: POP is responsible for downloading mail from
3 3 mailbox server to mal client.
m3: HTTP is responsible for viewing application on
R3
9 web browser.
R5
Ans. (c)
Linked Answer Questions 52 and 53: Consider a 54. Which of the following transport layer protocols is
network with five nodes, N1 to N5, as shown below. used to support electronic mail?
(a) SMTP (b) IP (c) TCP (d) UDP
N1
(GATE 2012: 1 Mark)
62. Assume that source S and destination D are con- Solution: Data rate: 500 Mbps = 5 × 108 bps
nected through two intermediate routers labelled R. Frame size: 10000 bits
Speed: 200000 km/s
S R R D 5 × 108 bits can travel = 1s
PRACTICE EXERCISES
5. MTU stands for 15. How many hops can be travelled by the IP packet
having TTL value 5?
(a) Minimum Transfer Unit
(b) Minimum Telephony Unit (a) It cannot travel any hop.
(c) Maximum Transfer Unit (b) It can travel only single hop.
(d) Memory Transfer Unit (c) It can travel five hops.
(d) It can travel four hops.
6. Using 7-bit sequence number, what is the maxi-
mum size (in bits) of the sender and receiver 16. What does the acronym ISDN for?
window using Go-Back-N ARQ?
(a) Indian Standard Data Network
(a) 127 and 1 (b) 1 and 127 (b) Integrated Services Digital Networks
(c) 127 and 127 (d) 1 and 1 (c) Intelligent Secure Digital Network
(d) Intelligent Services Digital Network
7. Using a 5-bit sequence number, what is the maxi-
mum size of the sender and receiver window using 17. If N is the maximum sequence number, then the
selective repeat ARQ? window size in selective repeat and Go-Back-N
(a) 16 and 16 (b) 1 and 16 protocols are, respectively,
(c) 16 and 1 (d) Depend upon the bits (a) N/2, N − 1 (b) N/2, N + 1
selected to be repeated (c) N + 1/2, N − 1/2 (d) N − 1, N
8. Which protocol is being used in wireless to over- 18. CRC can detect all burst error of up to M errors, if
come collision? generator polynomial G(x) is of degree
(a) CSMA/CD (a) It does not matter (b) M − 1
(b) CSMA/CA (c) M/2 (d) M + 1
(c) Both
(d) WEP 19. Which layers of the OSI reference model are host-
to-host layers?
9. The host bits present in class B IP address are
(a) Transport, session, presentation, application
_______.
(b) Session, presentation, application
10. Which protocol is used to provide error reporting (c) Datalink, transport, presentation, application
services to IP address? (d) Physical, datalink, network, transport
(a) ICMP (b) ARP 20. A nibble is used for packed sequence number-
(c) BGP (d) OSPF ing in a sliding window protocol used in a
computer network. What is the maximum window
11. Which protocol is used to find the physical address size?
for a logical address?
(a) D epends upon the underlying network
(a) ARP (b) RARP protocol
(c) IGMP (d) ICMP (b) Depends upon the number of nodes involved in
the network
12. Which protocol cannot be used to find the logi- (c) 15
cal address if the server is residing out of your (d) 16
network?
21. Infrared signals can be used in a closed area using
_______ propagation.
(a) ARP (b) RARP
(c) BOOTP (d) IGMP
(a) Sky (b) Ground
13. Which topology requires a central controller or hub? (c) Line of sight (d) Small signal
(a) Bus (b) Star 22. A bridge has access to _______ address in the same
(c) Mesh (d) Ring network.
14. The number of nibbles are reserved in the IP (a) Physical (b) Logical
header for header length is _______. (c) Supernet (d) Link state
37. A receiver receives the IP packet with the first 8 (a) 3000 (b) 1544
bits as 10100011. The length of the IP packet is (c) 110 (d) 64
(a) 163 bytes (b) 1 byte 45. In a pure ALOHA network, 100 stations share a
(c) 127 bytes (d) 12 bytes link of 1 Mbps. The throughput of such a network
38. The router performs at ________ layer(s). having 1000 bits size of frames and each station is
transmitting at the rate of 10 frames/s is
(a) Only physical
(b) Physical, datalink and network (a) 10% (b) 13.53%
(c) Physical, datalink and transport (c) 12.8% (d) 18.16%
(d) Datalink and network 46. An HDLC does not support
39. A packet has arrived with the offset value 100 and (a) simplex
HLEN value 5. The total length value is 100. What (b) multipoint link
is the number of first byte and the last byte? (c) balanced multipoint
(a) 879 (b) 880 (d) full duplex
(c) 881 (d) 882
47. A hub in the network is
40. Which of the following is true?
(a) a passive device
(a) In TCP/IP-based services, the destination (b) an active device
address is to be specified only during the initial (c) a server that serves every node
stage of setup. (d) a power supply concentrator
(b) Initial setup is required for UDP-based service.
(c) Packet sequencing is not guaranteed in TCP/ 48. Consider the following message: M = 1010001101.
IP-based services. The CRC of this message using the divisor polyno-
(d) Initial setup is not possible in UDP-based service. mial x5 + x4 + x2 + 1 is
41. How many characters (7 bits + 1 parity) can be (a) 1110 (b) 0101
transmitted over 2400 bps line, if the transfer is (c) 1001 (d) 0010
asynchronous (1 start and 1 stop)? 49. The broadcast address for the subnet that IP address
(a) 2400/8 (b) 2400/10 192.168.26.8, 255.255.255.248 is a member of
(c) 2400 × 8/2 (d) 256 (a) 192.168.26.225 (b) 192.168.26.255
42. In CRC, if the data unit is 110111001 and the divi- (c) 192.168.127.255 (d) 192.168.26.0
sor is 1011 then what is the dividend at the receiver? 50. A sender uses public key cryptography to send a
(a) 110111001101 secret message. Which of the following is true?
(b) 110111001000 (a) Sender encrypts using receiver’s public key
(c) 110111001011 (b) Sender encrypts using his own public key
(d) 101110111001 (c) Receiver decrypts using sender's private key
43. With respect to ICMP, which of the following (d) Receiver decrypts using own private key
statements is false?
51. Which of the following functionality must be
(a) ICMP reports about the routers. implemented by transport layer over and above the
(b) ICMP is also used to test connectivity. network protocol?
(c) ICMP and BOOTP are equivalent in their
(a) Recovery from packet loss
usage.
(b) End to end connectivity
(d) An ICMP message type is encapsulated for
(c) Packet delivery in encapsulated abstract order
transmission.
(d) Secure transmission from end to end
44. A 3000-lm long trunk is used to transmit frames
52. During the process of packet forwarding by the
routers, _____ field is not updated.
using Go-Back-N protocol. The propagation speed
is 6 ms/km and the trunk data rate is 1.544 Mbps.
We ignore the time it takes to receive the acknowl- (a) IP header source address
edgement bits. Frame size is 64 bytes. What should (b) IP header target address
be the maximum window size at the sender’s side (c) IP header TTL
in order to achieve 100% efficiency? (d) IP header checksum
53. Consider the network, using CIDR addressing, a PC 7. Station A uses 32 bytes packets to transmit mes-
having the IP address as 180.10.10.10/20. The num- sages to station B using a sliding window protocol.
bers of addresses in such a network will be _______ ? The round-trip delay between A and B is 60 ms
and the bottleneck bandwidth on the path between
(a) 255 (b) 4094 (c) 4096 (d) 1024
A and B is between 128 Kbps. What is the optimal
window size that A should use?
Set 2
(a) 20 (b) 30
1. Consider the following two cases given below, (c) 60 (d) 40
where G denotes the generator polynomial and M
denotes the received message: 8. Frames of 1000 bits are sent over a 106 bps duplex
link between the two hosts. The propagation time
Case 1: G = 1100, M = 1010101 is 34 ms. Frames are to be transmitted into this
Case 2: G = 11001, M = 111000111011 link to maximally pack them in transit (within the
In which transmissions errors occur? link). What is the minimum number of bits (l) that
(a) 1 only (b) 2 only will be required to represent the sequence of num-
(c) Both 1 and 2 (d) Neither 1 nor 2 bers distinctly? Assume that no time gap needs to
be given between transmissions of two frames.
2. A block of addresses is granted to a small
organisation. We have given a random address (a) l = 4 (b) l = 5
216.18.9.21/28. (c) l = 6 (d) l = 7
What is the first address in the block?
9. A 3-km long broadcast LAN has 107 bps bandwidth
(a) 216.18.9.0 (b) 216.18.9.16 and uses CSMA/CD. The signal travels along the
(c) 216.18.9.18 (d) 216.18.9.21 wire at 4 × 108 m/s. What is the minimum packet
size that can be used on this network?
3. How many total numbers of addresses are there in the
block using the information in the above question? (a) 50 bytes (b) 100 bytes
(c) 200 bytes (d) None of these
(a) 16 (b) 32 (c) 64 (d) 128
10. Match the following:
4. A channel has 10 Kbps bit rate using stop-and-wait
protocol and has propagation delay of 5 ms. For a (A) 204.26.37.47 i. Class A
frame with 360 bit, what will be the efficiency? (B) 145.12.13.0 ii. Class B
(C) 230.54.256.56 iii. Class C
(a) 92% (b) 88%
(D) 255.255.255.255 iv. Class D
(c) 84% (d) 78%
(E) 126.126.126.126 v. Class E
(F) 85.18.257.24 vi. Invalid IP
5. Consider a 3 Mbps token ring LAN and frame size
is of 200 byte. If the ring latency is 150 ms, then (a) A-iv B-ii C-iv D-v E-i F-i
what will be the effective data rate of the LAN? (b) A-iii B-ii C-vi D-v E-i F-vi
(c) A-iii B-ii C-iv D-iv E-vi F-i
(a) 2.10 Mbps (b) 2.25 Mbps
(d) A-iii B-ii C-iv D-vi E-i F-vi
(c) 2.35 Mbps (d) 2.90 Mbps
11. In a class C network if subnet mask is given as
6. Host A is sending data to host B over a full duplex
255.255.255.224. Calculate the number of subnet
link. A and B are using the sliding window protocol
and number of host in each subnet (practically
for flow control. The send and receive window sizes
possible).
are 6 packets each. Data packets (sent only from A
to B) are all 1000 bytes long and the transmission (a) 4 and 32 (b) 6 and 64
time for such a packet is 40 ms. Acknowledgement (c) 8 and 32 (d) 6 and 30
packets (sent only from B to A) are very small
and require negligible transmission time. The 12. We need to make a super network out of 16 class
propagation delay over the link is 180 ms. What C block, what is the supernet mask?
is the maximum achievable throughput in this (a) 255.255.255.240
communication? (b) 255.240.0.0
(a) 14.28 × 106 bps (b) 14.61 × 106 bps (c) 255.255.240.0
(c) 19.33 × 106 bps (d) 23.40 × 106 bps (d) 255.255.224.0
13. Given that bandwidth = 10 Mbps, distance = 4 km 20. If 6 bits are used for sequence number, then what
and velocity = 2 × 108 m/s. The number of bits is the sender window size and receiver window size
are transmitted in RTT is _______. in selective repeat scheme?
19. If N is a maximum sequence number in sliding 25. In a token ring network, the transmission speed is
window of Go-Back-N ARQ. How many sequence 32 bps and the propagation speed is 200 m/ms. The
bit will be there for use? 4-bit delay in this network is equivalent to:
(a) log2N (b) 1/log2N (a) 12 m of cable (b) 18 m of cable
(c) log2N2 (d) log2N + 1 (c) 22 m of cable (d) 25 m of cable
Set 1
1. (d) 8. (b) 15. (c) 22. (a) 29. (c) 36. (b) 43. (c) 50. (a)
2. (b) 9. (16) 16. (b) 23. (a) 30. (c) 37. (d) 44. (c) 51. (b)
3. (c) 10. (a) 17. (a) 24. (d) 31. (a) 38. (b) 45. (b) 52. (b)
4. (d) 11. (a) 18. (d) 25. (c) 32. (a) 39. (a) 46. (c) 53. (b)
5. (c) 12. (b) 19. (a) 26. (b) 33. (a) 40. (d) 47. (a)
6. (a) 13. (b) 20. (c) 27. (a) 34. (c) 41. (b) 48. (d)
7. (a) 14. (4) 21. (c) 28. (a) 35. (d) 42. (c) 49. (b)
Maximum number of subnets 19. (d) If sequence bits are N, then maximum sequence
= 27 − 2 = 128 − 2 = 126 number is = 2N− 1, so
Maximum number of hosts in each subnet Number of sequence bits = log2N + 1
= 216 − 7 − 2 = 29 − 2 = 510
20. (c) If sequence bits are n, then window size = 2n−1
16. (c) Generally, the number of addresses usable for
So, for 6-bit sequence number, window size = 26−1 =
addressing specific hosts in each network is always
25 = 32
2N − 2, where N is the number of bits for host id.
21. (b) Given that distance = 500 m and bandwidth =
The binary representation of subnet mask is 11111
10 Mbps
111.11111111.11111000.00000000.
Frame transmission time = 2 × Propagation delay
There are 21 bits set in subnet. So, 11 (i.e., 32 − 21)
Therefore,
bits are left for host ids.
Frame size Distance
Total possible values of host ids is 211 = 2048 . = 2×
Bandwidth Velocity
Out of 2048 values, 2 addresses are reserved.
Frame size 500 m
The address with all bits as 1 is reserved as broad- = 2×
cast address and address with all host id bits as 0 10 × 10 b/s
6
2 × 108 m/s
is used as network address of subnet. Frame size = 50 bits
17. (a) Given that bandwidth = 100 Mbps = 100 × 22. (a) Formula to calculate time = C + rS = MS
106 = 108 bits and propagation time = 100 ms where capacity (C) = 1.2 × 106, token generation
We know that rate (r) = 6 mb/s and output rate (M) = 8 × 106.
Putting values into formula, we get
RTT = 2 × PT = 200 ms
Data transfer in 1 s = 108 (1.2 × 106) + 6 × 106 × S = (8 × 106) S
106(1.2 + 6S) = (8 × 106) S
So, data transfer in 200 ms (in 1 RTT) = 200 × 10−6 ×
108 = 20000 bits 1.2
S= = 0. 6 s
Required RTT for transmitting 40000 bits = 2
40000/20000 = 2 23. (b) Packet size = (0000000000111111)2 = 63
18. (b) Sequence bits (n) = 5 Header size = HLEN × 4 = 1010 × 4 = (1010)2 ×
Maximum value of sequence number = 2n− 1 4 = 40
Packet size = Header + Payload
0 1 2 3 - 30 31 0 1 2 - 30 31 0 1 2 - 30 31 0 1 2 3 63 = 40 + Payload ⇒ Payload = 23
- - -
24. (c) Error message types
- - -
3→ destinations unreachable
- - -
4→ source quench
(0−30) = 31 frames 11→ time exceeded
12→ parameter problem
(31−29) = 31 frames
(30−28) = 31 frames 25. (d)
Total = 93 frames Given that R = 32 × 106 bps, B = 4, V = 200 m/ms;
d =?
Total frames left 6 and they will be counted as 29,
30, 31, 0, 1, 2. Bv 4 × 200
d= = = 25 m of cable
So, after 99th frame, sequence number will be 3. R 32 × 106 × 10−6
2
Number of questions
Marks 1
1 Marks 2
Total number of questions
0
2015 2014 2013 2012 2011 2010
Year Concept
2015 Unix Sockets, XML and HTML
2014 HTML Web page and http server
2013 Communication technology
2012 Nil
2011 HTML elements
2010 Client—server application
WEB TECHNOLOGIES
To communicate over the Internet, Web servers and 1980 Tim Berners-Lee invents the WWW, at
Web browsers act as client-server programs. Web data CERN (the world famous nuclear research
is distributed over Internet using these programs. HTML lab at Switzerland).
is a markup language that is used for presentation 1990 Concepts like - HTTP, Web browser,
and interaction with people in Web browsers. Web Uniform Resource Identifier (URI) and HTML
servers use IP address and port numbers for identifi- 1993 Launch of the first graphical web browser,
cation. Using Web browser, data request is sent using named - MOSAIC at USA.
HTTP protocol. Apache, Tomcat and IIS are popular
Web-server programs, whereas Internet Explorer (IE), 1994 Hosting of the first International WWW
Chrome, Safari, Opera and Firefox are popular Web Conference, formation of W3C
browsers. 1996 Commercialization of the Web
The World Wide Web (WWW), also termed as `web’, 1998 Google was founded by Larry Page and
is an information medium which is accessed through the Sergey Brin
Internet. The following table describes the major mile- 1999 Concept of Dot-com, its boom and bust.
stones in the development of the WWW (Table 11.1).
(Continued )
HTML stands for hypertext markup language, widely <body> Defines the document’s body
used for the development of Web pages. It is used to <h1> - <h6> Defines headings 1 to 6
display text, images and other objects in a specified <p> Defines a paragraph
way on the browser. Hypertext signifies the technique
of linking HTML documents together. The link available <br> Inserts a single line break
on a Web page is called hypertext. Markup language <center> Defines a centering content
signifies the marking of a text document with tags, which <hr> Defines a horizontal rule
are used to display on Web browsers. It was designed
<pre> Preserves formatting
in 1991 by Berners-Lee, and later, it was published in
1995 with standard specifications. The latest version <!--> Defines a comment
of HTML-5 was published in 2012. World Wide Web <samp> Defines a sample computer
Consortium (W3C) defines the specifications of HTML. code
HTML 4.0 was published in 1997 and then revised in
<var> Defines a variable
1998. HTML 4.01 was published in 1999.
HTML files are written in Notepad, a text editor on
Windows, with a file extension. html. The output can be
shown by opening it using a Web browser. 11.2.2 HTML Attributes
11.2.1 HTML Tags Attributes are extra bits of information in tags. These
are used to define the characteristics of an HTML
HTML tags are used to mark up HTML elements and are element. These show inside the opening tag and their
surrounded by two characters `<’ and `>’ known as angle values are always inside quotation marks and consist
brackets (see Table 11.2). HTML tags generally comes of two parts: name is the property and value is the
in pair such as <b> and </b>. The first tag in a pair value of the property to be set. For example, <body
is the opening tag and the second tag is the ending tag. bgcolor="blue">, bgcolor is the name and blue is
The text between the start and end tags is the element the value.
content. These are not case-sensitive, which means <b>
and <B> are the same.
11.2.2.1 Core Attributes
A typical HTML document has the following structure:
<html> There are four core attributes used in HTML: id, title,
class and style.
<head>
1. The id attribute can be used to uniquely identify
Document header related tags any element within an HTML page. For example,
<p id="html"> This paragraph explains the
</head> definition of HTML.
2. The title attribute is used for the title of the
<body> element. The syntax of title attribute is same as
that of the id attribute.
Document body related tags 3. The class attribute is used to associate an element
with a style sheet, and specifies the class of the
</body> element.
4. The style attribute is used to specify Cascading
</html> Style Sheet (CSS) rules within the element.
The background can be a colour or an image and speci- The HTML tables are used to arrange data into rows and
fied in the <body> tag. columns of cells. These are created using the <table> tag.
<tr> tag is used to create table rows and <td> tag 2. Method attribute is used to upload data.
is used to create data cells. <th> tag is used for table 3. Target defines the target window, where the result
heading. Cellpadding and cellspacing attributes are used of the script is displayed, and
to adjust the white space in the table cells. Cellpadding 4. Enctype defines the encoding of the data by the
defines the distance between cell borders and the con- browser.
tent within a cell and cellspacing defines the width of
the border. Colspan and rowspan attributes are used to 11.2.14 HTML Marquees
merge two or more columns into a single column and
row, respectively. For table backgrounds, bgcolor and HTML marquee is a scrolling piece of text displayed on
background attributes are used. The height and width of the Web page. The <marquee> tag is used for this.
the table can be changed with the height and width attri-
butes, respectively. Caption tag is used for the caption of
the table. <thead>, <tbody> and <tfoot> are used for 11.3 CASCADING STYLE SHEETS
creating portions of HTML table into header, body and
footer, respectively.
Cascading Style Sheets (CSS) is another method of styl-
ing content. It is a style sheet language which describes
11.2.12 HTML Frames the appearance and format of the document written in
any markup language. Generally, it is used to modify
To divide the browser window into multiple sections, the style of web pages and user interfaces written in
HTML frames are used. A separate HTML document HTML and XHTML, plain XML, XUL, etc. Along
can be loaded in each section. A frameset is simply a with HTML and JavaScript, the CSS is a technology
collection of such kind of frames in a browser window. used by most websites to create attractive webpages,
<frameset> tag is used to create frames in the browser visually appealing user interfaces for web and mobile
window. The rows attribute is used for horizontal frames applications.
and cols attribute is used for vertical frames. The value of CSS is designed primarily to distinguish document con-
rows and cols is represented in percentage, for example, tent from document presentation, using elements such as
<frameset cols="25%,50%,25%">. the layout, colours, and fonts.
Each frame is indicated by the <frame> tag. The
<frame> tag has mainly two attributes: name and src.
For example, <frame name="top" src="/html/ 11.4 XML
top_frame.htm"/>.
Other attributes of a frame are border, frameborder XML stands for eXtensible Markup Language. It is
and framespacing. Border defines the width of the border an open standard to share data and information
of each frame in pixels. Frameborder takes value either 1 between computers and computer programs. It is a
(yes) or 0 (no) for 3D border display. The framespacing meta-markup language, which defines the syntax to
defines the amount of space between frames in a frameset. define other semantic, domain-specific and structured
The <frame> tag has the following attributes: src, markup languages. It also describes the structure and
name, frameborder, marginwidth, marginheight, nore- meaning of a document. It is a set of rules for defining
size, scrolling and longdesc. semantic tags. XML documents always have a single
The <iframe> tag is used to define a rectangular
root element. The element names are case-sensitive,
always closed and correctly nested. Attributes of ele-
region, that can display another document having scroll-
ment are always quoted. The default entities defined
bars and borders.
are <, >, &, “, and ’. An XML document has a tree-like
structure.
11.2.13 HTML Forms
Syntax for an XML:
These are used to collect data from the site visitor. There
<Element Name>
are various form elements: text fields, drop-down menus,
combo-box, radio buttons, checkboxes, etc.
<Element Name>Content</Element Name>
The <form> tag has the following attributes: action,
method, target and enctype. <Element Name>Content</Element Name>
1. Action attribute is used for backend script, which
<Empty Element Name/>
is ready to process passed data.
1. Defining your own elements 11.4.3 Tag Rules for XML Documents
2. Better organized documents
3. Sorting of database An XML document is a collection of XML elements. An
XML element starts from (including) the element’s start
11.4.2 Document-Type Definition tag and ends at (including) the element’s end tag. The
tags in XML should follow the following restrictions:
Document-type definition (DTD) defines the tags in 1. XML tags cannot overlap.
the document; the tags contain other tags, the number 2. Tags should be closed.
and sequence of the tags, the attributes of tags and the 3. A tag that does not contain any text can contain
values of those attributes. the end marker at the end of the start tag.
11.4.4 Types of XML Documents and methods for accessing documents. It has three parts:
Core DOM, XML DOM and HTML DOM.
There are two ways to create an XML document:
1. Well formed: These documents follow the XML 11.4.7 XUL
tag rules and do not follow the DTD. Document
has a top-level element, all elements must have a Developed by Netscape and Mozilla, It stands for eXten-
starting and an ending tag, element names should sible User interface Language and pronounced as “Zool”.
be case-sensitive and elements must be nested It is a series of XML tags, which allows different oper-
properly. ating platforms to exchange data or program. It sup-
2. Valid: A valid document must have followed the ports Cascading Style Sheets, JavaScript, RDF, DOM
rules by DTD and should be well formed. and HTML.
The comparison of HTML and XML is given in
Table 11.6. 11.4.8 Flash and Silverlight
Table 11.6 | Comparison of HTML and XML Developed by Adobe Systems, Flash is a Rich Internet
Application (RIA), used for playing audio and video in
Attributes HTML XML
webpages. It is basically used for providing animation,
Internal Definition Yes Yes interactivity to games, advertisements, etc. It is run on
External Definitions No Yes Microsoft Windows, Mac OS, QNX, Google TV and
RIM.
Defined Tags Yes No
Supported by a subset of. NET Framework, Silverlight
Add New Tags No Yes
is also a Rich Internet Application (RIA). In this, user
Easily Add Objects Yes Yes interfaces are declared in eXtensible Application Markup
Easily Add Entities Yes Yes Language (XAML). It supports Windows Media Video
(WMV), Windows Media Audio (WMA), MPEG layer
Note: XML and HTML were designed with different III (MP3).
goals:
1. XML was designed to describe data and to focus 11.4.9 User-Interface Language
on what data is.
2. HTML was designed to display data and to focus These are used for graphical user interfaces and control.
on how data looks. The objective of these interfaces is to use programs and
script codes in the form of markup. Some of user inter-
face markup languages are:
11.4.5 XHTML
1. XUL (eXtensible User-interface Language) devel-
It stands for eXtensible HyperText Markup Language. It oped by Mozilla Foundation.
was developed by W3C for the transition from HTML to
2. QML (Qt Meta Language) developed by Nokia,
XML. It is similar to HTML4.01, but it gives a consis-
used for mobile applications such as touch input,
tent and well-organised format. It combines the features
fluid animation, etc. It is based on JavaScript
of both HTML and XML.
declarative language.
The XHTML document use lower case for all HTML 3. UMIL (User Interface Markup Language) used to
elements and attributes. Certain tags in HTML are define user interfaces for computers. It is an XML
non-pair tags but XHTML requires end tags. All values based language.
must be quoted. XHTML define three DTD: Strict,
4. UsiXML (User Interface XML) is a complaint
Transitional and Frameset. XHTML 1.1 is a module
markup language. It describes the user interface
based XHTML, which supports Ruby Annotation ele-
for multiple contexts of use.
ments for the better understanding of East-Asian lan-
guages. It is compatible with HTML 4.0. 5. XAL (eXtensible Application Language) developed
by Nexaweb’s Enterprise Web 2.0 suite, used for
applications like Java client and Ajax Client.
11.4.6 Document Object Model (DOM)
Other user-interface languages are as follows: MXML,
It is a World Wide Web Consortium (W3C) standard, OpenLaszlo, ZUML, JavaFX, jInterface, Thinlet,
used to access documents such as HTML and XML. It Vexi, XHTML, XFDL, Xforms, XAML, XRC, EMML,
is a programming API. It defines the objects, properties GladeXML, SVG, etc.
11.5 BASIC CONCEPTS OF 6. Fax server: It manages sending and receiving
CLIENT— SERVER COMPUTING of fax.
7. Database server: It manages databases and
responds to clients through SQL queries. It shares
Client-server consists of two parts: server, which pro- data in a database. Example: Oracle9i database
vides services and client, which request services from the server.
server (Fig. 11.1). The server in the network provides 8. Transaction servers: It manages data and
services to more than one client. There are different serv- remote procedures. It shares data and high-level
ers for different applications, such as file server, print processing, such as OnLine Transaction Processing
server, mail server, etc. So, a client can request services (OLTP), across a network. Example: Microsoft
from several servers on the network. Client and server SQL Server, BEA Tuxedo, etc.
can be on the same computer or on different computers
linked by a network. 11.5.2 Stateless and Stateful Servers
Advantages
1. Client requests services directly from the server.
Figure 11.2 | Single client single server.
2. It is less complicated for implementation.
3. It provides attractive graphical user interface
2. Multiple clients single server: In this topology, applications.
multiple clients are directly connected to one server 4. It has persistent connection between the client and
(Fig. 11.3). Example: Different nodes connected to the server.
a server in a lab. 5. It can be easily managed.
Disadvantages
1. Maintenance cost of application at client is high.
2. It increases load in the network.
3. Several PCs are required for individual applications.
4. Software distribution procedure is complicated in
two-tier architecture.
11.5.7.2 Three-Tier Architecture
Figure 11.3 | Multiple clients single server. In this, business logic is located in the middle tier.
Client’s request is also handled by the middle tier. Only
3. Multiple clients multiple servers: In this middle tier has to change if business rules are changed.
topology, multiple clients are connected to multiple The driver translates the request into a network protocol
servers (Fig. 11.4). Example: People accessing and makes a request via the proxy server. Application
facebook, Google and other applications. responsibilities in the three-tier architecture are of the
following three types:
1. Presentation (GUI) or user services: It main-
tains the graphical user interface and generates
output for the monitor. Presentation logic deals
with screen formatting, windows management,
input editing and what-if analysis.
2. Application services or business rules:
It executes applications and controls the flow of
the program. Business logic dealing with domain
and range validation, data dependency validation
and request/response architecture of inter-process
communication level.
3. Database services or data server: It manages
the databases. Server logic deals with data access,
Figure 11.4 | Multiple clients multiple servers. data management, data security and SQL parsing.
8. JAXP (Java API for XML Parsing): It pro- services. Enterprise bean instances runs in an EJB
vides support for the industry standard SAX and container.
DOM APIs for parsing XML documents. It also The container provides the following services to enter-
supports XSLT transform engines. prise beans:
9. RMI-IIOP: It is a sub-system composed of APIs
to use RMI-style programming. It supports both 1. Transaction Management: The programmer
the J2SE native RMI protocol (JRMP) and the need not to code for the transactions, rather, he
CORBA IIOP protocol. has to declare the enterprise bean’s transactional
properties in the deployment descriptor file. Then,
the container reads the file and handles the enter-
11.6.2 J2EE Architecture
prise bean’s transaction.
2. Security: Only authorized clients can invoke an
The Java2 SDK is an Enterprise Edition known as J2EE
enterprise bean’s methods. Client has a particular
SDK. It is the reference implementation provided by Sun
role and each role is permitted to invoke certain
Microsystems, Inc. Figure 11.5 shows the major elements
methods.
of the J2EE architecture.
3. Remote Client Connectivity: It manages
Tier 1 Tier 2 Tier 3 the low level communication between clients and
enterprise beans.
J2EE Server 4. Life Cycle Management: It maintains all the
EJB Container states from the creation to the removal of enter-
Enterprise prise beans.
Bean 5. Database Connection Pooling: The container
manages the pool of database connections. The
Enterprise enterprise bean obtains a connection from the pool.
Bean
These database connections can be re-used.
Enterprise 6. Web Container: It is a runtime environment for
Bean JSP files and servlets.
IMPORTANT FORMULAS
Important Abbreviations
SOLVED EXAMPLES
3. A digital signature is used to provide security Solution: <h1> will provide largest heading,
makes use of then <h2>, then <h3> and so on. <h6> gives the
(a) Digitally scanned signatures. smallest heading.
(b) A unique ASCII code number of the sender. Ans. (b)
(c) Private key encryption. 8. The following HTML tag is used for inserting a line
(d) Public key encryption. break?
Solution: Digital signature is an a symmetric (a) <br/> (b) <startbreak/>
cryptography technique which uses public key (c) <lb/> (d) <LINEBRK/>
encryption mechanism to provide integerity,
authentication and non-repudiation. Solution: <br/> tag is used for insert-
Ans. (d) ing a line break. There is no tag in html with
names<startbreak>, <lb/> and <LINEBRK>.
4. The <b> tag makes the enclosed text bold. The Ans. (a)
other alternative is to use
9. The correct syntax for adding yellow as a back-
(a) <strong> (b) <dark> ground colour in HTML is
(c) <big> (d) <highblack>
(a) <body style="background-color:yellow">
Solution: (b) <backgroundcolor>yellow</background
<strong> tag is used to define important text by color>
bold the text. (c) <color.background="yellow">
<dark> – there is no tag in html named dark. (d) <backgrndcolor="yellow">
<big> tag is used to make text bigger than the
normal text. Solution: Syntax for background color is:
<highblack> – there is no tag in html named 1. <body bgcolor="color_name|hex_number|
highblack. rgb_number">
Ans. (a) 2. <body style="background-color:color_
5. What does HTML stand for? name">
(a) High tool and text markup language Any of these two can be used.
(b) Heavy tool markup language Ans. (a)
(c) Hypertext markup language 10. The HTML tag used to make a text bold is
(d) Hypertext my language
(a) <b> (b) <heavybold>
Solution: HTML is a markup language which is (c) <blackbold> (d) <bld>
used to design web pages. It stands for Hypertext
Markup Language. Solution: <b> tag is used in HTML to make text
Ans. (a) bold.
Ans. (a)
6. Who describes and controls the making of various
Web standards? 11. The HTML tag used to make a text italic is
(a) The World Wide Web Consortium (a) <italic> (b) <i>
(b) IEEE and IETE (c) <textitalic> (d) <slantingtext>
(c) Administrator and Mozilla
(d) IEEE Solution: <i> tag is used in HTML to make text
italic. There is no tag in html with names<italic>,
Solution: The World Wide Web Consortium <textitalic> and <slantingtext>.
(W3C) is the international standard organisation Ans. (b)
for managing and controlling web standard. IEEE
12. The HTML tag used to create a hyperlink is
and IETE are professional association societies for
advancement of technology. (a) <ahref="http://www.myexams.com">
Ans. (a) MYEXAMS</a>
7. Which of the following HTML tag will provide the (b) <aurl="https://www.myexams.com">
largest heading myexams</a>
(c) <anamelink="http://www.myexams.com">
(a) <h6> (b) <h1> myexams.com</a>
(c) <head8> (d) <heading6> (d) <a>http://www.MYEXAMS.COM</a>
Solution: <a href =” URL addrss” hyperlink office systems (SGML) for text information
name </a> tag is used to create hyperlink in processing.
HTML. (a) ISO - 8878 (b) ISO - 8879
Ans. (a) (c) ISO - 8880 (d) ISO - 8881
13. Linking to another place in the same or another
Web page require two A (Anchor) tags, the first Solution: SGML is an ISO standard (ISO –
with the attribute and the second with 8879) for defining markup languages.
the attribute. Ans. (b)
(a) NAME, LINK 15. The facilities available in the Internet are
(b) LINK, HREF A. Electronic mail.
(c) HREF, NAME B. Remote login.
(d) TARGET, VALUE C. File transfer.
D. Encryption technique.
Solution: <a href =” addrss”> hyperlink
name </a> tag is used to create hyperlink in (a) A and B (b) A, B and D
HTML. (c) A, B and C (d) B, C and D
Ans. (c)
Solution: Internet provides services like e-mails,
14. HTML is defined using SGML - an file transfer and remote login; but it does not pro-
standard, information processing-text and vide encryption and decryption techniques.
Ans. (c)
1. Consider the HTML table definition given below: 3. HTML (HyperText Markup Language) has
<table border=1> language elements, which permit certain actions
<tr><td rowsp an=2>ab</td> other than describing the structure of the Web
document. Which one of the following actions
<tdcolspan=2> cd </td></tr>
is NOT supported by pure HTML (without any
<tr><td>ef</td><td rowspan=2>gh</td>
server or client-side scripting) pages?
</tr><tr><td colspan=2>ik</td></tr>
</table>
(a) E mbed Web objects from different sites into
The number of rows in each column and the the same page.
number of columns in each row are (b) Refresh the page automatically after a specified
(a) 2, 2, 3 and 2, 3, 2 (b) 2, 2, 3 and 2, 2, 3 interval.
(c) 2, 3, 2 and 2, 3, 2 (d) 2, 3, 2 and 2, 2, 3 (c) Automatically redirect to another page upon
(GATE 2009: 1 Mark) download.
(d) Display the client time as part of the page.
Solution: Two td commands are used in the first (GATE 2011: 1 Mark)
tr command and three td commands are used in
second tr command. So, it requires (2, 3, 2) rows
Solution: <OBJECT> … </OBJECT> tag is used to
in each column.
embed web objects.
Ans. (c)
<META HTTP-EQUIV="Refresh" CONTENT="5">
2. Which one of the following is not a client-server is used to refresh page after every 5 seconds.
application?
<META HTTP-EQUIV="Refresh" CONTENT="0;
(a) Internet chat (b) Web browsing URL=another-page.html"> is used to redirect.
(c) E-mail (d) Ping But for displaying the client time, there is no tag
(GATE 2010: 1 Mark) available.
Solution: Internet chat, Web browsing and email Ans. (d)
are client-server applications. Ping is a connectiv-
ity checking of the network. 4. Match the problem domains in Group I with the
Ans. (d) solution technologies in Group II.
PRACTICE EXERCISES
(c) It specifies the marking of the Web page. 17. How can you create an email link?
(d) It specifies formatting and layout instructions
(a) <ahref="mailto:xxx@yyy">
of the Web page.
(b) <ahref="[email protected]">
9. HTML is a subset of (c) <mailhref="[email protected]">
(d) <mail>[email protected]</mail>
(a) SGML (b) SGHT
(c) STML (d) SHML 18. How can you open a link in a new browser window?
10. The format of an HTML document is (a) <ahref="url" target="newblank">
(b) <ahref="url" target="_blank">
(a) ASCII (b) Special JPEG
(c) <ahref="url" target="subwindow">
(c) Special TIFF (d) Special Web
(d) <
ahref="url" target="nextwindow">
11. The advantages of XML over HTML are:
19. What does CSS stand for?
A. It allows processing of data stored in Web
(a) Computer secure scheme.
pages.
(b) Computer securesheets.
B. It uses meaningful tags which aids in under-
(c) Cascading style sheets.
standing the nature of a document.
(d) Cascading secure sheets.
C. Is simpler than HTML.
D. It separates presentation and structure of 20. What is the correct HTML for referring to an
document. external style sheet?
(a) A, B and C only (b) A, C and D only (a) <link rel="stylesheet" type="text/
(c) A, B and D only (d) B and D only css" href="mystyle.css">
12. XML uses (b) <style src="mystyle.css">
(c) <stylesheet>mystyle.css</stylesheet>
(a) user-defined tags. (b) pre-defined tags. (d) None of these
(c) extensible tags. (d) pairing tags.
21. An external style sheet in an HTML document is
13. A URL specifies the following: placed
A. Protocol used (a) in the <body> section.
B. Domain name of server hosting Web page (b) in the <linker> section.
C. Name of folder with required information (c) in the <head> section.
D. Name of document formatted using HTML (d) in the <top> section.
(a) A, B, C and D (b) B, C and D 22. The HTML tag used to define an internal style
(c) B, C and D (d) A, C and sheet is
14. The World Wide Web supports (a) <style> (b) <stylesheet>
A. Encryption. (c) <css> (d) <internal link>
B. HTML. 23. Which of the following HTML attribute defines
C. HTTP. inline styles?
D. Firewall.
(a) style (b) inlinefont
(a) A, B and C (b) B and C (c) objectdefine (d) inlinestyles
(c) A, C and D (d) A, B, C and D
24. The correct CSS statement is
15. The time taken by Internet packets
(a) {color=black(body}
(a) can be predetermined before transmission. (b) body {color:black}
(b) depends upon the size of the packet. (c) body:color:=black
(c) is irrelevant for audio packets. (d) {body(color:black)}
(d) is irrelevant for video packets.
25. Which of the following inserts a comment in a CSS
16. The DNS maps the IP addresses to file?
(a) a binary address as strings. (a) // this is a comment
(b) an alphanumeric address. (b) /* this is a comment */
(c) a hierarchy of domain names. (c) * this is a comment*
(d) a hexadecimal address. (d) “this is a comment”
26. How can we change the background colour? 34. Which of the following statements is used for creat-
ing a function?
(a) bgcolor:"newcolor"
(b) color=background (colorname) (a) function=Functionname()
(c) background-color: (b) function Functionname()
(d) BGCOL="colorname" (c) function:Functionname()
(d) function "Functionname"
27. To add a background color for all <h1> elements,
which of the following HTML syntax is used? 35. How do you call a function named “myFunction”?
(a) h1 {background-color:#FFFFFF} (a) myFunction()
(b) {background-color:#FFFFFF}.h1 (b) call function myFunction
(c) {background-color:#FFFFFF}.h1(all) (c) callmyFunction()
(d) h1.all{bgcolor= #FFFFFF} (d) callfunction.myFunction
28. To change the text colour in HTML we use 36. The correct syntax to define “if” statement for
executing some code if “x” is equals to 2?
(a) color: (b) latest—text—color=
(c) modifytextcolor: (d) newcolor: (a) if(x=2) then (b) ifx==2.0 then
(c) if(x==2) (d) if(x:=2)
29. JavaScript is defined under which HTML
element? 37. The correct syntax for a conditional statement to
execute following code if “x” is NOT equal to 8?
(a) <jscript> (b) <script>
(c) <scriptjava> (d) <define.js> (a) if x =! 8 then (b) if (x<> 8)
(c) if x<>8 then (d) if (x != 8)
30. The correct syntax to write “Hi There” in
Javascript is 38. What does XML stand for?
4. Which of the following is used to specify border 6. Which of the following is TRUE in context of an
type (solid or dotted or double line etc.) XML language
(a) Every XML document must have a DTD.
(a) Border-style
(b) There is no difference in lower case and upper
(b) Border-layout
case tags.
(c) Border-attrib
(c) All documents must have a root element.
(d) Border-width
(d) Closing tag is not mandatory.
7. In an HTML document, the correct place to refer
5. To declare the version of XML, the correct syntax is.
to an external style sheet is
(a) <?xml version=’1.0’/> (a) In the <body> section.
(b) <*xml version=’1.0’/> (b) In <external> tag.
(c) <?xml version="1.0"/> (c) At the top of the document.
(d) </xml version=’1.0’/> (d) In the <head> section.
1. Consider the statement: “Not all that glitters is Solution: Using topological sort, strongly con-
gold” Predicate glitters (x) is true if x glitters and nected components can be found. According to
predicate gold (x) is true if x is gold. Which one of the property, if (u,v) belongs to E2, (v,u) will not
the following logical formulae represents the above belong to E.
statement? Ans. (b)
(a) ∀x; glitters(x) ⇒ ¬gold(x) 4. Consider the following system of equations:
(b) ∀x; gold(x) ⇒ glitters(x) 3x + 2y = 1
(c) ∃x; gold(x) ∧ ¬glitters(x) 4x + 7z = 1
(d) ∃x; glitters(x) ∧¬ gold(x) x + y + z = 3
Solution: S: “Not all that glitters is gold” means x − 2y + 7z = 0
there exist some glitters that are not gold. So, ∃x is The number of solutions for this system is .
used along with AND operator.
∃x; glitters(x) ∧ ∼gold(x) Solution: The given equations are
3x + 2y = 1
Ans. (d)
4x + 7z = 1
2. Suppose you break a stick of unit length at a point x + y + z = 3
chosen uniformly at random. Then the expected x − 2y + 7z = 0
length of the shorter stick is . 3 2 0 1
4 0 7 1
Solution: The length of shorter stick ranges Augmented matrix r(A : B ) = 1 1 3
between 0 and 0.5 meters. So, the expected length 1
is 0.25. 1 −2 7 0
Ans. (0.25)
Applying row operations, the resultant matrix
3. Let G = (V,E) be a directed graph where V is the will be:
set of vertices and E the set of edges. Then which
one of the following graphs has the same strongly 1 1 1 3
0 −4 3 −11
connected components as G? 0 0 −15 −21
(a) G1 = (V,E1) where E1 = {(u,v) (u,v) ∉ E)}
0 0 0 0
(b) G2 = (V,E2) where E2 = {(u,v) (v,u) ∉ E)}
(c) G3 = (V,E3)[ where E3 = {(u,v) there is a ρ(A) = 3 = number of variables. So equations
path of length ≤ 2 from u to v in E} have a unique solution.
(d) G4 = (V,E4) where V4 is the set of vertices in Ans. (1)
G which are not isolated
5. The value of dot product of the eigenvectors cor- Applying property [A + BC = (A + B)(A + C)],
responding to any pair of different eigenvalues we have
of a 4-by-4 symmetric positive definite matrix is
PQ + P Q (R+ R )(R + S)
.
⇒ PQ + P Q(R + S)
Solution: The Eigen for symmetric positive matrix ⇒ PQ + P Q R + P QS
⇒ Q(P + P R) + P QS
corresponding to different Eigen values are orthog-
onal. Hence, their dot product is zero always.
Ans. (0) ⇒ Q(P + R) + P QS
6. Let the function ⇒ QP + QR + P QS
sin q cos q tan q ⇒ Q(P + P )(P + S) + QR
f(q ) = sin(p /6) cos(p /6) tan(p /6) ⇒ PQ + QS + QR
sin(p /3) cos(p /3) tan(p /3)
Ans. (a)
p p
where q ∈ , and f ′(q ) denote the derivative 8. The base (or radix) of the number system such
6 3 that the following equation holds is .
of f with respect to θ. Which of the following state-
ments is/are TRUE? Solution: (312/20)x = (13.1)x
p p
(I) There exists q ∈ , such that f ′(q ) = 0
6 3 3x2 + x + 2
=
x2 + 3x + 1
⇒ x2 − 5x = 0 ⇒ x = 0, 5
p p 2x x
(II) There exists q ∈ 3,x2 +such ′(q3)x≠+01
x + 2thatx2f+
6 3 = ⇒ x2 − 5x = 0 ⇒ x = 0, 5
2x x
(a) I only (b) II only
Base of number system cannot be 0. So, answer
(c) Both I and II (d) Neither I nor II is 5.
Ans. (5)
Solution: By applying mean value theorem, both
I and II found to be true. 9. A machine has a 32-bit architecture, with 1-word
Ans. (c) long instructions. It has 64 registers, each of which
is 32 bits long. It needs to support 45 instructions,
7. Consider the following Boolean expression for F:
which have an immediate operand in addition to
F(P, Q, R, S) = PQ + PQR + PQRS two register operands. Assuming that the immedi-
ate operand is an unsigned integer, the maximum
value of the immediate operand is .
The minimal sum-of products form of F is
(a) PQ + QR + QS (b) P + Q + R + S
Solution: Given that instruction size = 32 bits;
(c) P + Q + R + S (d) PR + PRS + P total instructions = 45, so opcode requires 6 bits;
Total registers = 64, so 6 bits for register operands
Solution: Immediate operand = 32 − 6 − 12 = 14
PQ + P QR + P Q R S So, the maximum value of the immediate operand
will be 16383
PQ + P Q(R + R S)
32 bits
Ans. (16383)
10. Consider the following program in C language: (a) T he graph does not have any topological
ordering
#include <stdio.h>
(b) Both PQRS and SRQP are topological orderings
main() (c) Both PSRQ and SPRQ are topological orderings.
{ (d) PSRQ is the only topological ordering.
int i;
Solution: Topological order of a directed graph is
int *pi=&i; the linear arrangement of vertices in which if there
scanf(“%d”, pi); is an edge between u to v, then u comes before v
printf(“%d \n”, i+5); in order. Option (c) satisfies this property, so both
} PSRQ and SPRQ are topological orderings.
Ans. (c)
Which one of the following statements is TRUE?
14. Let P be a quick sort program to sort numbers
(a) Compilation fails. in ascending order using the first element as the
(b) Execution results in a run-time error. pivot. Let t1 and t2 be the number of compari-
(c) On execution, the value printed is 5 more than sons made by P for the inputs [1 2 3 4 5] and [4
the address of variable i. 1 5 3 2], respectively. Which one of the following
(d) On execution, the value printed is 5 more than holds?
the integer value entered.
(a) t1 = 5 (b) t1 < t2
Solution: In the program, pi is a pointer variable
(c) t1 > t2 (d) t1 = t2
that contains the address of i. scanf gets the value
at address of i and printf prints value 5 more than i. Solution: Worst case of quick sort is when the
Ans. (d) list is already sorted. It requires n2 comparisons.
11. Let G be a graph with n vertices and m edges. Whereas an unsorted list requires nlogn compari-
What is the tightest upper bound on the running sons. List 1 is sorted, so the number of comparisons
time of Depth First Search on G, when G is repre- are more for list 1. Therefore, (c) is the correct
sented as an adjacency matrix? option (t1 > t2).
Ans. (c)
(a) Θ(n) (b) Θ(n + m)
15. Which one of the following is TRUE?
2
(c) Θ(n ) (d) Θ(m2)
(a) The language L = {an b n | n ≥ 0} is regular.
Solution: Tightest upper bound for running
(b) The language L = {an | n is prime} is regular.
depth first search using adjacency matrix is O(V2).
Number of vertices here are n. So, O(n2) is the (c) The language L = {w | w has 3k + 1b’s for some k Î N with å =
correct option. L = {w | w has 3k + 1b’s for some k Î N with å = {a,b}} is regular.
Ans. (c) (d) The language L = {ww | w ∈ ∑* with ∑ = {0,1}}
12. Consider rooted n node binary tree represented is regular.
using pointers. The best upper bound on the time Solution:
required to determine the number of sub trees (a) False. The language is not regular, because it
having exactly 4 nodes is Θ(nalogbn). Then the requires memory for remembering number of
value of a + 10b is a’s and b’s.
Solution: On taking input, the root of the binary (b) False. This is a recursive language.
tree prints all the subtrees of size 4 in O(n) time, (c) True. The language is regular because DFA
so a = 1, b = 0, and thus a + 10b = 1. construction is possible.
Ans. (1) (d) False. It is a context sensitive language.
Ans. (c)
13. Consider the directed graph given below.
16. Consider the finite automaton in the following
P Q figure.
0,1 1
R S 1 0,1 0,1
q0 q1 q2 q3
Which one of the following is TRUE?
What is the set of reachable states for the input scheduling the disk access, the request for cylinder
string 0011? 90 is serviced after servicing number
(a) {q1, q2, q3} (b) {q0, q1} of requests.
(c) {q0, q1, q2, q3} (d) {q3}
Solution: 30, 85, 90, 100, 105, 110, 135 and 145
Solution: With string `0011’, we can reach {q0, will be sort according to SSTF:
q1, q2} states. 100 →105 → 110 → 90 → 85 → 135 → 145 → 30
Ans. (a) 90 will be served after 3 requests.
Ans. (3)
17. Which one of the following is FALSE?
(a) A basic block is a sequence of instructions 20. Which one of the following is FALSE?
where control enters the sequence at the begin- (a) U ser level threads are not scheduled by the
ning and exits at the end. kernel.
(b) Available expression analysis can be used for (b) When a user level thread is blocked, all other
common subexpression elimination. threads of its process are blocked.
(c) Live variable analysis can be used for dead code (c) Context switching between user level threads
elimination. is faster than context switching between kernel
(d) x = 4 × 5 ⇒ x = 20 is an example of common level threads.
subexpression elimination. (d) Kernel level threads cannot share the code
Solution: x = 4 × 5 = 20 is not a common subex- segment.
pression elimination.
Solution: Code, text and data are sharable in kernel
Ans. (d)
threads. Only stack and registers are non-sharable.
18. Match the following Ans. (d)
(a) S1 is TRUE and S2 is a FALSE 24. Which one of the following are used to generate a
(b) Both S1 and S2 are TRUE message digest by the network security protocols?
(c) S1 is FALSE and S2 is a TRUE
(P) RSA (Q) SHA-1 (R) DES (S) MD5
(d) Both S1 and S2 are FALSE
(a) P and R only (b) Q and R only
Solution: S1: Employee (Name, Dept_ID)
Department (Dept_Name, Deptid) (c) Q and S only (d) R and S only
In the given relation, Employee DeptID is a Solution: Only SHA-1 and MD5 are used as mes-
foreign key referencing Deptid (P.K) of relation sage digest.
Department. Ans. (c)
Now, declare the foreign key by an equivalent
check assertion as follows:- 25. Identify the correct order in which the following
actions take place in an interaction between a web
CREATE TABLE Employee (
browser and a web server.
Name Varchar (10)
1. The web browser requests a webpage using
DeptID INT (6) check (DeptID IN (select Deptid HTTP.
from Department)), 2. The web browser establishes a TCP connection
PRIMARY KEY (Name)); with the web server.
3. The web server sends the requested webpage
The above use of check assertion is good to declare using HTTP.
the foreign key as far as insertion is considered for 4. The web browser resolves the domain name
relation Employee. But it will fail to implement using DNS.
changes done in Department relation in terms of
deletion & updation. So, S1 is false. (a) 4, 2, 1, 3 (b) 1, 2, 3, 4
S2: The given table definition is not valid due (c) 4, 1, 2, 3 (d) 2, 4, 1, 3
to invalid foreign key declaration. Attribute a is
Solution: First of all, browser resolves IP address
declared as foreign key which is a single valued
using DNS server, then connects to web server using
attribute and it is referencing the primary key
TCP connection. After connection establishment,
(a b) of relation R (a, b, c), which is a composite
browser request for the page and web server responds
key. A single value attribute cannot refer a com-
with the page. So, option (a) is correct.
posite key. So, S2 is false.
Ans. (a)
23. Consider the following three statements about link
state and distance vector routing protocols, for a
large network with 500 network nodes and 4000 Q. No. 26 — 55 Carry Two Marks Each
links
[S1] The computational overhead in link state pro- 26. Consider a token ring network with a length of 2 km
tocols is higher than in distance vector protocols. having 10 stations including a monitoring station.
[S2] A distance vector protocol (with split horizon) The propagation speed of the signal is 2 × 108 m/s
avoids persistent routing loops, but not a link state and the token transmission time is ignored. If each
protocol. station is allowed to hold the token for 2 msec,
[S3] After a topology change, a link state protocol the minimum time for which the monitoring sta-
will converge faster than a distance vector protocol. tion should wait (in msec) before assuming that the
token is lost in .
Which one of the following is correct about S1, S2,
and S3? Solution: Length = 2 km, stations = 10, signal speed
(a) S1, S2, and S3 are all true = 2 × 108 m/s and token hold time = 2 ms
(b) S1, S2, and S3 are all false.
(c) S1 and S2 are true, but S3 is false Length 2000 m
(d) S1 and S3 are true, but S2 is false. RTT = = = 10 µs
Speed 2 × 108 m/s
Solution: A distance vector protocol (with split
Minimum wait time = RTT + Stations × Hold time
horizon) avoids persistent routing loops, but not a
link state protocol. So, S2 is false.
Ans. (d) Minimum wait time = 10 + 10 × 2 = 30 ms
27. Let the size of congestion window of a TCP con- 29. Consider the following four schedules due to three
nection be 32 KB when a timeout occurs. The transactions (indicted by the subscript) using read
round trip time of the connection is 100 msec and and write on a data item x, denoted r (x) and
the maximum segment size used is 2 KB. The time w (x) respectively. Which one of them is conflict
taken (in msec) by the TCP connection to get back serializable?
to 32 KB congestion window in .
(a) r1(x); r2(x); w1(x); r3(x); w2(x)
Solution: Given that RTT = 100 ms, congestion (b) r2(x); r1(x); w2(x); r3(x); w1(x)
window size = 32 KB (c) r3(x); r2(x); r1(x); w2(x); w1(x)
(d) r2(x); w2(x); r3(x); r1(x); w1(x)
Congestion window 32
Time out = = = 16 KB
2 2 Solution To test conflict stabilizability, make
precedence graph. If there is a cycle between any
and MSS = 2 KB. Now,
two nodes, then the given schedule is not conflict
2 KB → 1 RTT (Slow Start) serializable.
4 KB → 2 RTT
8 KB → 3 RTT
(a)
T1 T2 Cycle is there
16 KB → 4 RTT (Threshold)
T1 T2 Cycle
So, notisconflict
there
After threshold point is reached, there begins an T
T11 T
T22 Cycle is there
serializable
So, not
Cycle is conflict
there
additive increase phase. T3 So,
So, not conflict
serializable
not conflict
T3
18 KB → 5 RTT T
serializable
serializable
T33
20 KB → 6 RTT (b) T
1 T2
22 KB → 7 RTT T1 T2 Cycle is there
T
T11 T
T22 Cycle is there
˙ T3 Cycle
Cycle is
is there
there
˙
30 KB → 11 RTT T3
T
T33
32 KB T1 T2 Cycle is there
(c) Cycle is there
Therefore, total time = 11 RTTs T1 T2
= 11 × 10 T
T11 T
T22
Cycle
Cycle is
is there
there
= 1100 ms T3
Ans. (1100) T3
T
T33
28. Consider a selective repeat sliding window protocol T1 T2 No cycle, so, it is a
T T2 No cycle,serializable
conflict so, it is a
that uses a frame size of 1 KB to send data on a (d) T 1 T No cycle, so, it
1.5 Mbps link with a one-way latency of 50 msec. T11 T22 No it is
is aa
schedule
cycle,serializable
conflict so,
T3 conflict serializable
To achieve a link utilization of 60%, the minimum schedule
conflict serializable
T3 schedule
number of bits required to represent the sequence T schedule
T33
number field is .
n×T
S2: AB→C, D→E, E→C is a minimal cover for
Let window size = n then link utilization = the set of functional dependencies AB→C, D→E,
(T + RTT) AB→E, E→C
0.6(5.33 + 100) Which one of the following is CORRECT?
Therefore, n = = 12
5.33
(a) S1 is TRUE and S2 is FALSE.
Window size in selective repeat = 2n−1 = 12; (b) Both S1 and S2 are TRUE.
so n − 1 = 4 ⇒ n = 5 (c) S1 is FALSE and S2 is TRUE.
Ans. (5) (d) Both S1 and S2 are FALSE.
Solution: REQ2:
S1: True, the highest normal form achieved by P1 (2,0,0)
any relation with two single valued attributes is With (3, 2, 0), request of P1 can be served. There
BCNF. is one safe sequence (P1, P2, P0). So, REQ2 can
S2: False, because the closure of AB contains be fulfilled.
different set of attributes. Ans. (b)
Ans. (a)
32. Consider the following set of processes that need
31. An operating system uses the Banker’s algorithm for to be scheduled on a single CPU. All the times are
deadlock avoidance when managing the allocation of given in milliseconds
three resource types X, Y, and Z to three processes
P0, P1, and P2. The table given below presents the
Process Arrival Execution
current system state. Here, the allocation matrix
Name Time Time
shows the current number of resources of each type
allocated to each process and the Max matrix shows A 0 6
the maximum number of resources of each type B 3 2
required by each process during its execution.
C 5 4
D 7 6
Allocation Max
X Y Z X Y Z E 10 3
P0 0 0 1 8 4 3
P1 3 2 0 6 2 0 Using the shortest remaining time first scheduling
algorithm, the average process turnaround time (in
P2 2 1 1 3 3 3
msec) is .
There are 3 units of type X, 2 units of type Y Solution
and 2 units of type Z still available. The system
is currently in a safe state. Consider the follow- A B A C E D
ing independent requests for additional resources
in the current state: 0 3 5 8 12 15 21
34. A canonical set of items is given below bags 1 to 5. Their total weight comes out to be
s → L. > R 323 gm. Then the product of the labels of the bags
having 11 gm coins is .
Q → R.
On input symbol < the set has Solution From the given information, we have
Solution: The given DFA accepts all the strings Solution Largest clique problem is known
ending with any number of 1’s. All the three NP-Complete problem. In the question, it is given
expressions are ending with 1 and are accepted by that there exists a polynomial time algorithm for larg-
the given DFA. est clique problem. Thus, P = NP = NP-Complete.
Ans. (d) Ans. (d)
37. There are 5 bags labeled 1 to 5. All the coins in a
given bag have the same weight. Some bags have 39. The minimum number of comparisons required to
coins of weight 10 gm, others have coins of weight find the minimum and the maximum of 100 num-
11 gm. I pick 1, 2, 4, 8, 16 coins, respectively, from bers is .
40. Consider a hash table with 9 slots. The hash func- The value returned by the function MyX is the
tion is h(k) = k mod 9. The collisions are resolved (a) m aximum possible sum of elements in any sub-
by chaining. The following 9 keys are inserted in array of array E.
the order: 5, 28, 19, 15, 20, 33, 12, 17, 10. The (b) maximum element in any sub-array of array E.
maximum, minimum, and average chain lengths in (c) sum of the maximum elements in all possible
the hash table, respectively, are sub-arrays of array E.
(d) the sum of all the elements in the array E.
(a) 3, 0, and 1 (b) 3, 3, and 3
(c) 4, 0, and 1 (d) 3, 0, and 2
Solution: The following code calculates sum of all
the elements in array.
Solution:
for(i = 0; i < size; i++)
Length Y = Y + E[i];
0 0 This Z = Z + E[k] calculates the sum of sub-array
1 28 19 10 × 3 and compare it with Y. If sum is greater, then it is
2 20 × 1 assigned to the Y. Hence, it calculates maximum
3 12 × 1 possible sum of any subarray.
4 0 Ans. (a)
5 5 × 1
6 15 33 × 2 42. Consider the following pseudo code. What is the
total number of multiplications to be performed?
7 0
17 ×
8 1 D = 2
for i = 1 to n do
9 for j = i to n do
for k = j + 1 to n do
Thus, average length =
9
=1 D = D * 3
9
Ans. (a) (a) Half of the product of the 3 consecutive integers.
(b) One-third of the product of the 3 consecutive
41. Consider the following C function in which size is integers.
the number of elements in the array E: (c) One-sixth of the product of the 3 consecutive
integers.
int MyX(int *E, unsigned int size) (d) None of the above.
{
int Y = 0; Solution:
int Z;
int i, j, k; ∑ ( n − 1) + ∑ ( n − 2) + ∑ 1 = n(n − 1)(n + 1)
for(i = 0; i < size; i++) 6
Y = Y + E[i]; Ans. (c)
for(i = 0; i < size; i++)
for(j = i; j < size; j++) 43. Consider a 6-stage instruction pipeline, where all
{ stages are perfectly balanced. Assume that there
Z = 0; is no cycle-time overhead of pipelining. When an
for(k = i; k <= j; k++) application is executing on this 6-stage pipeline,
Z = Z + E[k]; the speedup achieved with respect to non-pipelined
if (Z > Y)
Y = Z;
}
return Y;
}
execution if 25% of the instructions incur 2 pipeline 46. The function f (x) = x sin x satisfies the follow-
stall cycles is . ing equation. f ˝(x) + f(x) + t cosx = 0. The value
of t is .
Solution: For non-pipeline 6 stages = 6 cycles are
required Solution:
f (x) = x sinx
Pipeline time = 1 + (25/100) × 2 = 1.5
f ˝(x) + f (x) + t cosx = 0
f ´(x) = sinx + x cosx = > f ˝(x) = cosx + x(−sinx)
Non-pipelined 6
Speed-up = = =4
Pipeline time 1.5 + cosx = 2cosx − x sinx
Ans. (4) 2cosx − x sinx + x sinx + tcosx = 0
44. An access sequence of cache block addresses is of
2cosx + tcosx = 0 ⇒ t = −2
length N and contains n unique block addresses.
The number of unique block addresses between Ans. (−2)
two consecutive accesses to the same block address
is bounded above k. What is the miss ratio if 47. A function f(x) is continuous the interval [0,2]. It is
the access sequence is passed through a cache of known that f (0) = f (2) = −1 and f (1) = 1.
associativity A ≥ k exercising least-recently-used Which one of the following statements must be true?
replacement policy?
(a) T here exists a y in the interval (0,1) such that
(a) n/N (b) l/N (c) 1/A (d) k/n f (y) = f (y + 1)
(b) For every y in the interval (0,1), f (y) = f (2 − y)
Solution: The miss ratio is n/N. (c) The maximum value of the function in the
Ans. (a) interval (0.2) is 1
(d) There exists a y in the interval (0,1) such that
45. Consider the 4-to-1 multiplexer with two lines S1
f (y) =f (2 − y)
and S0 given below.
Solution: Let g(x) = f (x) − f (x + 1) defined in [0,1]
The minimal sum-of-products form of the Boolean 48. Four fair six-sided dice are rolled. The probability
expression for the output F of the multiplexer is that the sum of the results being 22 is X/1296. The
value of X is .
(a) PQ + QR + PQR
Solution Sum 22 comes with following possibilities:
(b) PQ + PQR + PQR + PQR
6 + 6 + 6 + 4 = 2 2 (numbers can be arranged in
(c) PQR + PQR + QR + PQR 4 ways)
6 + 6 + 5 + 5 = 22 (numbers can arranged in 6 ways)
(d) PQR
Therefore, the total ways X = 4 + 6 = 10
(1,2)}. Note that the pennant (1,2) is not the same (c) (3,3,3,1,0,0) (d) (3,2,1,1,1,0)
as the pennant (2,1). The number of 10-pennants 2,2,0,0,0 1,0,0,1,0 ← Reorder
1,−1,0,0 ← not graphic
is .
1,1,0,0,0
Solution 10 pennants: 1 + 9 + 28 + 35 + 15 + 0, 0, 0, 0
1 = 89 Ans. (c)
Ans. (89)
53. Which one of the following propositional logic for-
mulas is TRUE when exactly two of p, q, and r are
50. Let S denote the set of all functions f: {0,1} 4
TRUE?
→{0,1}4. Denote by N the number of functions
from S to the set {0,1}. The value of log2 log2 N is (a) ((p↔q)∧ r)∨(p ∧ q ∧ ~ r)
. (b) (~ (p↔q)∧ r)∨(p ∧ q ∧ ~ r)
(c) ((p→q)∧ r)∨(p ∧ q ∧ ~ r)
Solution: If a function is defined A → B, then
(d) (~ (p↔q)∧ r) ∧ (p ∧ q ∧ ~ r)
Size of A = |A| and Size of B = |B|
A = {0,1}4 ⇒ Number of elements in A, that is, Solution: Consider P = R = true and Q = false.
All the options other than B will become false.
|A| = 216
Number of functions from A → B, N = 2|A|
Ans. (b)
55. Consider two processors P1 and P2 executing the Suppose P1 takes 5 cycles, then P2 takes 20% more,
same instruction set. Assume that under identical that is, 5 + 5 × (20/100) = 6 cycles
conditions, for the same input, a program running P2 takes 25% less time = 5 − 5 × (25/100) = 3.75 ns
on P2 takes 25% less time but incurs 20% more CPI Let us say frequency of P2 = x GHz
(clock cycles per instruction) as compared to the pro- x GHz = x × 109 Hz
gram running on P1. If the clock frequency of P1 is 1 1 cycle ⇒ 1/x ns
6 cycles ⇒ 6/x ns = 3.75 ns
GHz, then the clock frequency of P2 (in GHz) is
Solution: Frequency of P1 = 1 GHz = 109 Hz Therefore x = 6/3.75 = 1.6 GHz
1 cycle ⇒ 1/109 s = 1 ns Ans. (1.6)
1. The security system at an IT office is composed pieces of paper are kept in a box. One of the pieces
of 10 computers of which exactly four are work- is drawn at random from the box. The expected
ing. To check whether the system is functional, length of the word drawn is . (The
the officials inspect four of the computers picked answer should be rounded to one decimal place.)
at random (without replacement). The system is
Solution: The given 9 words are as follows:
deemed functional if at least three of the four com-
The, Quick, Brown, Fox, Jumps, Over, The, Lazy, Dog
puters inspected are working. Let the probability
that the system is deemed functional be denoted Words with length 3 are => THE, FOX, THE,
by p then 100p = . DOG (4)
Words with length 4 are => OVER, LAZY (2)
Solution: P is the probability that system is func-
Words with length 5 are => QUICK, BROWN,
tional means at least three systems are working,
JUMPS (3)
p will be calculated as:
Probability for drawn 3 length words = 4/9
P for at least 3 systems are working
Probability for drawn 4 length words = 2/9
All 4 systems working + 3 systems working Probability for drawn 5 length words = 3/9
=
Number of ways to pick 4 systems from 100
4 2
P that 3 systems are working Expected word length = 3 × + 4 × +
= (Number of systems not selected) 9 9
3
× (Number of ways to pick 3
from 4 selected systems)
5×
9
= 6 × 4×
×3×2×4 = 3.88
Ans. (3.88)
Therefore,
3. The maximum number of edges in a bipartite
P for at least 3 systems are working graph on 12 vertices is .
(4 × 3 × 2 × 1) + (6 × 4 × 3 × 2 × 4)
= = 0.1192 Solution: The maximum number of edges in
10 × 9 × 8 × 7
n2
So, 100P = 11.92 bipartite graph =
4
Ans. (11.92) For n = 12, maximum number of edges in bipartite
2. Each of the nine words in the sentence “The 12 × 12
quick brown fox jumps over the lazy dog” graph = = 36 edges
4
is written on a separate piece of paper. These nine Ans. (36)
10. Consider the function func shown below: 1 and 7 are inserted into the heap in that order.
int func(int num) {
The level-order traversal of the heap after the
insertion of the elements is:
int count = 0;
(a) 10, 8, 7, 3, 2, 1, 5 (b) 10, 8, 7, 2, 3, 1, 5
while (num) {
(c) 10, 8, 7, 1, 2, 3, 5 (d) 10, 8, 7, 5, 3, 2, 1
count++;
num>>= 1; Solution:
} 10 10
return (count);
} 8 5 8 5
(c) the shortest paths from W to only those nodes (c) Both + and * are right associative
that are leaves of T. (d) Both + and * are left associative
(d) the longest path in the graph.
Solution: From production rule P → Q + P, we
Solution: Application of BFS is to find the short- can decide + is right associative and from T →
est path from u to v. In the given statement, W is T * U, we conclude * is left associative.
the source node. It will calculate shortest path to Ans. (b)
every node from W. So, option (b) is correct.
Ans. (b) 18. Which one of the following is NOT performed
during compilation?
15. If L1 = {an | n ≥ 0} and L2 = {b n | n ≥ 0}, Consider
(a) Dynamic memory allocation
(I) L1⋅L2 is a regular language (b) Type checking
(II) L1⋅L2 = {an bn n ≥ 0} (c) Symbol table management
(d) Inline expansion
Which one of the following is CORRECT?
Solution: Dynamic memory allocation is done at
(a) Only (I) (b) Only (II)
the run time, not at compile time.
(c) Both (I) and (II) (d) Neither (I) nor (II)
Ans. (a)
In option (1), concatenation of two regular lan-
19. Which one of the following is TRUE?
guages is regular (property).
But in option (2) L1⋅L2 = {an bn n ≥ 0} is not (a) T he requirements document also describes how
regular because both the variables are independent the requirements that are listed in the docu-
in the given languages L1 and L2. So, only option ment are implemented efficiently.
(a) is correct. (b) Consistency and completeness of functional
Ans. (a) requirements are always achieved in practice.
16. Let A ≤ m B denotes that language A is mapping
(c) Prototyping is a method of requirements
validation.
reducible (also known as many-to-one reducible) to
(d) Requirements review is carried out to find the
language B. Which one of the following is FALSE?
errors in system design.
(a) If A ≤ m B and B is recursive then A is recursive.
Solution: Prototype is constructed to check that
(b) If A ≤ m B and A is undecidable then B is
requirements can be fulfilled or not.
undecidable.
Ans. (a)
(c) If A ≤ m B and B is recursively enumerable
then A is recursively enumerable. 20. A FAT (file allocation table) based file system is
(d) If A ≤ m B and B is not recursively enumerable being used and the total overhead of each entry in
then A is not recursively enumerable. the FAT is 4 bytes in size. Given a 100 × 106 bytes
disk on which the file system is stored and data
Solution: As language A is mapping reducible to block size is 103 bytes, the maximum size of a file
language B, we have f : ∈* → ∈* where for every w, w ∈ A ⇔ fthat
(w) ∈ B be stored on this disk in units of 106 bytes
can
∈* where for every w, w ∈ A ⇔ f (w) ∈ B is .
Hence, if A ≤ m B and B is not recursively enumer-
able then A is not recursively enumerable. Solution:
Ans. (d) Disk size 108
Number of entries = = 3 = 105
17. Consider the grammar defined by the following Block size 10
production rules, with two operators * and + Entry size = 4 B, total space consumed = 4B × 105
S → T *P Maximum file size can be stored = 108 − 0.4 × 106 =
99.6 × 106 B
T → U | T *U
Ans. (99.6)
P → Q+P |Q
Q → Id
21. The maximum number of super keys for the rela-
tion schema R (E, F, G, H) with E as the key is
U → Id .
Which one of the following is TRUE? Solution: Considering E as key, Total 23 = 8 keys
(a) + is left associative, while * is right associative are possible.
(b) + is right associative, while * is left associative Ans. (8)
(a) T1 < T2 < T3 (b) T1 > T2 > T3 exactly one static embedded image which is also
(c) T2 = T3, T3 < T1 (d) T1 = T3, T3 > T2 at S. Assuming no caching, which one of the follow-
ing is correct about the HTML webpage loading
Solution: (including the embedded image)?
Given that bandwidth = 106 bytes/sec, file size =
1000 bytes, header size = 100 bytes (a) Q needs to send at least 2 HTTP requests to S,
each necessarily in a separate TCP connection
Case 1: A sends complete file to B
to server S
Total frame size = 1000 + 100 = 1100 bytes (b) Q needs to send at least 2 HTTP requests to
Transmission time = 1100/106 = 1100 ms S, but a single TCP connection to server S is
T1 = 3300 ms
sufficient
(c) A single HTTP request from Q to S is suffi-
Case 2: File size in chunks of 100 bytes cient, and a single TCP connection between Q
Total frame size = 100 + 100 = 200 bytes and S is necessary for this
Transmission time for 1 packet = 200/106 = 200 ms
(d) A single HTTP request from Q to S is suf-
ficient, and this is possible without any TCP
Transmission time for 10 packets = 2000 ms connection between Q and S
T2 = 2000 + 200 + 200 = 2400 ms Solution: Q sends at least 2 HTTP request to S,
Case 3: File size in chunks of 50 bytes single TCP connection is sufficient.
Total frame size = 50 + 100 = 150 bytes Ans. (b)
Transmission time for 1 packet = 150/106 = 150 ms 29. Consider the following schedule S of transactions
Transmission time for 20 packets = 3000 ms
T1, T2, T3, T4:
All the transactions are reading committed values, 32. Three processes A, B and C each execute a loop of
so schedule is recoverable. 100 iterations. In each iteration of the loop, a process
Ans. (c) performs a single computation that requires tc CPU
milliseconds and then initiates a single I/O operation
30. Consider a join (relation algebra) between relations
that lasts for tio milliseconds. It is assumed that the
r(R) and s(S) using the nested loop method. There
computer where the processes execute has sufficient
are 3 buffers each of size equal to disk block size,
number of I/O devices and the OS of the computer
out of which one buffer is reserved for intermediate
assigns different I/O devices to each process. Also,
results. Assuming size(r(R)) < size(s(S)), the join
the scheduling overhead of the OS is negligible. The
will have fewer number of disk block accesses if
processes have the following characteristics:
(a) relation r(R) is in the outer loop.
(b) relation s(S) is in the outer loop. Process id tc tio
(c) join selection factor between r(R) and s(S) is A 100 ms 500 ms
more than 0.5. B 350 ms 500 ms
(d) join selection factor between r(R) and s(S) is
C 200 ms 500 ms
less than 0.5.
Solution: The relation r(R) should be in outer The processes A, B, and C are started at times
loop to have minimum number of disk accesses. 0, 5 and 10 milliseconds, respectively, in a pure
Ans. (a) time sharing system (round robin scheduling) that
uses a time slice of 50 milliseconds. The time in
31. Consider the procedure below for the Producer- milliseconds at which process C would complete its
Consumer problem which uses semaphores: first I/O operation is .
Semaphore n = 0; Solution: Time quantum = 50 ms
Semaphore s = 1; A B C A B C B C B C B
0 50 100 150 200 250 300 350 400 450 500 650
void producer ( ) void consumer() After completion of its CPU operations at 500 ms,
{ { C goes for I/O operations. This will last for 500 ms.
while (true) while(true) Hence, process C will complete its first I/O at
{ semWait(s); 500 + 500 = 1000 ms.
Produce(); semWait(n); Ans. (1000)
SemWait(s); removeFromBuffer(); 33. A computer has twenty physical page frames which
addToBuffer(); semsignal(s); contain pages numbered 101 through 120. Now a
semSignal(s); consume(); program accesses the pages numbered 1, 2, …,
100 in that order, and repeats the access sequence
semSignal(n); }
THRICE. Which one of the following page replace-
} } ment policies experiences the same number of page
} faults as the optimal page replacement policy for
this program?
Which one of the following is TRUE? (a) Least-recently-used (b) First-in-first-out
(a) The producer will be able to add an item to the (c) Last-in-first-out (d) Most-recently-used
buffer, but the consumer can never consume it.
Solution: Reference string = 1, 2, 3…. 100, 1, 2,
(b) The consumer will remove no more than one
3…. 100, 1, 2, 3…. 100
item from the buffer.
(c) Deadlock occurs if the consumer succeeds in In OPR technique, total page faults will be = 300
acquiring semaphore s when the buffer is empty. (100 every time)
(d) The starting value for the semaphore n must be This behaves same as most recently used, so num-
1 and not 0 for deadlock-free operation. bers of page faults are same.
Ans. (d)
Solution: When consumer gets semaphore s and
buffer is empty that means value of n is still zero. 34. For a C program accessing X[i] [j] [k], the follow-
Thus, consumer goes into infinite loop at second ing intermediate code is generated by a compiler.
step resulting in deadlock. Assume that the size of an integer is 32 bits and
Ans. (c) the size of a character is 8 bits.
Solution: Solution:
Eigen vector is AX = l X
S E M
1 0 0 0 1
1 bit 8 bits 23 bits 0 1 1 1 0
(14.25)10 = (1110.01)2 A = 0 1 1 1 0
Now, normalize the binary number 1.11001 × 23 0 1 1 1 0
1 0 0 0 1
E = e + Bias
Convert the given matrix in diagonal matrix. The
E = 3 + 127 = 130 diagonal entries are called Eigen values.
Binary equivalent of 130 = (10000010)2 Eigen values = 2, 3
Therefore, S = 1 (Since negative number) Product of Eigen values = 2 × 3 = 6
M = (11001000…) Ans. (6)
E = 10000010 48. The probability that a given positive integer lying
between 1 and 100 (both inclusive) is NOT divis-
1100 0001 0110 0100 0000 …. ible by 2, 3 or 5 is .
C 1 6 4 0 000
Solution: Let A event → Divisible by 2, B event →
Ans. (a) Divisible by 3, C event → Divisible by 5
46. In the Newton-Raphson method, an initial guess of P(A′ ∩ B′ ∩ C′) = 1 − P(A ∪ B ∪ C)
x0 = 2 is made and the sequence x0, x1, x2, … is P(A) = 50/100, P(B) = 33/100, P(C) = 20/100
obtained for the function 0.75x3 − 2x2 − 2x + 4 = 0 P(A ∩ B) = 16/100, P(B ∩ C) = 6/100,
P(A ∩ C) = 10/100, P(A ∩ B ∩ C) = 3/100
Consider the statements
P(A ∪ B ∪ C) = P(A) + P(B) + P(C) − P(A ∩ B) −
(I) x3 = 0.
P(B ∩ C) − P(A ∩ C) + P(A ∩ B ∩C)
(II) The method converges to a solution in a finite
number of iterations. P(A ∪ B ∪ C) = (50 + 33 + 20 − 16 − 6 − 10 +
3)/100 = 74/100
Which of the following is TRUE?
P(A′ ∩ B′ ∩ C′) = 1 − 74/100 = 26/100 = 0.26
(a) Only I (b) Only II
(c) Both I and II (d) Neither I nor II Ans. (0.259 to 0.261)
Solution: According to Newton—Raphson method: 49. The number of distinct positive integral factors of
.
x1 = x0 − f0 /f0′
2014 is
Solution: 1 4
2 2
Both S1 and S2 are true. 2 1 2 1
2 1 2 1
Ans. (a)
51. A cycle on n vertices is isomorphic to2 its comple-
2 2 1 2 2 2 1
ment. The value of n is 1 . 1 1 1
Solution: 2 2 2 2
Consider n = 5, Applying Kruskal’s 5
2
2 2
V22 2 2 2
V2 V3 2 1 2V3 1
2 2 2 1
2 2 1 1
V1 2 1
V1 1 1
V4 2 2
V5 V4 V5 2 2 6
Complement 2
Cycle graph 3 2
of 2 1 2 1
2 cycle graph
1 2 1
By checking properties these two graphs are isomorphic. 2 2 2 1
2 2 2 1 1 1
1 1 Ans. (5)
2 2
52. The number of distinct minimum spanning trees
2 2 Hence, number of distinct minimum spanning trees
for the weighted graph below is are 6.
Ans. (6)
2
2 53. Which one of the following Boolean expressions is
1 2 1
NOT a tautology?
S (d)
A c A b
A1 5 A1 3
A2 4 A2 4
A2 4 Hence, option (c) is correct.
A3 6 Ans. (c)
Query Output: 55. Consider a main memory system that consists of
8 memory modules attached to the system bus,
A b which is one word wide. When a write request is
A1 3 made, the bus is occupied for 100 nanoseconds (ns)
A2 4 by the data, address, and control signals. During
A2 4 the same 100 ns, and for 500 ns thereafter, the
addressed memory module executes one cycle
(a) accepting and storing the data. The (internal) oper-
A b ation of different memory modules may overlap in
A1 3 time, but only one request can be on the bus at
A2 4 any time. The maximum number of stores (of one
A2 4 word each) that can be initiated in 1 millisecond
is .
A2 4
(b) Solution:
A c For write request bus is occupied for = 100 ns
A1 3 For storing the data requires = 100 ns
A2 4 100 ns → 1 store
(c) 100/109 → 1 store
A b 1 sec → 107
A1 3 10−3 s → 10−3 × 107 stores = 104, that is, 10,000
A2 4 stores
A2 4 Ans. (10000)
Solution: Solution:
a=b+c T(9) = 1 + min(T(x), T(y))
c=a+d T(x) and T(y) can be two values: either move right
d = 2b + c + d or short cut
e=c
Moving right means: 10 and shortcut from 9 is 15
a=d
So product is 15 × 10 = 150.
The maximum number of nodes and edges are
Ans. (150)
(6, 6).
Ans. (a) 38. Consider the decision problem 2CNFSAT defined
as follows:
35. Which one of the following problems is undecidable?
{f f is a satisfiable propositional formula in CNF
(a) Deciding if a given context-free grammar is with at most two literal per clause}
For example, φ = (x1 ∨ x2 ) ∧ (x1 ∨ x3 ) ∧ (x2 ∨ x4 )
ambiguous.
(b) Deciding if a given string is generated by a
is a Boolean formula and it is in 2CNFSAT. The
given context-free grammar.
decision problem 2CNFSAT is
(c) Deciding if the language generated by a given
context-free grammar is empty. (a) NP-Complete.
(d) Deciding if the language generated by a given (b) solvable in polynomial time by reduction to
context-free grammar is finite. directed graph reachability.
J Q2 J Q1 J Q0
C C C
K Q2 K Q1 K Q0
Solution:
Present State Next State
Q2 Q1 Q0 Q2(J = Q1′, K = Q0) Q1(J = Q2, K = Q2′) Q0(J = Q1, K = Q0′)
0 0 0 1 0 0
1 0 0 1 1 0
1 1 0 1 1 1
Ans. (c)
46. With respect to the numerical evaluation of the 47. The value of the integral given below is
p
definite integral, K = ∫
b 2
∫x
x dx , where a and b are 2
a cos x dx
given, which of the following statements is/are 0
TRUE? (a) −2p (b) p (c) −p (d) 2p
(I) The value of K obtained using the trapezoidal Solution:
rule is always greater than or equal to the p
F(P,Q) = ((1 ⊕ P) ⊕ (P ⊕ Q)) ⊕ ((P ⊕ Q) (P ′( PQ′ + P ′Q) + P (PQ′ + P ′Q)′ ) ⊕ (Q(PQ′ + P ′Q)′ + Q′(PQ′ + P ′Q
⊕ (Q ⊕ 0)) (P ′( PQ′ + P ′Q) + P (PQ′ + P ′Q)′ ) ⊕ (Q(PQ′ + P ′Q)′ + Q′(PQ′ + P ′Q))
Q′ ⊕ P
The equivalent expression for F is
(P ⊕ Q)′
(a) P + Q (b) P + Q
(c) P ⊕ Q (d) P ⊕ Q
Ans. (d)
(c) T(n) = 2T(n−1) + cn 7. Which of the following is/are correct inorder tra-
(d) T(n) = T(n/2) + cn versal sequence(s) of binary search tree(s)?
Solution: Quicksort’s worst case is when the list I. 3, 5, 7, 8, 15, 19, 25
is already sorted or when it is in reverse order. In II. 5, 8, 9, 12, 10, 15, 25
both cases, the pivot element is already in sorted III. 2, 7, 10, 8, 14, 16, 20
order and list will always be divided into two lists IV. 4, 6, 7, 9, 18, 20, 25
containing 1 and n−1 elements.
(a) I and IV only (b) II and III only
Thus, the recurrence relation formed is as follows: (c) II and IV only (d) II only
T(n) = T(n−1) + T(1) + cn Solution: Inorder traversal of tree is the sorted
Ans. (b) order of nodes of the tree. Check the given
5. The height of a tree is the length of the longest sequences; the one which is in sorted order is the
root-to-leaf path in it. The maximum and minimum correct inorder traversal.
number of nodes in a binary tree of height 5 are Ans. (a)
8. Which one of the following is TRUE at any valid
(a) 63 and 6, respectively
state in shift — reduce parsing?
(b) 64 and 5, respectively
(c) 32 and 6, respectively (a) Viable prefixes appear only at the bottom of
(d) 31 and 5, respectively the stack and not inside
(b) Viable prefixes appear only at the top of the
Solution: If height of the binary tree is h, then stack and not inside
Minimum number of nodes in a tree = h + 1 (c) The stack contains only a set of viable prefixes
Maximum number of nodes = 2h+1 − 1 (d) The stack never contains viable prefixes
Equivalence class testing is dividing the input of Solution: A: The given set and 2A: Power set of
software into small partitions of equivalent data, A, that is, set of subsets of A.
from which various test cases are derived. This is
black box testing. Volume or stress testing is a type Φ is the subset of every set.
of system testing. Therefore, statements I and II are true.
Alpha testing is done by the user/customer for Since 5 does not belong to 2A. Hence, statement IV
checking its performance. is not true.
Ans. (d) Ans. (c)
D Q D Q D Q D Q
CLK
CLR
Ans. (d)
12. For computers based on three-address instruction II. If the estimated round trip time at any given
formats, each address field can be used to specify point of time is t sec the value of the retrans-
which of the following. mission timeout is always set to greater than or
equal to t sec.
S1: A memory operand
III. The size of the advertised window never changes
S2: A processor register
during the course of the TCP connection.
S3: An implied accumulator register
IV. The number of unacknowledged bytes at the
(a) Either S1 or S2 (b) Either S2 or S3 sender is always less than or equal to the
(c) Only S2 and S3 (d) All of S1, S2 and S3 advertised window.
Solution: Computer with three-address instruc-
tion format can use each address field to specify (a) III only (b) I and III only
either processor register or memory operand. (c) I and IV only (d) II and IV only
Ans. (a)
Solution: It is not necessary to have the subse-
13. Suppose two hosts use a TCP connection to trans-
quent sequence number as m + 1.
fer a large file. Which of the following statements
is/are FALSE with respect to the TCP connection? Size of window is not constant during TCP connec-
tion. It changes once the threshold value of conges-
I. If the sequence number of a segment is m,
tion window reaches. So, statement III is false.
then the sequence number of the subsequent
segment is always m + 1. Ans. (b)
14. Suppose that everyone in a group of N people wants not recursive, which of the following is/are neces-
to communicate secretly with the N−1 others using sarily true?
symmetric key cryptographic system. The commu- I. L1 (complement of L1) is recursive
nication between any two persons should not be
II. L 2 (complement of L2) is recursive
decodable by the others in the group. The number
of keys required in the system as a whole to satisfy III. L1 is context-free
the confidentiality requirement is IV. L1 ∪ L2 is recursively enumerable
(a) 2N (b) N(N−1) (a) I only (b) III only
(c) N(N−1)/2 (d) (N−1)2 (c) III and IV only (d) I and IV only
Solution: In symmetric key cryptography, for n Solution: Context-free language is also recur-
users the number of possible keys is n(n−1)/2. sive and recursive is closed under complement.
Therefore, statement I is true.
Ans. (c)
Complement of recursive enumerable is not recur-
15. Which of the following statements is/are FALSE? sive. Hence, statement II is false.
I. XML overcomes the limitations in HTML to Context-free language is not closed under comple-
support a structured way of organizing content. ment. Hence, statement III is false.
II. XML specification is not case sensitive while ∼L1 is recursive and hence also recursive enumer-
HTML specification is case sensitive. able. L2 is recursive enumerable. RE languages are
III. XML supports user defined tags while HTML closed under union. Hence, statement IV is true.
uses pre-defined tags. Ans. (d)
IV. XML tags need not be closed while HTML tags
19. Consider a system with byte-addressable memory,
must be closed
32-bit logical addresses, 4 kilobyte page size and
(a) II only (b) I only page table entries of 4 bytes each. The size of the
(c) II and IV only (d) III and IV only page table in the system in megabytes is .
Solution: HTML is case insensitive, while XML is Solution: Logical address space = 32 bits
case sensitive. Therefore, statement II is incorrect. Page size = 4 KB = 212
XML tags must be closed. Hence, statement IV is Each page table entry size = 4 B
also false.
Number of pages = 232/212 = 220
Ans. (c)
Page table size = (Number of pages) × (Page table
16. Which one of the following fields of an IP header is entry size) = 4 B × 220 = 4 MB
NOT modified by a typical IP router? Ans. (4)
(a) Checksum (b) Source address 20. The following two functions P1 and P2 that share
(c) Time to Live (TTL) (d) Length a variable B with an initial value of 2 execute
concurrently.
Solution: Source address is the only thing that
remains fixed and cannot be varied. P1() P2()
Ans. (b) { {
C = B – 1; D = 2 * B;
17. In one of the pairs of protocols given below, both the B = 2 * C; B = D – l;
protocols can use multiple TCP connections between } }
the same client and the server. Which one is that?
The number of distinct values that B can possible
(a) HTTP, FTP (b) HTTP, TELNET take after the execution is .
(c) FTP, SMTP (d) HTTP, SMTP
Solution: Initial value of B = 2
Solution: HTTP can use multiple TCP connec- There are four possibilities in which two processes
tions for each resource. can be executed:
FTP uses Telnet protocol for control information
P1 is executed first and then P2:
on a TCP connection and another TCP connection
for data exchange. After this execution B = 3
Ans. (a) P2 is executed first and then P1
18. For any two languages L1 and L2 such that L1 is After this execution B = 4
context-free and L2 is recursively enumerable but P1 is preempted by P2
2 2
23. In the LU decomposition of the matrix
9
,
4 Q. NO. 26 – 55 Carry Two Marks Each
if the diagonal elements of U are both 1, then the
lower diagonal entry l22 of L is –———–—.
26. Suppose that the stop-and-wait protocol is used on
Solution: In LU decomposition, we have a link with a bit rate of 64 kilobits per second and
é2 2 ù él11 0 ù é1 u12 ù 20 milliseconds propagation delay. Assume that
êë4 9 úû = êl úê ú the transmission time for the acknowledgement
ë 21 l22 û ë0 1 û and the processing time at nodes are negligible.
Solving the above matrix, we have l11 = 2 . Now, Then the minimum frame size in bytes to achieve
l11u12 = 2 ⇒ u12 = 1 a link utilization of at least 50% is .
Also, l21 = 4 . So, l21u12 + l22 = 9 ⇒ l22 = 5
Solution: Minimum frame size means 100%
Ans. (5)
utilization.
24. The output of the following C program is .
RTT = 20 × 2 = 40 ms
void f1(int a, int b)
{ Bit rate = 64 Kbps
int c; 64 × 10 bytes ⇒ 1 sec
3
27. Consider a max heap, represented by the array: 40, Now consider that a value 35 is inserted into this
30, 20, 10, 15, 16, 17, 8, 4. heap. After insertion, the new heap is
Solution:
40 40
30 20 Insert 30 20
35
10 15 16 17 10 15 16 17
8 4 8 4 35
40 40
35 20 30 20
10 30 16 17 10 35 16 17
8 4 15 8 4 15
40, 35, 20, 10, 30, 16, 17, 8, 4, 15
Ans. (b)
= 0.1 × 0.8 × 0.7 × 0.6 + 0.9 × 0.2 × 0.7 × of the elements of . Let pr be the probability that
0.6 + 0.9 × 0.8 × 0.3 × 0.6 + 0.9 × 0.8 × an element (x, y, z) ∈ 3 chosen equiprobably satis-
0.7 × 0.4 fies x ∨ (y ∧ z) = (x ∨ y) ∧ (x ∨ z). Then
= 0.4404 (a) pr = 0 (b) pr = 1
Ans. (0.4404)
(c) 0 < pr £
1 1
(d) < pr < 1
30. The binary operator ≠ is defined by the following 5 5
truth table.
3
Solution: Number of elements in
p q p≠q
= Number of ways to choose 3 elements from 5
0 0 0
= 5 × 5 × 5 = 125 ways
0 1 1
Now, when we take x = t, then also the given con-
1 0 1
dition is satisfied for any y and z.
1 1 0
Number of ways y and z can be taken = 5 × 5 =
Which one of the following is true about the binary 25 ways.
operator ≠?
If we take x = p, y = p, z = r, the given condition
(a) Both commutative and associative is satisfied. So, pr > (25 / 125) > 1/5
(b) Commutative but not associative
(c) Not commutative but associative For x = t, y = r, z = p, the given condition is not
(d) Neither commutative nor associative satisfied. So, pr ≠ 1.
Solution: Looking at the truth table, we analyze So, option (d) is correct.
that this is the same as XOR operation. XOR is Ans. (d)
both commutative (i.e., x*y = y*x) and associative
33. Consider the operations
(i.e., a*(b*c) = (a*b)*c).
Ans. (a) f(X,Y,Z) = X´YZ + XY´ + Y´Z´ and
99 g(X,Y,Z) = X´YZ + X´YZ´ + XY.
31. ∑
1
= _______. Which one of the following is correct?
x =1 x(x + 1)
(a) Both {f} and {g} are functionally complete
Solution: (b) Only {f} is functionally complete
99 (c) Only {g} is functionally complete
å x(x + 1) = 1 × 2 + 2 × 3 + 3 × 4 + + 99 × 100
1 1 1 1 1
(d) Neither {f} nor {g} is functionally complete
x =1
2− 1 3− 2 100 − 99 Solution: A system of Boolean functions is func-
= + + + tionally complete iff function satisfies following
1× 2 2 × 3 99 × 100
properties:
1 1 1 1 1 1
= 1− + − + − + − ••at
2 2 3 3 99 100 least one function that does not preserve zero
1 ••at least one function that does not preserve one
= 1− = 0.99
100 ••at least one function that is not linear
Ans. (0.99) ••at least one function that is not monotone
32. Suppose = {p,q,r,s,t} is a lattice represented by ••at least one function that is not self-dual
the following Hasse diagram:
{f} satisfies all the above properties.
t Ans. (b)
34. Let G be a connected planar graph with 10 verti-
q r s ces. If the number of edges on each face is three,
then the number of edges in G is .
E= 24 p =q +r
Ans. (24) 1 s =p +q
u =s ∗v
35. Let an represent the number of bit strings of length
n containing two consecutive 1’s. What is the
recurrence relation for an?
(a) an-2 + an-1 + 2n-2 2 v =r +u 3 q =s ∗u
(b) an-2 + 2an-1 + 2n-2
(c) 2an-2 + an-1 + 2n-2
(d) 2an-2 + 2an-1 + 2n-2
q =v +r
Solution: Suppose n = 3, possible strings are ⇒
4
011, 110, 111
Checking the three conditions given in question,
For n = 2 ⇒ 11 is the only possible string
r is live at block2 since r is used in block4 and has a
a3 = a1 + a2 + 21 = 0 + 1 + 2 = 3 ⇒ True path from block2 to block4 in the control flow graph.
a3 = a1 + 2a2 + 21 = 0 + 2 + 2 = 4 ⇒ False Also there isn’t any modification of r in the path.
a3 = 2a1 + a2 + 21 = 0 + 1 + 2 = 3 ⇒ True
Hence, r is live at block2. Similarly, check for u.
Ans. (c)
a3 = 2a1 + 2a2 + 21 = 0 + 2 + 2 = 4 ⇒ False
Now for options (a) and (c), check by considering n 37. The least number of temporary variables required
= 4. The answer comes out to be (a). to create a three-address code in static single
assignment form for the expression q+r/3+s—
Ans. (a)
t*5+u*v/w is –––.
36. A variable x is said to be live at a statement Si in Solution: We will need a temporary variable for
a program if the following three conditions hold storing the result of each binary operation.
simultaneously:
q + r / 3 + s — t * 5 + u * v/w
I. There exists a statement Sj that uses x t1 = r/3;
II. There is a path from Si to Sj in the flow graph t2 = t*5;
corresponding to the program t3 = u*v;
III. The path has no intervening assignment to x t4 = t3/w;
including at Si and Sj t5 = q + t1;
t6 = t5 + s;
t7 = t5 — t2;
p =q +r
1
t8 = t7 + t4
s =p +q So, a total of 8 temporary variables are required.
u =s ∗v Ans. (8)
38. Consider an Entity-Relationship (ER) model in
which entity sets E1 and E2 are connected by an
m:n relationship R12. E1 and E3 are connected by
2 v =r +u 3 q =s ∗u a 1: n (1 on the side of E1 and n on the side of E3)
relationship R13.
E1 has two single-valued attributes a11 and a12
q =v +r
of which a11 is the key attribute. E2 has two single-
4
valued attributes a21 and a22 of which is the key
attribute. E3 has two single-valued attributes a31
The variables which are live at the statement in and a32 of which a31 is the key attribute. The rela-
basic block 2 and at the statement in basic block 3 tionships do not have any attributes.
of the above control flow graph are If a relational model is the derived from the
above ER model, then the minimum number of
(a) p, s, u (b) r, s, u relations that would be generated if all the rela-
(c) r, u (d) q, v tions are in 3NF is .
E2: <a21, a22> Solution: This PDA is accepts all strings of the
E3: <a31, a32, a11> form x0x’ or x1x’, where x’ is the reverse of the
1’s complement of x. The given string 101100 has
E1−E3: <a11, a21>for m:n relationship E1 − E2 6 letters and we are given 5 letter strings. So, x0
We cannot combine any relation here as it will give is done, with x = 10110. Now, the stack contains
rise to partial functional dependency and thus vio- 10110 and remaining string should be opposite and
late 3NF. reverse to it, so that the stack becomes empty. So,
Ans. (4) 1’s complement of x = 01001 and x’ = 10010.
Ans. (b)
39.
41. Let G = (V, E) be a simple undirected graph, and
b a a b s be a particular vertex in it called the source.
a
N:
b For x ∈ V, let d(x) denote the shortest distance
M:
in G from s to x. A breadth first search (BFS) is
performed starting at s. Let T be resultant BFS
b a tree. If (u, v) is an edge of G that is not in T, then
which one of the following CANNOT be the value
Consider the DFAs M and N given above. The
of d(u) — d(v)?
number of states in a minimal DFA that accepts
the languages L(M) ∩ L(N) is . (a) —1 (b) 0 (c) 1 (d) 2
Solution: M accepts the strings ending with `a’. Solution: In BFS traversal of given graph, we can
N accepts the strings ending with `b’. Thus, their either visit node u first or node v. Let us assume
intersection should accept an empty string. node u if visited first. Then all neighbors of node u
will be in the queue to be visited. Since v is also a
neighbor of u and v is not visited before, d(v) will
become d(u) + 1.
So, d(u) − d(v) = 2 is not possible.
a,b Ans. (d)
Ans. (1)
42. Consider a uniprocessor system executing three
40. Consider the NPDA tasks T1, T2 and T3, each of which is composed
of an infinite sequence of jobs (or instances) which
〈Q = {q0, q1, q2}, ∑ = {0, 1}, Γ = {0, 1, Γ}⊥ d, q0, arrive periodically at intervals of 3, 7 and 20
⊥, F = {q2}〉, milliseconds, respectively. The priority of each task
where (as per usual convention) Q is the set of is the inverse of its period, and the available tasks
states, ∑ is the input alphabet, d is the stack alpha- are scheduled in order of priority, with the highest
bet, d is the state transition function, q0 is the ini- priority task scheduled first. Each instance of T1,
tial state, ⊥ is the initial stack symbol, and F is T2 and T3 requires an execution time of 1, 2 and
the set of accepting states. The state transition is 4 milliseconds, respectively. Given that all tasks
as follows: initially arrive at the beginning of the 1st milli-
second and task preemptions are allowed, the first
instance of T3 completes its execution at the end of
1, Z → 1 Z 0, 1Z → Z milliseconds.
0, Z → 0 Z 1, 0Z → Z
q0 q1 q2 Solution:
T1 T2 T1 T3 T1 T3 T2 T1 T2 T3
0/1/∈, Z → Z
1 2 4 5 7 8 9 10 11 12 13
∈, ⊥ → ∈
Ans. (13)
Which one of the following sequences must follow 43. A positive edge-triggered D flip-flop is connected
the string 101100 so that the overall string is to a positive edge-triggered JK flip-flop as follows.
accepted by the automaton? The Q output of the D flip-flop is connected to both
the J and K inputs of the JK flip-flop, while the Q that is, 60 seconds = 10000 rotations
output of the JK flip-flop is connected to the input 60
of the D flip-flop is connected to both the J and K 1 rotation = = 6 ms
10000
inputs of the JK flip-flop, while the Q output of Rotational latency = 6/2 = 3 ms
the JK flip-flop is connected to the input of the D
flip-flop. Initially, the output of the D flip-flop is One rotation = One track
set to logic one and the output of the JK flip-flop Number of sectors in one track = 600 ⇒ 600 sectors
is cleared. Which one of the following is the bit = 6 ms
sequence(including the initial state) generated at the One sector ⇒ 6/600 = 0.01 ms
Q output of the JK flip-flop when the flip-flops are
The file contains 2000 sectors.
connected to a free-running common clock? Assume
that J = K = 1 is the toggle mode and J = K = 0 is Therefore, 2000 sectors take = 2000 × 0.01 = 20 ms
the state-holding mode of the JK flip-flop. Both the Hence, total time needed to read the file
= (Number of sectors) × (Seek time + Rotational
flip-flops have non-zero propagation delays.
(a) 0110110….. (b) 0100100…. latency) + (Time for 2000 sectors)
(c) 011101110…. (d) 011001100… = 2000 × (4 + 3) + 20 =14020
Solution: Ans. (14000—14020)
45. Consider a non-pipelined processor with a clock
rate of 2.5 gigahertz and average cycles per instruc-
tion of four. The same processor is upgraded to a
D Q J Q1 pipelined processor with five stages; but due to the
internal pipelined delay, the clock speed is reduced
FF1 FF2 to 2 gigahertz. Assume that there are no stalls in
the pipeline. The speed up achieved in this pipe-
K lined processor is .
Solution: Non-pipelined method:
Clock Q Q1
Frequency = 2.5 GHz
0 (initial) 1 0
Time required for one cycle = 1/(2.5) = 0.4 ms
1 0 1
Each instruction requires 4 cycles. Therefore, time
2 1 1 for each instruction = 0.4 × 4 = 1.6 ms
3 1 0 Pipelined structure:
4 0 1 Frequency = 2 GHz
5 1 1 Time = 0.5 ms
Enhanced time for each stage in pipeline structure =
6 1 0
time for one stage of pipeline = 0.5 ms
7 0 1 Non-pipelined structure time 1.6
Speed up = = = 3.2
Ans. (a) Enhanced time in pipeline 0.5
Non-pipelined structure time 1.6
44. Consider a disk pack with a seek time of 4 = = 3.2
Enhanced time in pipeline
milliseconds and rotational speed of 10000 rotations 0.5
Ans. (3.2)
per minute (RPM). It has 60 sectors per track and
each sector can store 512 bytes of data. Consider a 46. Suppose the following disk request sequence (track
file stored in the disk. The file contains 2000 sec- numbers) for a disk with 100 tracks is given: 45,
tors. Assume that every sector access necessitates a 20, 90, 10, 50, 60, 80, 25, 70. Assume that the ini-
seek, and the average rotational latency for access- tial position of the R/W head is on track 50. The
ing each sector is half of the time for one complete additional distance that will be traversed by the
rotation. The total time (in milliseconds) needed to R/W head when the Shortest Seek Time First
read the entire file is . (SSTF) algorithm is used compared to the SCAN
(Elevator) algorithm (assuming that SCAN algo-
Solution: Seek time = 4 ms rithm moves towards 100 when it starts execution)
RPM = 10000 rotations is tracks.
Solution: SSTF:
SCAN: 1 10 20 25 45 50 60 70 80 90 100
1 10 20 25 45 50 60 70 80 90 100 10 15
10 10 10 10
10 10
10 5 65
10 10
55
5 20 Total Head Moments = 10 + 15 + 10 + 10 + 10 + 65
+ 5 + 10 = 130
10
47. Consider a main memory with five page frames (a) Both incur the same number of page faults.
and the following sequence of page references: 3, (b) FIFO incurs 2 more page faults than LRU
8, 2, 3, 9, 1, 6, 3, 8, 9, 3, 6, 2, 1, 3. Which one of (c) LRU incurs 2 more page faults than FIFO
the following is true with respect to page replace- (d) FIFO incurs 1 more page faults than LRU
ment policies First In First Out (FIFO) and Least
Recently Used (LRU)?
Solution:
FCFS:
1 1 1 1 1 1 1 1 1 1
9 9 9 9 9 9 9 9 2 2 2
2 2 2 2 2 2 8 8 8 8 8 8 8
8 8 8 8 8 8 3 3 3 3 3 3 3 3
3 3 3 3 3 3 6 6 6 6 6 6 6 6 6
Hit Hit Hit Hit Hit Hit
3 8 2 3 9 1 6 3 8 9 3 6 2 1 3
LRU:
1 1 1 1 1 1 1 2 2 2
9 9 9 9 9 9 9 9 9 9 9
2 2 2 2 2 2 8 8 8 8 8 1 1
8 8 8 8 8 6 6 6 6 6 6 6 6 6
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
Hit Hit Hit Hit Hit Hit
3 8 2 3 9 1 6 3 8 9 3 6 2 1 3
2 /p
∫1/p
cos(1 / x) Insertion will take a constant time, that is, Θ(1).
48. dx = _____.
x2 N insert operations need = N × Θ(1) = Θ(N) time.
2 /p
Solution: Substituting 1/x = t in ∫
cos(1 / x) Delete operations will also be done in constant time
dx
1 /p
x2 since the pointer to be deleted is provided and no
Differentiating both sides, we get −1/x2 dx = dt time will be required for search.
t = 1/x = p and p /2 (log N)1/2 delete operations will be completed in =
p Θ((log N)1/2) time
Therefore, ∫p /2 cos(t) dt = sin p − sin p / 2 = −1 Decrease key operation will also take = Θ((log
Ans. (−1) N)1/2) time
If we use sorted order array, insertion will take
49. Consider the following 2 × 2 matrix A where two more time since we need to find its exact position
elements are unknown and are marked by a and b. in the list where to insert the data. And in the case
The eigenvalues of this matrix are −1 and 7. What of min heap, more time will be required in inser-
are the values of a and b? tion and deletion since heapify algorithm needs to
1 4 be implemented after each insertion or deletion.
A=
b a Ans. (a)
Solution:
Student_Name Marks
15
Raj 310 B E
Rohit 140 4
Ans. (2)
A 2 F
52. What is the output of the following C code?
Assume that the address of x is 2000 (in decimal) 9 6
and an integer requires four bytes of memory? C D
int main()
{ B
15
E
unsigned int x[4][3] = {(1,2,3),
{4,5,6}, {7, 8, 9}, {10, 11, 12}}; 10 4
printf(“%u, %u, %u”, x+3, *(x+3),
*(x+2)+3); A 2 7 F
}
9 6
(a) 2036, 2036, 2036
(b) 2012, 4, 2204 C D
16
Sum = 10 + 9 + 2 + 15 + 16 + 7 + 4 + 6 = 69
(c) 2036, 10, 10
(d) 2012, 4, 6
Ans. (69)
Solution:
1 2 3 54. Consider the following C function.
4 5 6 int fun1 (int n)
7 8 9 {
10 11 12 int i, j, k, p, q = 0;
for (i = 1; i < n; ++i)
As given in question, integer takes 4 bytes to {
store p = 0;
for(j = n; j > 1; j = j/2)
Here, X + 3 ⇒ 2000 + (3 × 3) × 4 = 2036 ++p;
*(X + 3) ⇒ Value at address 2036, %u will print for(k = 1; k < p; k = k*2)
++q;
the address only, that is, 2036 }
*(X + 2) + 3 ⇒ 2000 + (3 × 2)4 + 3 × 4 = 2036 return q;
}
Ans. (a)
Which one of the following most closely approxi-
53. The graph shown below has 8 edges with distinct mates the return value of the function fun1?
integer edge weights. The minimum spanning tree
(MST) is of weight 36 and contains the edges: (a) n3 (b) n(log n)2
{(A, C), (B, C), (B, E), (E, F), (D, F)}. The edge (c) n log n (d) n log(log n)
weights of only those edges which are in the MST Solution: First for loop will execute n times, the
are given in the figure shown below. The minimum others are sequential not nested, so both execute
possible sum of weights of all 8 edges of this graph log(n) and log(p) times but the value returned by
is . fun1 is “q” which is calculated in the second loop.
15 So, n × (log(p)), [where p = log (n)] ⇒ n log (log
B E n)times.
4 Ans. (d)
1. Consider the following two statements. (a) R is symmetric and reflexive but not transitive
S1: If a candidate is known to be corrupt, then he (b) R is reflexive but not symmetric and not transitive
will not be elected (c) R is transitive but not reflexive and not symmetric
(d) R is symmetric but not reflexive and not transitive
S2: If a candidate is kind, he will be elected
Which one of the following statements follows from Solution: R is not reflexive since a and b are dis-
S1 and S2 per sound interference rules of logic? tinct as given in the question. Now, the given rela-
tion is true for a = 4 and b = 8; this is also true for
(a) If a person is known to be corrupt, he is kind
a = 8 and b = 4. Hence, it satisfies the condition
(b) If a person is not known to be corrupt, he is
for symmetric relation, that is, if (a,b) satisfies then
not kind
(b,a) should satisfy the relation.
(c) If a person is kind, he is not known to be corrupt
(d) If a person is not kind, he is not known to be R does not satisfy transitivity. For example, a = 5,
corrupt b = 10, c = 12. So, (5,10) satisfies the relation,
(10,12) also satisfies the relation, but (5,12) does
Solution: Let p: Candidate is known to be corrupt not. Hence, the relation is not transitive.
q: Candidate is kind Ans. (d)
r: Candidate is elected
4. The number of divisors of 2100 is .
S1: p → ~r => r → ~p
Solution:
S2: q → r
2100 = 22 + 3 × 52 × 7
Therefore, q → ~p, that is, if a candidate is kind,
he is not known to be corrupt. Number of divisors = (2 + 1) × (1 + 1) × (2 + 1) ×
Ans. (c) (1 + 1) = 36
Ans. (36)
2. The cardinality of the power set of {0, 1, 2, … 10}
is . 5. The larger of the two eigenvalues of the matrix
Solution: The cardinality of the power set of 4 5
{0, 1, 2, … , 10} is 2 number of elements in set, that is, 2 1 is .
6. An unordered list contains n distinct elements. The III. If L is a language in NP, L is Turing decidable.
number of comparisons to find an element in this Which of the above statements is/are true?
list that is neither maximum nor minimum is (a) Only II (b) Only III
(a) Θ(n log n) (b) Θ(n) (c) Only I and II (d) Only I and III
(c) Θ(log n) (d) Θ(1)
Solution: Turing decidable languages are recur-
Solution: In an unordered list, if we consider any sive languages. Recursive languages are closed
three elements, one of them would neither be maxi- under complement, that is, complement of every
mum or minimum. Hence, in constant time we can recursive language is recursive.
find the required element. Ans. (d)
Ans. (d)
11. Consider the following function written in the C
7. The minimum number of JK flip-flops required to programming language.
construct a synchronous counter with the count
sequence (0, 0, 1, 1, 2, 2, 3, 3, 0, 0, …….) is . void foo (char *a) {
if (*a && *a != ' ')
Solution: Number of distinct states = 4. Hence,
{
minimum flip-flops required is 2.
Ans. (2) foo (a+1);
putchar *a;
8. Assume that for a certain processor, a read request
takes 50 nanoseconds on a cache miss and 5 nano- }
seconds on a cache hit. Suppose while running a }
program, it was observed that 80% of the proces- The output of the above function on input “ABCD
sors read requests result in a cache hit. The aver- EFGH” is
age and access time in nanoseconds is . (a) ABCD EFGH (b) ABCD
Solution: Given that cache hit rate = 0.8, so (c) HGFE DCBA (d) DCBA
cache miss rate = 0.2 Solution: If condition fails when the space is
Time on cache miss = 50 ns and time on cache hit encountered, then output will be DCBA.
= 5 ns
Ans. (d)
So, average access time = 0.8(5) + 0.2(50) = 14 ns
Ans. (14) 12. Consider a complete binary tree where the left and
the right subtrees of the root are max-heaps. The
9. A computer system implements a 40-bit virtual lower bound for the number of operations to con-
address, page size of 8 kilobytes, and a 128-entry vert the tree to a heap is
translation look-aside buffer (TLB) organized into
(a) Ω(log n) (b) Ω(n)
32 sets each having four ways. Assume that the
(c) Ω(n log n) (d) Ω(n2)
TLB tag does not store any process id. The mini-
mum length of the TLB tag in bits is . Solution: Left and right subtrees of binary tree are
max heaps. Thus, to convert the tree into heap we
Solution:
only need to compare the roots. This would always
40 be done on the number of levels which is log(n).
Ans. (a)
22 5 13 13. A binary tree T has 20 leaves. The number of nodes
in T having two children is .
TAG SET-OFFSET WORD-OFFSET
Page size = 8 KB = 210 × 23 = 213 ⇒ 13 byte Solution: The number of nodes with two children
word-offset. is always one less than the number of leaf nodes.
Number of sets = 32 = 25 ⇒ 5 bytes set-offset Here, Leaf nodes = 20 ⇒ Number of two children
nodes = 19.
Remaining bytes are for tag = 40 − 18 = 22 Ans. (19)
Ans. (22)
14. Consider the following C function.
10. Consider the following statements
int fun(int n)
I. The complement of every Turing decidable
{
language is Turing decidable.
II. There exists some language which is in NP but int x=1, k;
is not Turing decidable. if (n==1) return x;
for (k=1; k<n; ++k) Register allocation can be used in graph coloring.
x = x + fun (k) * fun (n-k); Post-order traversal is used for expression
return x; evaluations.
} Ans. (c)
The return value of fun (5) is . 18. In the context of abstract-syntax-tree (AST) and
Solution: control-flow-graph (CFG), which one of the follow-
ing is TRUE?
For n = 1, f (n) = 1
n -1 (a) In both AST and CFG, let node, N2 be the
For n > 1, f (n) = 1 + ∑k
=1 f(k) f(n - k) successor of node N1. In the input program,
f(5) = 51 the code corresponding to N2 is present after
Ans. (51) the code corresponding in N1.
15. A software requirements specification (SRS) doc- (b) For any input program, neither AST nor CFG
ument should avoid discussing which one of the will contain a cycle.
following? (c) The maximum number of successors of a node
in an AST and a CFG depends on the input
(a) User interface issues program.
(b) Non-functional requirements (d) Each node is AST and CFG corresponds to
(c) Design specification atmost one statement in the input program.
(d) Interfaces with third party software
Solution: Option (a) is false in the case when
Solution: Design specifications are not discussed in CFG contains a cycle. Hence, option (b) is also
SRS document. Only design constraints are discussed false as it says CFG cannot contain cycle. Also,
Ans. (c) single block can contain multiple statements. Thus,
16. Consider two decision problems Q1, Q2 such that option (d) is also false.
Q1 reduces in polynomial time to 3-SAT and Ans. (c)
3 -SAT reduces in polynomial time to Q2. Then 19. Consider the basic COCOMO model where E is the
which one of following is consistent with the above effort applied in person-months, D is the develop-
statement? ment time in chronological months, KLOC is the
(a) Q1 is in NP, Q2 is NP hard estimated number of delivered lines of code (in
(b) Q1 is in NP, Q2 is NP hard thousands) and ab, bb, cb, db have their usual mean-
(c) Both Q1 and Q2 are in NP ings. The basic COCOMO equations are of the form
(d) Both Q1 and Q2 are NP hard
(a) E = ab (KLOC ) exp (bb ), D = cb (E ) exp (db )
Solution: 3-SAT is NPC problem. (b) D = ab (KLOC ) exp (bb ), E = cb (D ) exp (db )
Q1 ≤p 3-SAT and 3-SAT ≤p Q2 (c) E = ab exp (bb ), D = cb (KLOC ) exp (db )
If an NP problem A is polynomial time reducible to (d) E = ab exp (Db ), D = cb (KLOC ) exp (bb )
another NP problem B, then B is NPC. But if we
do not know the type of B problem, then B is said to Solution: Basic COCOMO model equations are of
be NP hard. Hence, Q1 is in NP and Q2 is NP hard. the form:
Ans. (a) Effort = ab(KLOC ) exp(bb)
17. Match the following: Development time = cb(Effort) exp(db)
Ans. (a)
(P) Lexical analysis (1) Graph coloring
20. A system has 6 identical resources and N processes
(Q) Parsing (2) DFA minimization
competing for them. Each process can request at
(R) Register allocation (3) Post-order traversal most 2 resources. Which one of the following values
(S) Expression (4) Production tree of N could lead to a deadlock?
evaluation
(a) 1 (b) 2
(a) P − 2, Q − 3, R −1, S − 4 (c) 3 (d) 4
(b) P − 2, Q − 1, R − 4, S − 3
(c) P − 2, Q − 4, R − 1, S − 3 Solution: No deadlock occurs when N = 1, N = 2
(d) P − 2, Q − 3, R − 4, S − 1 and N = 3. When N = 4, deadlock may occur as
two processes will get complete resources; and two
Solution: Lexical analyzer uses DFA. will wait for the completion of other two.
Parsing is done though production tree. Ans. (d)
21. Consider the following transaction involving two transmission delay, and that its propagation delay
bank accounts x and y. is the same as the data propagation delay. Also
read (x) ; x : = x — 50; write (x); read (y); assume that the processing delays at the nodes are
y: = y + 50; write (y) negligible. The efficiency of the stop-and-wait pro-
The constraint that the sum of the accounts x and tocol in this setup is exactly 25%. The value of
y should remain constant is that of the one-way propagation delay (in milliseconds) is
.
(a) Atomicity (b) Consistency
(c) Isolation (d) Durability 1 1
Solution: In stop and wait, efficiency = =
1 1 (1 + 2a) 4
Solution: Consistency means the overall state of = ⇒ a = 3/2
system should remain constant before and after the (1 + 2a) 4
transaction. Hence, the sum remains constant with 1000 × 8
Transmission time, Tt = = 8 ms
the property of consistency. 106
8
Ans. (b) Propagation time, Tp = a × Tt = 3 × = 12 ms
2
22. With reference to the B+ tree index of order 1 Ans. (12)
shown below, the minimum number of nodes
(including the Root node) that must be fetched 25. Which one of the following statements is NOT cor-
in order to satisfy the following query: “Get all rect about HTTP cookies?
records with a search key greater than or equal to (a) A
cookie is a piece of code that has the potential
7 and less than 15” is . to compromise the security of an internet user
(b) A cookie gains entry to the user’s work area
9
through an HTTP header
(c) A cookie has an expiry date and time
5 13 17 (d) Cookies can be used to track the browsing
pattern of a user at a particular site
1 9
Q. No. 26 – 55 Carry Two Marks Each
2 5 4 13 17
26. Consider the following routing table at an IP router:
(a) listen, accept, bind, recv For each IP address in Group I identify the correct
(b) bind, listen, accept, recv choice of the next hop from Group II using the
(c) bind, accept, listen, recv entries from the routing table above.
(d) accept, listen, bind, recv
Group I Group II
Solution: The correct order is bind, listen, accept,
recv. First three are used for connection establish- (i) 128.96.171.92 (a) Interface 0
ment and recv is used for data transfer operation. (ii) 128.96.167.151 (b) Interface 1
Ans. (b) (iii) 128.96.163.151 (c) R2
6
24. A link has a transmission speed of 10 bits/sec. (iv) 128.96.165.121 (d) R3
It uses data packets of size 1000 bytes each. (e) R4
Assume that the acknowledgement has negligible
30. Consider two relations R1(A,B) with the tuples 50 × 106 bytes/sec. if the average seek time of the
(1,5), (3,7) and R2 (A,C) = (1,7), (4,9). Assume disk is twice the average rotational delay and the
that R(A,B,C) is the full natural outer join of controller’s transfer time is 10 times the disk trans-
R1 and R2. Consider the following tuples of the fer time, the average time (in milliseconds) to read
form (A,B,C): a = (1,5,null), b = (1,null,7) c = or write a 512-byte sector of the disk is .
(3,null,9), d = (4,7,null), e = (1,5,7), f = (3,7,null),
g = (4,null,9).Which one of the following state- Solution:
ments is correct? RPM = 15000
(a) R contains a, b, e, f, g but not c, d. 15000 rotations ⇒ 60 sec
(b) R contains all of a, b, c, d, e, f, g 1 rotation ⇒ 4 ms = average seek time
(c) R contains e, f, g but not a, b Rotational latency = 4/2 = 2 ms
(d) R contains e but not f, g
Transfer rate = 50 × 106 bytes/sec
Solution: In option (c) 50 × 106 bytes ⇒ 1 sec
512
A B A C 512 bytes ⇒ = 0.01 ms ⇒ Disk transfer time
(50 × 106 )
R1 ⇒ 1 5 R2 ⇒ 1 7 time
3 7 4 9 Controller’s transfer time = 10 × (Disk
transfer
time) = 10 × 0.01 = 0.1 ms
A B C Average time = 4 + 2 + 0.1 = 6.1 ms
Ans. (6.1)
R⇒ 1 5 7
33. A computer system implements 8 kilobyte pages
3 7 Null
and a 32-bit physical address space. Each page
4 Null 9 table entry contains a valid bit, a dirty bit, three
permission bits, and the translation. If the maxi-
a = (1,5,null), b = (1,null,7) c = (3,null,9), d = (4,7,null), mum size of the page table of a process is 24 mega-
e = (1,5,7), f = (3,7,null), g = (4,null,9). bytes, the length of the virtual address supported
a, b, c and d are not the rows of relation R but e, f, g by the system is bits.
are the rows in relation R. Hence, option (c) is correct.
Solution: Page size = 8 KB = 213
Ans. (c) Physical address space = 32 bits
31. Consider six memory partitions of sizes 200 KB, Number of frames = 232/213 = 219 frames
400 KB, 600 KB, 500 KB, 300 KB and 250 KB,
Number of bits for page table entry = 19 + 1 V +
where KB refers to kilobyte. These partitions need
1 D + 3 P = 24 bits
to be allotted to four processes of sizes 357 KB,
210 KB, 468 KB and 491 KB in that order. If the Page table size = 24 MB = (24 × 220 × 8) bits
best fit algorithm is used, which partitions are = 24 × (Number of pages)
NOT allotted to any process?
So, number of pages = 223
(a) 200 KB and 300 KB (b) 200 KB and 250 KB Therefore, virtual address apace contains
(c) 250 KB and 300 KB (d) 300 KB and 400 KB (23 + 13) = 36 bits
Solution: Partitions needed for the processes = Ans. (36)
357 KB, 210 KB, 468 KB and 491 KB
34. Consider the intermediate code given below.
Using best fit strategy, the places allocated to the
processes are: (1) i = 1 (2) j = 1
357 KB 491 KB 468 KB 210 KB (3) t1 = 5 * i (4) t2 = t1+ j
(5) t3 = 4 * t2 (6) t4 = t3
200 400 600 500 300 250 (7) a[t4] = −1 (8) j = j + 1
(9) if j < = 5 goto (3) (10) i = i + 1
Thus, two memory slots are never used.
(11) if i < 5 goto (2)
Ans. (a)
The number of nodes and edges in the control-flow-
32. Consider a typical disk that rotates at 15000 rota- graph constructed for the above code, respectively,
tions per minute (RPM) and has a transfer rate of are
Ans. (3) 3 4 6 23
36. Which of the following languages is/are regular? 10 12 18 25
L1:{wxwR|w, x ∈ {a,b}* and |w|, |x| > 0}, wR is 31 ∞ ∞ ∞
the reverse of string w
When an element is removed from a Young tab- the array, and all elements greater than the pivot is
leau, other elements should be moved into its place in the right part. In addition, it moves the pivot so
so that the resulting table is still a Young tableau that the pivot is the last elements of the left part.
(unfilled entries may be filled in with a ∞). The The return value is the number of elements in the
minimum number of entries (other than 1) to be left part.
shifted, to remove 1 from the given Young tableau The following partially given function in the C pro-
is . gramming language is used to find the k th smallest
element in an array a [ ] of size n using the parti-
Solution: tion function. We assume k ≤ n.
int kth_smallest(int a[], int n, int k)
1 2 5 14 After removing ∞ 2 5 14
element 1 {
3 4 6 23 3 4 6 23 int left_end partition (a,n);
10 12 18 25 10 12 18 25 if (left_end + 1 == k)
{
31 ∞ ∞ ∞ 32 ∞ ∞ ∞
return a [left_end];
Intial state }
Shift element 2
if ((left_end + 1) > k)
with ∞
{
return kth_smallest (__________);
2 4 5 14 Shift 4 2 ∞ 5 14 }
with ∞ else
3 ∞ 6 23 3 4 6 23
return kth_smallest (__________);
10 12 18 25
10 12 18 25 }
31 ∞ ∞ ∞ 31 ∞ ∞ ∞ }
The pipelined processor uses operand forwarding Solution: F(A, B, C, D) = [D′ + AB ′+ A′C + AC ′D
from the PO stage to the OF stage. The number + A′C ′D]′
of clock cycles taken for the execution of the above
sequence of instructions is . CD
AB 00 01 11 10
Solution:
00 0 0 0 0
1 2 3 4 5 6 7 8 9 10 11 12 13
01 0 0 0 0
I I P P P W
11 0 0 1 0
O O - - P P P P P W
- - - - - -
I O P W 10 0 0 0 0
∫ f ( x ) dx = ∫ x dx + ∫x dx = 0
instruction is implemented as follows: −1 / 3 −1/ 3
Store the value of PSW register in the stack So, only statement II is true.
Load the starting address of the subroutine in PC Ans. (c)
The content of PC just before the fetch of a CALL 47. Perform the following operations on the matrix
instruction is (5FA0)16. After execution of the
CALL instruction, the value of the stack pointer is 3 4 45
7 9 105
(a) (016A)16 (b) (016C)16
(c) (0170)16 (d) (0172)16 13 2 195
Solution: (i) Add the third row to the second row
Given that memory is byte addressable. (ii) Subtract the third column from the first column.
Implementation of CALL instruction is as follows: The determinant of the resultant matrix is .
••Store the current value of PC in the stack:
Solution: After applying row and column opera-
Current stack pointer +2 = (016E)16 +2
tions, the resultant matrix is:
••Store the value of PSW register in the stack:
(016E)16 +2 +2 = =(0172)16 −42 4 45
Ans. (d) B = −280 11 300
45. The number of min-terms after minimizing the −182 2 195
following Boolean expression is —————.
|B| = 0
[D′ + AB ′ + A′C + AC ′D + A′C ′D]′ Ans. (0)
Solution: Strings formed by the given equations 54. Which one of the following assertions concerning
are represented by regular expression: 1(0 +10)*1. code inspection and code walkthrough is true?
Ans. (c)
(a) C
ode inspection is carried out once the code has
51. A graph is self-complementary if it is isomorphic to been unit tested
its complement. For all self-complementary graphs (b) Code inspection and code walkthrough are
on n vertices, n is synonyms
(c) Adherence to coding standards is checked adders and one OR gate. The propagation delay of
during code inspection an XOR gate is twice that of an AND/OR gate.
(d) Code walkthrough is usually carried out by an The propagation delay of an AND/OR gate is 1.2
independent test team microseconds. A 4-bit ripple-carry binary adder is
implemented by using four full adders. The total
Solution: Code inspection generally checks that propagation time of this 4-bit binary adder in
coding standards are followed or not. microseconds is .
Ans. (c)
Solution: Total delay = 4 × 4.8 = 19.2 ms
55. A half adder is implemented with XOR and AND
gates. A full adder is implemented with two half Ans. (19.2)
1. Consider the following C program segment. 2. Suppose U is the power set of the set S = {1, 2, 3, 4,
5, 6}. For any T ∈ U, let |T| denote the number of
#include <stdio.h>
elements in T and T1 denote the complement of T.
int main() For any T, R ∈ U, let T \R be the set of all
{ elements in T which are not in R. Which one of the
following is true?
char sl[7] = “1234”, *p;
p = sl + 2; (a) ∀X ∈ U (|X| = |X ′|)
(b) ∃X ∈ U ∃ Y ∈ U (|X | = 2, |Y | = 5 and X ∩ Y
*p = ‘0’;
= 0)
printf(“%s”, s1); (c) ∀X ∈ U ∀ Y ∈ U (|X | = 2, |Y | = 3 and X \Y
} = 0)
(d) ∀X ∈ U ∀ Y ∈ U (X\Y = Y ′\X ′)
What will be printed by the program?
(a) 12 (b) 120400 Solution:
(c) 1204 (d) 1034 S = {1, 2, 3, 4, 5, 6}
Let X = {1,2}
X ′ = S − X = {3, 4, 5, 6}
Solution: The given character array
representation:
|X | ≠ |X ′|
1 2 3 4 \0
100 101 102 103 Option (a) is false.
Let X = {1, 2, 4, 5, 6} and Y = {1, 2, 3, 4, 5}
s1: Base address = 100 X ∩ Y = {1, 2, 4, 5} ≠ ∅
p = 100 + 2 = 102
Option (b) is incorrect.
*p = 102 = 0
Let X = {1, 2} and Y = {2, 4, 5}
1 2 0 4 \0 X/Y = {1} ≠ ∅
100 101 102 103 Option (c) is incorrect.
Output will be: 1204 So, option (d) is the correct answer.
Ans. (c) Ans. (d)
8. In a web server, ten WebPages are stored with the (a) Only I
URLs of the form (b) Only II
(c) I or III or IV but not II
http://www.yourname.com/var.html; where, var is
(d) II or III or IV but not I
a different number from 1 to 10 for each Webpage.
Suppose, the client stores the Webpage with var
Solution:
= 1 (say W1) in local machine, edits and then
tests. Rest of the WebPages remains on the web
n2 (n + 1)2
server. W1 contains several relative URLs of the X = 13 + 23 + 33 + 43 + L + n3 =
form “var.html” referring to the other WebPages. 4
Which one of the following statements needs to
be added in W1, so that all the relative URLs This is equivalent to Q (n4), O(n5) and W(n3).
in W1 refer to the appropriate WebPages on the Ans. (c)
web server?
11. Consider a binary tree T that has 200 leaf nodes.
(a) <a href: “http://www.yourname.com/”, href: Then, the number of nodes in T that have exactly
“…var.html”> two children are .
(b) <base href: “http://www.yourname.com/”>
(c) <a href: “http://www.yourname.com/”> Solution:
(d) <base href: “http://www.yourname.com/”, Leaf (p) = 200
range “…var.html”> Total nodes = 2p − 1 = 399
Solution: Base tag with href is used: < base href :“ Nodes with degree exactly two children = 399 −
http : // www.yourname.com / ”, range :“...var. 200 = 199
html” > Ans. (199)
Ans. (b)
12. Given a hash table T with 25 slots that stores 2000
9. Consider the following statements elements, the load factor a for T is .
I. Q(n )
4
II. Q (n5) Apply AX = lX
III. O(n5) IV. Ω(n3) Thus, we get Eigen vectors as {a (−4,2,1)|a ≠ 0,
The equality above remains correct if X is a ∈ R}
replaced by Ans. (b)
(
14. The value of lim x →∞ 1 + x2 e−x is ) .
Option (c): If person is of Type 2, then he
is telling lie that toss is head. Option (c) is
1 incorrect.
(a) 0 (b)
2 Option (d): If person is of Type 1, then he is
(c) 1 (d) ∞ telling the truth that toss is head. Hence, this is
incorrect.
Solution: Ans. (a)
( )
lim x→∞ 1 + x2 e−x = (∞)0 = 1 17. While inserting the elements 71, 65, 84, 69, 67,
83 in an empty binary search tree (BST) in
the sequence shown, the element in the lowest
Ans. (c)
level is
15. The number of 4 digit numbers having their digits
(a) 65 (b) 67
in non-decreasing order (from left to right) con-
(c) 69 (d) 83
structed by using the digits belonging to the set
{1, 2, 3} is .
Solution: 67 is at the lowest level.
Total = 10 + 4 + 1 =15
Ans. (15) 67
16. In a room there are only two types of people, Ans. (b)
namely Type 1 and Type 2. Type 1 people always
18. The result evaluating the postfix expression
tell the truth and Type 2 people always lie. You
10 5 + 60 6 / * 8 - is
give a fair coin to a person in that room, without
knowing which type he is from and tell him to toss (a) 284 (b) 213
it and hide the result from you till you ask for it. (c) 142 (d) 71
Upon asking, the person replies the following
Solution:
“The result of the toss is head if and only if I am \
telling the truth.”
Which of the following options is correct? 6
(a) The result is head 5 60 60 10 8
(b) The result is tail 10 10 15 15 15 15 150 150 142
(c) If the person is of Type 2, then the result is tail
(d) If the person is of Type 1, then the result is tail 10 5 + 60 6 / ∗ 8 −
\
Ans. (c)
Solution: Type 1 people always tell the truth.
Type 2 people always lie. 19. Consider the following relation
Statement: “The result of the toss is head if and Cinema (theater, address, capacity)
only if I am telling the truth.”
Which of the following options will be needed at
Option (a): This statement extracts that the the end of the SQL query?
person is of Type 1 and the result is head.
SELECT P1. address
Option (b): We cannot draw conclusion about
the Type of person. So, it is incorrect. FROM Cinema P1
23. Consider a software program that is artificially Q. No. 26 — 55 Carry Two Marks Each
seeded with 100 faults. While testing this program,
159 faults are detected, out of which 75 faults are
from those artificially seeded faults. Assuming that 26. The velocity v (in kilometer/minute) of a motor-
both real and seeded faults are of same nature and bike which starts from rest, is given at fixed inter-
have same distribution, the estimated number of vals of time t(in minutes) as follows:
undetected real faults is .
t 2 4 6 8 10 12 14 16 18 20
Solution: v 10 18 25 29 32 20 11 5 2 0
Solution:
g(5) g(3)
F = P′ + QR = P′(Q + Q′) (R + R′) + (P+P′)QR
F = P′Q + P′Q′(R + R′) + PQR + P′QR
g(4) g(2) g(2) g(10)
F = P′QR + P′QR′ + P′Q′ R+ P′Q′R′+ PQR +
P′QR
g(3) g(1) g(1) g(−1) g(1) g(−1)
P QR
00 01 11 10
g(2) g(0) g(0) g(−2) g(0) g(−2)
0 1 1 1 1
1 0 0 1 0
g(1) g(−1)
F = ∑(0, 1, 2, 3, 7)
g(0) g(−2)
= ∏ (4, 5, 6)
Ans. (b)
S2 and S3 are true.
29. Consider a B+ tree in which the search key is Ans. (a)
12 bytes long, block size is 1024 bytes, record
pointer is 10 bytes long and block pointer is 8 31. Language L1 is polynomial time reducible to lan-
bytes long. The maximum number of keys that guage L2. Language L3 is polynomial time reducible
can be accommodated in each non-leaf node of to L2, which in turn is polynomial time reducible to
the tree is . language L4.
Solution: In B+ trees, if order of tree is p then for Which of the following is/are true?
internal nodes: I. if L4 ∈ P, then L2 ∈ P
p(child pointer) + (p − 1)(Key value) ≤ Block size II. if L1 ∈ P or L3 ∈ P, then L2 ∈ P
III. L1 ∈ P, if and only if L3 ∈ P
p(8) + (p − 1)12 ≤ 1024
IV. if L4 ∈ P, then L1 ∈ P and L3 ∈ P
20p − 12 ≤ 1024
(a) II only (b) III only
p ≤ 51 ⇒ p = 51 (c) I and IV only (d) I only
Solution: Given, L1 ≤P L2
So, for 51 order tree, we can save maximum
50 keys.
L3 ≤P L2
Ans. (50)
L2 ≤P L4
30. Given the function F = P′ + QR, where F is a If L4 is polynomial, then it implies that L2 is also
function in three Boolean variables P, Q and R and polynomial. Similarly, if L2 is polynomial this also
P′ = !P, consider the following statements. implies that L1 and L3 are also polynomial.
(S1) F = ∑ (4, 5, 6) Ans. (c)
32. Consider the following C program. p is a pointer to array. 1st element of p points to 10
#include <stdio.h> 2nd element points to 40
int f1(void); 3rd element points to 50
int f2(void); 4th element points to 20
int f3(void); 5th element points to 30
int x = 10; Thus, implementing the code gives us 140.
int main() Ans. (140)
{
34. Which of the following languages are context-free?
int x = 1;
L1 = {ambnanbm|m, n ≥ 1}
x + = f1() + f2() + f3() + f2();
printf (“%d”, x); L2 = {ambnambn|m, n ≥ 1}
return 0; L3 = {ambn|m = 2n + 1}
} (a) L1 and L2 only (b) L1 and L3 only
(c) L2 and L3 only (d) L3 only
int f1() {int x = 25; x++; return x;}
int f2() {static int x = 50; x++; return Solution: For identifying the context-free lan-
x;} guage, check whether the stack implementation is
int f3() {x *= 10; return x}; possible or not.
33. Consider the following C program. I. Processes should acquire all their resources at
the beginning of execution. If any resource is
#include <stdio.h> not available, all resources acquired so far are
int main() released.
{ II. The resources are numbered uniquely, and pro-
static int a[] = {10, 20, 30, 40, 50}; cesses are allowed to request for resources only
in increasing resource numbers
static int *p[] = {a, a+3, a+4, a+1,
III. The resources are numbered uniquely, and pro-
a+2};
cesses are allowed to request for resources only
int **ptr = p; in decreasing resource numbers
ptr++; IV. The resources are numbered uniquely. A pro-
printf(“%d%d”, ptr-p, **ptr); cess is allowed to request only for a resource
} with resource number larger than its currently
held resources
The output of the program is .
Which of the above policies can be used for pre-
Solution: a[] = {10,20,30,40,50} venting deadlock?
(a) Any one of I and III but not II or IV 38. Consider the following reservation table for a pipe-
(b) Any one of I, III, and IV but not II line having three stages S1, S2, and S3.
(c) Any one of II, and III but not I or IV
(d) Any one of I, II, III, and IV Time→
Solution: All the four methods can be used to pre- 1 2 3 4 5
vent deadlock in the system.
S1 X X
Ans. (d)
S1 X X
36. In the network 200.10.11.144/27, the fourth octet
(in decimal) of the last IP address of the network S3 X
which can be assigned to a host is .
The minimum average latency (MAL) is .
Solution:
IP address: 200.20.11.144 => 200.20.11.10010000 Solution: For S1 latency = 0 + 4
Subnet mask address: 225.225.225.11100000
For S2 latency = 1 + 3
Looking at the subnet address, we get to know that
For S3 latency = 2
1st three bits gives the subnet number and remain-
ing are for host. Average latency = 10/3 = 3.33
Thus, 100 10000 Ans. (3.33)
Last host bits will be: 100 11110 ⇒ 158 39. Consider the following code sequence having five
Ans. (158) instructions I1 to I5. Each of these instructions has
37. Consider a network connecting two systems located the following format.
8000 kilometers apart. The bandwidth of the net- OP Ri, Rj, Rk
work is 500 × 106 bits per second. The propagation where operation Op is performed on contents of
speed of the media is 4 × 106 meters per second. It registers Rj and Rk and the result is stored in
is needed to design a Go Back − N sliding window register Ri.
protocol for this network. The average packet size
is 107 bits. The network is to be used to its full I1: ADD R1, R2, R3
capacity. Assume that processing delays at nodes I2: MUL R7., R1, R3
are negligible. Then, the minimum size in bits of
the sequence number field has to be . I3: SUB R4, R1, R5
I4: ADD R3, R2, R4
Solution:
I5: MUL R7, R8, R9
Distance = 8000 km = 8 × 106 m
Speed = 4 × 106 m/s
Consider the following three statements.
The number of times printf statement is executed Solution: In a spanning tree containing 100 verti-
is . ces, number of edges will be 99.
Solution: j and k will be evaluated to be 2 and Therefore, weight is increased by = 500 + 5 × 99
−1, respectively. After getting in the loop, and = 995
implementing every iteration, the printf is called Ans. (995)
10 times.
46. Two hosts are connected via a packet switch with
Ans. (10) 107 bits per second links. Each link has a propaga-
1 1
tion delay of 20 microseconds. The switch begins
44. If for non-zero x, af (x) + bf = − 25 where forwarding a packet 35 microseconds after it receives
x x the same. If 10000 bits of data are to be transmitted
2
a ≠ b then ∫ f (x) dx is
between the two hosts using a packet size of 5000 bits,
the time elapsed between the transmission of the first
1
bit of data and the reception of the last bit of the data
in microseconds is .
1 47b
(a) a(ln 2 − 25) + 2
a −b
2 2
Solution: Transmission time of 5000 bits = 500
47b
1 microseconds
(b) a(2 ln 2 − 25) − 2
a2 − b2 Total transmission time of first bit = 500 + 20 +
35 + 20 = 575 microseconds
1 47b
a(2 ln 2 − 25) +
2
(c)
a −b
2 2 Total transmission time of last bit = 500 + 500 =
1000 microseconds
1 47b
(d) a(ln 2 − 25) − 2 Total time = 1000 + 575 = 1575 microseconds
a −b
2 2
Ans. (1575)
Solution:
47. For the processes listed in the following table,
1 which of the following scheduling schemes will give
af (x) + bf (1/x) = (1)
x − 25 the lowest average turnaround time?
3 + 14 + 4 + 4
Average turnaround time = = 6.25
4 Control Flow Diagram of Program — Z:
Similarly checking for other cases, we will find that
average turnaround time is minimum in SRTF. Control flow diagram of
Ans. (c) Program–X
48. Consider three software items: Program—X, Control
Flow Diagram of Program—Y and Control Flow
Diagram of Program—Z as shown below Control flow diagram of
Program–Y
Program X:
sumcal(int maxint, int value) The values of McCabe’s Cyclomatic complexity of
{ Program—X, Program—Y, and Program—Z, respec-
int result =0, i = 0; tively, are
if(value < 0) (a) 4, 4, 7
{ (b) 3, 4, 7
value = -value; (c) 4, 4, 8
} (d) 4, 3, 8
54. Let f (n) = n and g(n) = n(1 + sin n), where n is a 55. Consider the following grammar G
positive integer. Which of the following statements
S→FH
is/are correct?
F→pc
I. f (n) = O(g(n))
H→dc
II. f (n) = Ω(g(n))
where S, F, and H are non-terminal symbols, p, d,
(a) Only I and c are terminal symbols. Which of the following
(b) Only II statement(s) is/are correct?
(c) Both I and II S1: LL(1) can parse all strings that are generated
(d) Neither I nor II using grammar G
Solution: S2: LR(1) can parse all strings that are generated
using grammar G
f (n) = n and g(n) = n1 + sin n
(a) Only S1 (b) Only S2
Minimum value of g(n) = n1−1 = 1 (c) Both S1 and S2 (d) Neither S1 nor S2
Maximum value of g(n) = n2 Solution: When we try to parse the given gram-
mar with LL(1) parser or LR(1) parser, there
In either case, it is not equal to f (n). Hence, f (n)
occurs a conflict in both cases. Hence, it cannot be
can’t be best or worst case for g(n).
parsed with any of these parsers.
Ans. (d) Ans. (d)
mapping function, 74 LAN (local area network), 492—493 detection of, 346
set associative mapping, 75 network layer protocols, 501—505 prevention of, 341—342
write-back updating policies, 74 network security, 510—512 receovery from, 346—347
write-through policy, 74 presentation layer, 508 resource allocation graph, 340—341
Calling function, 120 routing algorithms, 498—499 Decimal number system, 3
Cascading Style Sheets (CSS), 541 session layer, 507 Decoder, 14
Central processing unit (CPU) sockets, 507 logic diagram of, 14
data paths, 64 topology, 492 Defect rate, 464
design, 64—65 Transmission Control Protocol Defect removal efficiency (DRE), 464
general register organization, 62 (TCP), 506—507 Degree of multiprogramming, 330
hardwired control unit, 64 transport layer, 505 Demultiplexer, 16
instruction execution, 63—64 User Datagram Protocol (UDP), 506 Derivation tree, 253
micro-programmed control unit, Computer organization, 57 D flip—flops, 17—18
64—65 Concatenation, 238 truth table of, 18
RISC vs. CISC processors, 65 Concurrency control protocols, 423—424 Digital circuits
single accumulator organization, 62 Consensus theorem, 7 classification of, 11
stack organization, 63 Constructive cost model (COCOMO), combinational circuits, 11—16
Chomsky normal form (CNF), 256 463—464 sequential circuits, 16—26
Class, 107 Context-free grammar, 252 Digital electronics, 3
Clear accumulator (CLA), 59 Context-free grammar simplification, Digital logic families, 26—27
Clear operation, 62 254—256 pros and cons of logic families, 27
Client—server computing, 544—546 Control flow statements Dijkstra’s algorithm, 200—201
functions of a client, 544 break statement, 110 Dining philosopher’s problem, 336
functions of a server, 544—545 conditional statements, 108—110 Direct memory access (DMA), 66—67
merits and demerits, 546 continue statement, 110 burst or block transfer mode, 66
N-tier architecture, 546 goto statement, 110 channel, 66
server types, 544 return statement, 110 controller, 66
three-tier architecture, 545—546 Control hazards, 69 cycle stealing mode, 66
topologies for, 545 Conversions of number system, 4 data transfer mode, 66
two-tier architecture, 545 decimal number system to any interconnection with memory, CPU
Clocked RS flip—flops, 17 number system, 4 and I/O devices, 67
logic diagram of, 17 number system to decimal number modes of operation, 66
Code optimization phase system, 4 Dispatcher, 330
primary source of, 307—308 Counters, 22—26 Do … while loop, 109
types and levels of, 307 Counting semaphores, 338—339 Duality theorem, 7
Column major order, 111 Counting sort Dynamic programming, 203—204
Combinational circuits, 11—16 pseudocode for, 193 applications of, 204—2057
decoder, 14 time complexities, 192
demultiplexer, 16 CPU scheduling algorithms, 331—335
encoder, 14 Critical section problem, 337 E
full adder, 12 Cryptography, 510—511
Early binding, 118—119
full subtractor, 13
Else—if condition statement, 109
half adder, 11—12
Encapsulation, 107
half subtractor, 12
D Encoder, 14
look-ahead carry adder, 13—14
Database design Enumerated data types, 117
multiplexer, 14
attribute closure, 411 Ethernet LAN technology, 492—493
parallel adder, 13
decomposition process, 412—414 Even palindrome, 238
Compiler, 287
integrity constraints, 410—411 Expression conversion, 120
construction tools, 290
keys, 411—412 infix to postfix conversion, 120
grouping of phases, 290
process of normalization, 411 infix to prefix conversion, 121—122
phases of, 288—289
Database management system External interrupts, 65
Complement accumulator (CMA), 59
Computer architecture, 57 (DBMS), 403—404
quantitative principles to design architecture of, 405
high-performance processor, 58 components of, 404—405 F
registers and their functions, 57—58 Data models, 405 Fibonacci series, 204
Computer networks ER model, 407—410 File processing approach, 403—404
application layer, 508—509 relational model, 406—407 File structures
congestion and its causes, 505—506 Data path structures of CPU, 64 B trees, 416
connections, 491 single bus structure, 64 B+ tree structure, 416—417
devices used for internetworking, 509 three bus structure, 64 indexing attributes, 416
digital signature, 511 two bus structure, 64 sequential files, 416
firewalls, 511—512 Data types in programming language, File system
International Standards 107—108 allocation methods, 359—362
Organization/Open Systems Deadlocks, 339 directory structures, 359
Interconnection (Reference Model avoidance of, 342—343 Finite automata, 237—238
1984) (ISO/OSI) model, 494—498 characteristics of, 340 conversion in, 246—249
Producer—consumer problem, 336 Rightmost derivation (RMD) tree, 253 Sorting, 186
Product-of-sums (POS) form, 8 Ring counter, 26 Spinning or busy waiting, 337
using K-map, 9 Ripple carry adder, 13 Stack, 119
Programmed I/O, 65 Risk analysis applications of, 120
Programming languages, 105—106 identification of risk, 466 POP operation on, 119—120
fifth generation /5GL/(natural risk projection or risk estimation, 466 PUSH operation on, 119
language programming), 106 risk strategies, 466 Stack pointer (SP), 63
first generation/1GL/(machine types of risk, 466 Standard context-free language,
level programming), 106 Row major order, 111 252—253
fourth generation /4GL/(non- RS flip—flops, 16—17 Storage classes, 118
procedural programming), 106 clocked, 17 String of a language, 238
second generation /2GL/(assembly logic diagram of, 17 Structured programming, 107
level programming), 106 truth table of, 17 Structured query languages (SQL), 414
third generation /3GL/(high-level Runtime environment commands, 414—416
programming), 106 activation record and activations Structures, 117
Pumping lemma, 244—245 trees, 304 Substring, 238
Pushdown automata (PDA) lexical vs dynamic scoping, 305 Suffix of a string, 239
deterministic (NPDA), 251 procedure call return model, 304—305 Sum-of-products (SOP) form, 8
model of, 250—251 storage organization, 303—304 using K-map, 9
non-deterministic (NPDA), 251 symbol table, 305 Switch condition statement, 109
transition function of, 251 Synchronous counter, 22
Synchronous interrupts, 66
Syntax-directed translation (SDT)
S applications of, 302
Q Schedule, 417—419 attributes supported by, 302
Quads, 9 based on recoverability, 419 characteristics, 302
Queue, 122 based on serializability, 420—423 types of, 302
applications of, 123 Scoping, 117
basic operations, 122—123 Scripting languages, 106
types of, 123 Searching, 186
Quick sort Security of computers, 368 T
pseudocode for, 192 from accidental data loss, 369 T flip—flops, 19
randomized version of, 193 anti-virus approaches, 371 truth table, 19
recurrence relation for, 192 attacks from outside the system, Threads
time complexities, 192 370—371 benefits of, 347
attacks from within the system, 370 types of, 347—348
cryptography, 369 Three-variable K-map, 9
from threats, 369 Token bus and ring, 493
R Selection sort Transactions, 417
Race-around problem, 18 pseudocode for, 187 Trees
Race condition, 336—337 time complexities, 188 applications, 126
Radix complement, 4 Selective clear operation, 62 binary, 125—126
subtraction using, 6 Selective complement operation, 62 properties of, 125
Radix minus one complement, 4 Selective set operation, 62 Truth table, 12—14
subtraction using, 4—5 Semaphores, 338 Turing machine (TM), 256—258
Radix or base, 4 Sequential circuits, 16—26 Two-variable K-map, 9
Read after write (RAW), 68 Serial-in/serial-out shift register, 20—21
Readers—writers problem, 336 Serial-in to parallel-out (SIPO), 21
Recurrence relations Shift left register, 21 U
iteration method, 180 Shift register counter, 26
Unions, 117
master theorem, 181 Shift right register, 20
User-defined function, 112
recursion tree, 181 Sockets, 507
Recursion, 112 Software
advantages, 113 architecture, 472
function, 112—113 characteristics of, 456 V
Recursive and recursive enumerable coding, 472 Vectored interrupts, 65
languages, 258 design stage, 470—472 Virtual memory, 355
Recursive function, 120 engineering, 457
Registers, 20 interrupts, 65
Regular sets/languages, properties testing, 472—477
of, 249 Software Development Life Cycle (SDLC) W
Relative address mode, 60 elicitation techniques, 469—470 Web technology
Return statement, 112 external interfaces, 468 Cascading Style Sheets (CSS), 541
Reversal of a string, 238 requirement, 467—468 Document Object Model (DOM), 543
Reversing a list, 120 requirement engineering, 468—469 flash, 543