ComPUTeR ScIence
TEXTBOOK FOR CLASS XII
            2024-
            25
 12130 – COMPUTER SCIENCE
 Textbook for Class XII                                              ISBN 978-93-5292-338-0
First Edition
September 2020 Bhadrapada 1942                                 ALL RIGHTS RESERVED
                                         No part of this publication may be reproduced, stored in a
                                          retrieval system or transmitted, in any form or by any
Reprinted                                 means,      electronic,     mechanical,    photocopying,
                                          recording or otherwise without the prior permission of
September 2021 Bhadrapada 1943            the publisher.
December 2021 Agrahayana 1943            This book is sold subject to the condition that it shall
                                          not, by way of trade, be lent, re-sold, hired out or
October 2022 Kartika 1944                 otherwise disposed off without the publisher’s consent,
March 2024 Chaitra 1946                   in any form of binding or cover other than that in which
                                          it is published.
                                         The correct price of this publication is the price printed on
                                          this page. Any revised price indicated by a rubber
                                          stamp or by a sticker or by any other means is
                                          incorrect and should be unacceptable.
PD 25T SU
                                            OFFICES OF THE PUBLICATION
                                            DIVISION, NCERT
© National Council of Educational               NCERT Campus
Research and Training, 2020                     Sri Aurobindo
                                                Marg
                                                New Delhi 110 016             Phone : 011-26562708
                                                108, 100 Feet Road
                                                Hosdakere Halli
                                                Extension
                                                Banashankari III Stage
                                                Bengaluru 560 085             Phone : 080-26725740
                                                Navjivan Trust Building
                                                P.O.Navjivan
                                                Ahmedabad 380 014             Phone : 079-27541446
                                                CWC Campus
                                                Opp. Dhankal Bus Stop
                                                Panihati
 250.00                                         Kolkata 700 114               Phone : 033-25530454
                                                CWC Complex
                                                Maligaon
                                                Guwahati 781 021              Phone : 0361-2674869
                                         Publication Team
                                         Head, Publication                 : Anup Kumar Rajput
                                         Division
Printed on 80 GSM paper with NCERT
watermark                                Chief Editor                      : Shveta Uppal
Published at the Publication Division    Chief Production Officer          : Arun Chitkara
by the Secretary, National Council of
                                         Chief Business                    : Amitabh Kumar
Educational Research and Training,
                                         Manager (In charge)
Sri Aurobindo Marg, New Delhi 110 016
and printed at Shree Vrindavan           Assistant Production              : Sunil Kumar
Graphics                                 Officer
(P) Ltd., E-34, Sector-7, Noida 201
301 Uttar Pradesh                                                  Cover and Layout
                                                    Meetu Sharma, DTP Operator, DESM
                                        2024-
                                        25
      FOReWORD
Computer science as a discipline has evolved over the years and has
emerged as a driving force of our socio-economic activities. It has made
continuous inroads into diverse areas — be it business, commerce, science,
technology, sports, health, transportation or education. With the advent
of computer and communication technologies, there has been a paradigm
shift in teaching-learning at the school level. The role and relevance of this
discipline is in focus because the expectations from the school pass-outs
have grown to be able to meet the challenges of the 21st century. Today, we
are living in an interconnected world where computer-based applications
influence the way we learn, communicate, commute or even socialise!
    There is a demand for software engineers in various fields like
manufacturing, services, etc. Today, there are a large number of
successful startups delivering different services through software
applications. All these have resulted in generating interest for this subject
among students as well as parents.
    Development of logical thinking, reasoning and problem-solving skills
are fundamental building blocks for knowledge acquisition at the higher
level. Computer plays a key role in problem solving with focus on logical
representation or reasoning and analysis.
    This textbook focuses on the fundamental concepts and
problem-solving skills while opening a window to the emerging and
advanced areas of computer science. The newly developed syllabus
has dealt with the dual challenge of reducing curricular load as well as
introducing this ever evolving discipline. This textbook also provides space
to Computational Thinking and Artificial Intelligence, which envisaged in
National Education Policy, 2020.
    As an organisation committed to systemic reforms and continuous
improvement in the quality of its products, NCERT welcomes comments and
suggestions which will enable us to revise the content of the textbook.
                                                       HRUSHIKESH SENAPATY
                                                                    Director
New Delhi                                   National Council of Educational
August 2020                                         Research and Training
                                    2024-
                                    25
2024-
25
       PRefACe
In the present education system of our country, specialised or discipline
based courses are introduced at the higher secondary stage. This stage
is crucial as well as challenging because of the transition from general
to discipline-based curriculum. The syllabus at this stage needs to have
sufficient rigour and depth while remaining mindful of the comprehension
level of the learners. Further, the textbook should not be heavily loaded
with content.
     Computers have permeated in every facet of life. Study of basic concepts
 of computer science has been desirable in education. There are courses
                 offered in the name of Computer Science, Information and
     Communication Technology (ICT), Information Technology (IT), etc., by
       various boards and schools up to secondary stage, as optional. These
   mainly focus on using computer for word processing, presentation tools
                                                   and application software.
    Computer Science (CS) at the higher secondary stage of school education
is also offered as an optional subject. At this stage, students usually opt
for CS with an aim of pursuing a career in software development or related
areas, after going through professional courses at higher levels. Therefore,
at higher secondary stage, the curriculum of CS introduces basics of
computing and sufficient conceptual background of Computer Science.
    The primary focus is on fostering the development of computational
thinking and problem-solving skills. This book has 13 chapters covering
the following broader themes:
   •   Data Structure: understanding of important data structure Stack,
       Queue; Searching and Sorting techniques.
   •   Database: basic understanding of data, database concepts, and
       relational database management system using MySQL. Structured
       query language — data definition, data manipulation and data
       querying.
   •   Programming: handling errors and exceptions in programs written
       in Python; handling files and performing file operations in Python.
   •   Network and Communication: fundamentals of Computers
       networks, devices, topologies, Internet, Web and IoT, DNS. Basics
       of Data communication — transmission channel, media; basics of
       protocols, mobile communication generations.
   •   Security Aspects: introduction to basic concepts related to network
       and Internet security, threats and prevention.
    Each chapter has two additional components — (i) activities and (ii)
think and reflect for self assessment while learning as well as to generate
further interest in the learner. A number of hands-on examples are given to
gradually explain methodology to solve different types of problems across
the Chapters. The programming examples as well as the exercises in the
                                   2024-
                                   25
                                    vi
chapters are required to be solved in a computer and verify with the given
outputs.
    Box items are pinned inside the chapters either to explain related
concepts or to describe additional information related to the topic covered
in that section. However, these box-items are not to be assessed through
examinations.
    Project Based Learning given as the end includes exemplar projects
related to real-world problems. Teachers are supposed to assign these or
similar projects to be developed in groups. Working in such projects may
promote peer-learning, team spirit and responsiveness.
       The chapters have been written by involving practicing teachers
as well as subject experts. Several iterations have resulted into this
book. Thanks are due to the authors and reviewers for their valuable
contribution. I would like to place on record appreciation for Professor
Om Vikas for leading the review activities of the book as well as for his
guidance and motivation to the development team throughout. Comments
and suggestions are welcome.
New Delhi                                         Rejaul Karim Barbhuiya
20 August 2020                                         Assistant Professor
                                                        Central Institute of
                                                   Educational Technology
                                  2024-
                                  25
       TexTBOOk DevelOPMenT COMMITTee
CHIEF ADVISOR
Om Vikas, Professor (Retd.), Former Director, ABV-IIITM, Gwalior, M.P.
MEMBERS
Anju Gupta, Freelance Educationist, Delhi
Anuradha Khattar, Assistant Professor, Miranda House, University of Delhi
Chetna Khanna, Freelance Educationist, Delhi
Faheem Masoodi, Assistant Professor, Department of Computer Science,
University of Kashmir
Harita Ahuja, Assistant Professor, Acharya Narendra Dev College, University
of Delhi
Mohini Arora, HOD, Computer Science, Air Force Golden Jubilee Institute,
Subroto Park, Delhi
Mudasir Wani, Assistant Professor, Govt. College for Women Nawakadal,
Sri Nagar, Jammu and Kashmir
Naeem Ahmad, Assistant Professor, Madanapalle Institute of Technology
and Science, Madanapalle, Andhra Pradesh
Purvi Kumar, Co-ordinator, Computer Science           Department,    Ganga
International School, Rohtak Road, Delhi
Priti Rai Jain, Assistant Professor, Miranda House, University of Delhi
Sangita Chadha, HOD, Computer Science, Ambience Public School,
Safdarjung Enclave, Delhi
Sharanjit Kaur, Associate Professor, Acharya Narendra Dev College,
University of Delhi
MEMBER-COORDINATOR
Rejaul Karim Barbhuiya, Assistant Professor, CIET, NCERT, Delhi
                                   2024-
                                   25
      AcknOWLeDGemenTs
The National Council of Educational Research and Training
acknowledges the valuable contributions of the individuals and
organisations involved in the development of Computer Science textbook
for Class XII.
    The Council expresses its gratitude to the syllabus development team
including MPS Bhatia, Professor, Netaji Subhas Institute of Technology,
Delhi;
T. V. Vijay Kumar, Professor, School of Computer and Systems Sciences,
Jawaharlal Nehru University, New Delhi; Zahid Raza, Associate Professor,
School of Computer and Systems Sciences, Jawaharlal Nehru University,
New Delhi; Vipul Shah, Principal Scientist, Tata Consultancy Services,
and the CSpathshala team; Aasim Zafar, Associate Professor, Department
of Computer Science, Aligarh Muslim University, Aligarh; Faisal Anwer,
Assistant Professor, Department of Computer Science, Aligarh Muslim
University, Aligarh; Smruti Ranjan Sarangi, Associate Professor,
Department of Computer Science and Engineering, Indian Institute of
Technology, Delhi; Vikram Goyal, Associate Professor, Indraprastha
Institute of Information Technology (IIIT), Delhi; and Mamur Ali, Assistant
Professor, Department of Teacher Training and Non-formal Education
(IASE), Faculty of Education, Jamia Millia Islamia, New Delhi.
    The Council is thankful to the following resource persons for
providing valuable inputs in developing this book — Veer Sain Dixit,
Assistant Professor, Atma Ram Sanatan Dharma College, University of
Delhi; Mukesh Kumar, DPS RK Puram, Delhi; Aswin K. Dash, Mother’s
International School, Delhi; Anamika Gupta, Assistant Professor, Shaheed
Sukhdev College of Business Studies, University of Delhi, Sajid Yousuf
Bhat, Assistant Professor, University of Kashmir, Jammu and Kashmir.
    The council is grateful to Prof. Sunita Farkya, Head, Department of
Education in Science and Mathematics, NCERT and Prof. Amarendra P.
Behera, Joint Director, CIET, NCERT for their valuable cooperation and
support throughout the development of this book.
       The Council also gracefully acknowledges the contributions of Meetu
Sharma, Graphic Designer cum DTP Operator; Kanika Walecha, DTP Operator;
Pooja, Junior Project Fellow; in shaping this book. The contributions of the
office of the APC, DESM and Publication Division, NCERT, New Delhi, in
bringing out this book are also duly acknowledged.
    The Council also acknowledges the contribution of Ankeeta Bezboruah
Assistant Editor (Contractual) Publication Division, NCERT for copy editing
this book. The efforts of Naresh Kumar, DTP Operator (Contractual),
Publication Division, NCERT are also acknowledged.
                                   2024-
                                   25
     COnTenTs
FOREWORD                                                      iii
PREFACE                                                        v
CHAPTER 1 EXCEPTION HANDLING            IN   PYTHON            1
           1.1    Introduction                                 1
           1.2    Syntax Errors                                1
           1.3    Exceptions                                   3
           1.4    Built-in Exceptions                          3
           1.5    Raising Exceptions                           4
           1.6    Handling Exceptions                          7
           1.7    Finally Clause                              13
CHAPTER 2 FILE HANDLING          IN PYTHON                    19
           2.1    Introduction to Files                       19
           2.2.   Types of Files                              20
           2.3    Opening and Closing a Text File             21
           2.4    Writing to a Text File                      23
           2.5    Reading from a Text File                    25
           2.6    Setting Offsets in a File                   28
           2.7    Creating and Traversing a Text File         29
           2.8    The Pickle Module                           32
CHAPTER 3 STACK                                               39
           3.1    Introduction                                39
           3.2    Stack                                       40
           3.3    Operations on Stack                         42
           3.4    Implementation of Stack in Python           43
           3.5    Notations for Arithmetic Expressions        46
           3.6    Conversion from Infix to Postfix Notation   47
           3.7    Evaluation of Postfix Expression            49
CHAPTER 4 QUEUE                                               53
           4.1 Introduction to Queue                          53
           4.2 Operations on Queue                            55
                                   2024-
                                   25
                                   x
         4.3 Implementation of Queue using Python              56
         4.4 Introduction to Deque                             59
         4.5 Implementation of Deque Using Python              61
CHAPTER 5 SORTING                                              67
         5.1   Introduction                                    67
         5.2   Bubble Sort                                     68
         5.3   Selection Sort                                  71
         5.4   Insertion Sort                                  74
         5.5   Time Complexity of Algorithms                   77
CHAPTER 6 SEARCHING                                            81
         6.1   Introduction                                    81
         6.2   Linear Search                                   82
         6.3   Binary Search                                   85
         6.4   Search by Hashing                               90
CHAPTER 7 UNDERSTANDING DATA                                   97
         7.1     Introduction to Data                          97
         7.2     Data Collection                              101
         7.3     Data Storage                                 102
         7.4     Data Processing                              102
         7.5     Statistical Techniques for Data Processing   103
CHAPTER 8 DATABASE CONCEPTS                                   111
         8.1     Introduction                                 111
         8.2     File System                                  112
         8.3     Database Management System                   115
         8.4     Relational Data Model                        120
         8.5     Keys in a Relational Database                123
CHAPTER 9 STRUCTURED QUERY LANGUAGE (SQL)                     131
         9.1   Introduction                                   131
         9.2   Structured Query Language (SQL)                131
         9.3   Data Types and Constraints in MySQL            133
         9.4   SQL for Data Definition                        134
         9.5   SQL for Data Manipulation                      141
         9.6   SQL for Data Query                             144
         9.7   Data Updation and Deletion                     154
         9.8   Functions in SQL                               156
         9.9   GROUP BY Clause in SQL                         167
                               2024-
                               25
                                  xi
          9.10 Operations on Relations                            169
          9.11 Using Two Relations in a Query                     172
CHAPTER 10 COMPUTER NETWORKS                                      181
          10.1   Introduction to Computer Networks                181
          10.2   Evolution of Networking                          183
          10.3   Types of Networks                                184
          10.4   Network Devices                                  187
          10.5   Networking Topologies                            191
          10.6   Identifying Nodes in a Networked Communication   194
          10.7   Internet, Web and the Internet of Things         195
          10.8   Domain Name System                               197
CHAPTER 11 DATA COMMUNICATION                                     203
          11.1   Concept of Communication                         203
          11.2   Components of data Communication                 204
          11.3   Measuring Capacity of Communication Media        205
          11.4   Types of Data Communication                      206
          11.5   Switching Techniques                             208
          11.6   Transmission Media                               209
          11.7   Mobile Telecommunication Technologies            215
          11.8   Protocol                                         216
CHAPTER 12 SECURITY ASPECTS                                       223
          12.1   Threats and Prevention                           223
          12.2   Malware                                          224
          12.3   Antivirus                                        230
          12.4   Spam                                             231
          12.5   HTTP vs HTTPS                                    231
          12.6   Firewall                                         232
          12.7   Cookies                                          233
          12.8   Hackers and Crackers                             234
          12.9   Network Security Threats                         235
CHAPTER 13 PROJECT BASED LEARNING                                 241
         13.1 Introduction                                        241
         13.2 Approaches for Solving Projects                     242
         13.3 Teamwork                                            243
         13.4 Project Descriptions                                245
                                2024-
                                25
2024-
25