Saturday 6 November 2021

UNIT 02: BASICS OF PROGRAMMING IN C++ - Text Book Exercise Ans SLOs Question Answers - Computer Science For Class X

GO TO INDEX

Computer Science For Class X
Unit 02: Basics Of Programming In C++
Text Book Exercise Ans SLOs Question Answers

B. RESPOND THE FOLLOWING:

Q.1: What is computer program?
Ans: COMPUTER PROGRAM:
A computer program is a set of instructions that performs a specific task when executed by a computer. A computer requires programs to function and typically executes the program's instructions in a central processing unit. A computer program is usually written by a computer programmer in a programming language.

Q.2: List five common high level languages used and describe their purposes.
Ans: HIGH LANGUAGES WITH THEIR PURPOSES:
S.NO. Languages Purpose
1. Python Artificial Intelligence & Machine Learning
2. JavaScript  Rich Interactive Web Development
3. Java Enterprise Application Development
4. C / C++ Operating Systems and System Tools
5. Golang Server-Side Programming
6. C# Application & Web Development

Q.3: Using the rules of naming variable, develop ten meaningful and valid variable names.
Ans: VARIABLE NAMES:
S.NO. Variable Names S.NO. Variable Names
1. num16. _auto
2. num_27. Sum
3. average8. length
4. discount_rate9. percentage
5. Obtained_marks10. height

Q.4: Write differences between machine and assembly languages.
Ans: DIFFERENCE BETWEEN MACHINE AND ASSEMBLY LANGUAGE:
Assembly language Machine language
 Assembly language is only understandable by human being. Machine language is only understandable by computers.
 Modifications and error fixing can be done in assembly language. Modifications and error fixing cannot be done in machine language.
 It is easy to memorize the assembly language because some alphabets and mnemonic are used. Machine language is very difficult to memorize due to the use of binary format (0s and 1s).
 Execution is slow as compared to machine language. Execution is fast in machine language because all data is already present in binary format.

Q.5: What are strings in C++?
Ans: STRINGS IN C++:
Variables that can store alphanumeric value that consist of multiple characters are called strings. In C++ strings are used by one-dimensional array of characters, which is terminated by a null character \0.

Q.6: What are the differences between declaring and initializing variables?
Ans: DECLARATION (CREATING) VARIABLES:
Variable declaration is a process in which we create storage space for variable in memory. A variable declaration consists of data type and name of the variable written as follow:
  • data_type variable_name;
  • Int sum;

INITIALIZATION:
Assign initial value to a variable is known as variable initialization. It can be initialized during declaration or separately. The equal sign is used to assign value written as follows:
  • data_type variable_name = value;
  • Int sum = 3;

Q.7: List any three advantages of using an IDE.
Ans: ADVANTAGES OF INTEGRATED DEVELOPMENT ENVIRONMENT (IDE):
  • IDE combines all tools that need for development. Programmers don't need to switch between different tools to design a layout, write the code, debug, build, etc.
  • Many IDEs incorporate basic spelling checkers, so automatically checked for errors to improve code.
  • Libraries provide for functions in IDEs that are not included in the core part of the programming language.

Students Learning Outcomes (SLOs) - Question Answers
By Mrs. Ayesha Arif
Vice Principal
(Jauhar Progressive School)

Q.1: Define Computer program OR Explain the concept of Computer program and programming language.
Ans: COMPUTER PROGRAM:
The set of instructions given to the computer are known as a computer program or Software, and the process of feeding or storing these instructions in the computer is known as computer programming.

PROGRAMMER:
The person who knows how to write a computer program correctly is known as a programmer.

PROGRAMMING LANGUAGES:
Computer cannot understand English, Urdu or any other common language that humans use for interacting with each other. They have their own special languages, designed by computer programmers. Programmers write computer programs in these special languages called programming languages. Java, C, C++, C#, Python are some of the most commonly used programming languages.

Q.2: Define syntax in programming language and syntax error? Also Describe the importance of syntax in any programming language.
Ans: SYNTAX:
Each programming language has some codes and rules in to write an accurate program. This set of rules is known as syntax of the language.
Importance Of Syntax In Programming Language:
  • Syntax can be thought of as grammar of a programming language.
  • It enables the computer read a set of code.
  • Syntax is the set of characters and keywords that a computer is able to read, interpret and convert.
  • Syntax play an important role in the execution of programs in text-based programming languages.
  • Text-based computer languages are based on sequences of characters, while visual programming languages like Visual basic are based on the layout and connections between symbols (which may be textual or graphical).
  • Example:
    In C++, this syntax displays the message "Hello World" on the screen.
  • Different programming languages using different types of syntax.

Syntax Error:
While programming, if proper syntax or rules of the programming language are not followed, the program does not compiled. In this case, the compiler generates an error. This kind of errors is called syntax errors. it is very common to new programmers to make syntax-based mistakes.

Q.3a: Define programming language and explain their classification?  OR Classify different programming languages into High, middle and low-level languages on the basis of their characteristics.
Ans: Programming Language:
A set of rules that provides a way of telling a computer what operations to perform is called a Programming Language. A programming language is a type of software that enables a computer understand specific instructions given to complete a task.
There are the following types of programming languages based on the accessibility of hardware .
  1. Low-level language
  2. Middle level language
  3. High level language

1. Low Level Language:
Low-level language is a programming language that can directly access and communicate with the hardware. It is represented in 0 or 1 forms . There are two types of low level languages.

  • Machine –Level language:
    It is the first generation programming language .The lowest language in hierarchy is the machine language ,therefore it has direct access to hardware. It is also called binary language because it is is written in binary digits 0s and 1s, means every instruction in Machine language consists of a series of 0s and 1s (binary code) that a computer can understand and execute directly. It does not require translator.

  • Assembly Language:
    It is second generation programming language . It is above the machine language therefore it has lesser accessibility to hardware. It is easy to write and maintain by humans. In assembly language, the statements are written in symbolic codes (termed as mnemonics) that are easier for human to read and write as compared to machine language because they are in simple English Language.

2. Middle-Level Language:
They were special purpose middle-level languages that were developed in the past . They have now become obsolete and are not used anymore.

3. High Level Language:
High level languages are closer to human languages than low-level language and include statement like GOTO and PRINT which are regular words. They do not have direct access to hardware therefore they require a translator (compiler or interpreter). Unlike the assembly language, the program of high level languages do not have to be written for a particular computer, but it can be executed on any machine that has a compiler for that language. FORTRAN, COBOL, BASIC, PASCAL, ADA, C-Language ,JAVA are some high level language.

Q.3b: Write down advantages of high-level language?
Ans: ADVANTAGES OF HIGH-LEVEL LANGUAGE:
The high-level language is easy to read, write, and maintain as it is written in English like words.
The high-level language is portable as opposed to low-level languages; i.e., these languages are not dependent on the machine.

Q.4:Differentiate between high level and low level languages?
Ans: Difference Between High Level Language And Low Level Language
S.NO. High Level Language Low Level Language
1. It is programmer friendly language. It is a machine friendly language.
2. High level language is less memory efficient. Low level language is high memory efficient.
3. It is easy to understand. It is hard (tough) to understand.
4. It is simple to debug. It is complex to debug comparatively.
5. It is simple to maintain. It is complex to maintain comparatively.
6. It is portable. It is non-portable.
6. It can run on any platform. It is machine-dependent.
8. It needs compiler or interpreter for translation. It needs assembler for translation.
9. It is used widely for programming. It is not commonly used now-a-days in programming.

OR
Low-level language High-level language
 It is a machine-friendly language, i.e., the computer understands the machine language, which is represented in 0 or 1. It is a user-friendly language as this language is written in simple English words, which can be easily understood by humans.
 It requires the assembler to convert the assembly code into machine code. It requires the compiler or interpreter to convert the high-level language instructions into machine code.
 One type of machine code cannot run on all machines, so it is not a portable language.  The high-level code can be translated to required machine-code, so it is a portable language.
 It has direct access to memory. It is less memory efficient.
 Coding and maintenance are not easy in a low-level language. Coding and maintenance are easier in a high-level language.

Q.5: Describe translators ,their role and types in detail?
Ans: TRANSLATOR:
Computers only understand machine code (binary) which is difficult to read, write and maintain. Therefore Programmers prefer to use a variety of high and low-level programming languages. Language translators are used to translate high level language into machine language so that computer can act upon the instruction given to it.
A program written in any language is called as source code.A translator takes a program written in source language as input and converts it into a machine language as output. It also detects and reports the error during translation.

Roles of translators:
1.Translating a program into an equivalent machine language program.
2. Providing alert messages wherever the programmer does not follow the rules of syntax of source language.

Different Types of Translators:
There are three different types of translators
  1. Compiler
  2. Interpreter
  3. Assembler

1. Compiler:
A compiler is complex system software that automatically converts a program written in some high-level language into an equivalent low-level machine language. The compiler or the language processor converts the entire program into machine code before execution. This translated program can be used again and again without the need for recompilation from source code.
It converts the whole program in one session and reports errors detected after the conversion. An error report is often produced after the full program has been translated. Errors in the program code may cause a computer to crash. These errors can only be fixed by changing the original source code and compiling the program again.

2. Interpreter:
An interpreter is another type of translator that converts each statement of a program written in a high level language into machine code and executes it before translating the next statement of the source program. It differs from a compiler that translates the entire source program into object program without undergoing its execution.
It translates the code line by line and reports the error as soon as it is encountered during the translation process. With interpreter, it is easier to detect errors in source code than in a compiler. An interpreter is faster than a compiler as it immediately executes the code upon reading the code.

3. Assembler:
An assembler is a translator used to translate assembly language to machine language. It is like a compiler for the assembly language but interactive like an interpreter. An assembler translates assembly language code to an even lower-level language, which is the machine code. The machine code can be directly understood by the CPU.

Q.6: Differentiate between Assembler, compiler and interpreter? OR Distinguish among various type of translators.
Ans: Difference Between ASSEMBLER, INTERPRETER And COMPILER
S.NO. ASSEMBLER INTERPRETER COMPILER
1. Assembler converts the assembly code into the machine code. Interpreter converts source code into the intermediate form and then converts that intermediate code into machine language. Compiler converts the source code written by the programmer to a machine level language.
2. Assembler inputs source code. Interpreter takes single instruction as input. Compiler inputs is preprocessed source code.
3. The output of assembler is binary code. In case of interpreter, No intermediate object code is generated. The output of compiler is a mnemonic version of machine code.
4. GAS, GNU Python, Perl, VB, PostScript, LISP etc. C, C#, Java, C++
5. Debugging is difficult. Interpreter is good for fast debugging. Debugging is easy.
6. Assembler converts source code to an object code first then it converts the object code to the machine language with the help of linker programs. Interpreter scans and translates the program line by line to equivalent machine code. Compiler scans the entire program first before translating into machine code.
7. Assembler is less intelligent than a compiler. Interpreter needs less memory when compared to compiler. Interpreter is slower when compared to compiler. Compiler is more intelligent than assembler.

Q.7: Describe errors and its types? OR Differentiate between syntax, runtime and logical errors.
Ans: ERRORS:
Errors are the mistakes made by the programmer during program writing. A program runs perfectly for the first time and each program needs to be tested to ensure that it is correct and contains no errors. There are three types of errors generally encountered.
  • a) Syntax Error
  • b) Logical Error
  • c) Run Time Error

a. SYNTAX ERROR:
The syntax of a programming language is the set of rules to be followed when writing a program in that language. These rules are similar to the grammar rules of Urdu or English language. When a program violates these rules, computer generates a syntax error. Syntax must be correct before program execution.
Most frequent syntax errors are:
  • Missing Parenthesis (})
  • Printing the value of variable without declaring it
  • Missing semicolon

b. LOGICAL ERROR:
These errors are the most difficult ones to find, if a programmer enters a sign of multiply instead of addition at the time of making the program, the answer will not be correct because interpreter will not be able the detect these types of errors. A logical error will not stop the execution of the program but the result will not be accurate.

c. RUN TIME ERROR:
These types of errors stop the execution of the program. It may be due to entering the invalid data at the time of executing a program. For example, if a program is expecting the user to enter the numeric data and the user enters a string data, computer will display an error message.

Q.8: Write a detailed note on programming environment of C++? OR Discuss about Integrated development Environment (IDE) of C++ & Develop the understanding about functions of different components of IDE.
Ans: PROGRAMMING ENVIRONMENT OF C++:
In order to correctly perform any task, we need to have proper tools. Therefore Programmer needs proper tools for programming. A collection of all the necessary tools for programming makes up a Programming environment. It is essential to setup a programming environment before we start writing programs. It works as a basic platform for us to write and execute programs. C++ runs on lots of platform like Windows, Linux, Unix, Mac, etc.

Integrated Development Environment (IDE):
The Software that provides a programming environment to facilitate programmers in writing and executing computer programs is known as an Integrated Development Environment (IDE). An IDE has a graphical user interface (GUI), meaning that a user can interact with it using windows and buttons to provide input and get output. An IDE consists of tools that help a programmer throughout the phases of writing, executing and testing a computer program. This is achieved by combining text editors, compilers and debuggers in a single interface.

Benefits of Integrated Development Environments:
  • Serves as a single environment for most of a developer’s needs such as compilation, linking, loading, and debugging tools.
  • Code completion capabilities improve programming workflow.
  • Automatically checks for errors to ensure top quality code.
  • Refactoring capabilities allow developers to make comprehensive and mistake-free renaming changes.

Components of IDE:
IDEs increase programmer productivity by combining common activities of writing software into a single application: editing source code, building executable, and debugging. Some IDEs for C programming language are:

  • Editing Source Code:
    Writing code is an important part of programming .Editing source code feature is a text editor designed for writing and editing the source code. Text editors enhance or simplify the writing and editing of code. IDEs facilitate this process with features like syntax highlighting and autocomplete.

  • Syntax Highlighting:
    Syntax highlighting makes code easier to read by visually clarifying different elements of language syntax.In this feature visual cues. Keywords, words that have special meaning are highlighted with different colors.

  • Code completion:
    Code completion features assist programmers by intelligently identifying and inserting common code components. These features save developers time writing code and reduce the chances of errors.

  • Compiler:
    Compilers translate programming language into machine language as binary code. IDEs provide automated build processes for languages, so the act of compiling and executing code is done automatically.

  • Linker:
    The linker opens the compiled program file and links it with the referenced library files as needed. Unless all linker items are resolved, the process stops and returns the user to the source code file within the text editor with an error message.

  • Loader:
    The IDE directs the operating system’s program called the loader to load the executable file into the computer’s memory and have the Central Processing Unit (CPU) start processing the instructions.

  • Debugging:
    Debugging is a step in which a programmer corrects a syntax error which may come after the compilation. When a program does not run correctly, IDEs provide debugging tools that allow programmers to examine different variables and inspect their code step by step.

Q.9: Define and differentiate between source program and object program?
Ans: SOURCE PROGRAM:
A program written by a programmer in a language other than machine language is called a Source Program.

OBJECT PROGRAM:
The output from a compiler or an assembler, which consists of machine language instructions, is called the Object Program.

DIFFERENCE BETWEEN SOURCE PROGRAM (CODE) AND OBJECT PROGRAM (CODE)
S.NO. SOURCE PROGRAM OBJECT PROGRAM
1. A collection of computer instructions written using a human readable programming language. A sequence of statements in binary digits that is generated after compiling the object program.
2. Contains English words according to syntax of programming language. Contains binaries.
3. Source code is another name for source program. Object code is another name for object program.
4. Programmers create the source program. Compilers create the object program.
5. It is the input to the computer. It is the output to the computer.
6. Programmers can read the source program Machines can read the object program.

Q.10: Write a short note on C++ programming and reserved words.
Ans: C++programming:
It is a general-purpose programming language that was created by Bjarne Stroustrup to make program writing easier and pleasant for programmers . It was an enhancement of the C language . It is a high-level language with an advantage of programming low- level (drivers, kernels) and even higher-level applications (games, GUI, desktop apps etc.). The basic syntax and code structure of both C and C++ are the same.

Reserved Words:
Every programming language has a list of words that are predefined. Each word has its specific meaning already known to the compiler. These words are known as reserved words or keywords. If a programmer gives them a definition of his own, it causes a syntax error. There is a total of 95 reserved words in C++. The reserved words of C++ may be conveniently placed into several groups. In the first group, we put those that were also present in the C programming language and have been carried over into C++. There are 32 of these.

Q.11: Describe the variables and constants in detail and Comprehend variable declaration rules in C++.
Ans: CONSTANTS:
Constants are those values that do not change while executing the program. Therefore the value is constant. Constants are used in two ways.
  1. Literal constant
  2. Defined constant

Literal constant:
It is a value typed into program wherever it is needed. Examples include the constants used for initializing a variable and constants used in lines of code:
21, 12.34, 'A', "Hello world! ", false, null

Defined constant:
There are some symbolic constants or named constants which are constants represented by name. The const keyword and #define preprocessor are used to define a constant. Many programming languages use ALL CAPS to define named constants like
const float PI = 3.14159; OR #define PI 3.14159.

VARIABLES:
Variables are the memory location that can hold a value. They are names used to represent values The values of a variable may be assigned by the programmer or it may be assigned as the result of calculation in the programme. Before a variable is assigned a value is assumed to be zero (or null for string variable).

Rules for Naming Variables:
  • The general rules for constructing names for variables are:
  • Names can contain letters, digits and underscores.
  • Names must begin with a letter or an underscore (_).
  • Names are case sensitive (myVar and myvar are different variables).
  • Names cannot contain whitespaces or special characters like !, #, %, etc.
  • Reserved words (like C++ keywords, such as int) cannot be used as names.
  • Names cannot be longer than 32 characters in C++ by default.

Declaring Variables:
In C++, there are different types of variables. A variable declaration tells the compiler where and how much storage is to be created for the variable. A variable declaration specifies a data type and name for that variable as follows:

  • Syntax:
    data_type variable_name;
    Where type is one of C++ data types (such as int), and variable_name is the name of the variable (such as x or myName).

Initializing variables:
Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression as follows:

  • Syntax:
    data_type variable_name = value;
    The equal sign is used to assign values to the variable.

Strings in C++
Variables that can store non-numeric values that are longer than one single character are known as strings.
The C++ language library provides support for strings through the standard string class. This is not a fundamental type, but it behaves in a similar way as fundamental types do in its most basic usage. Strings can be declared without an initial value and can be assigned values during execution.

Q.12: List any five high level languages and describe their purpose.
Ans: Common High Level Languages:
  1. Python
  2. Java
  3. C++
  4. C#:
  5. JavaScript:
  6. Pasacl
  7. COBOL
  8. FORTRAN
  9. BASIC

Python:
Python is used for Data Analytics, Machine Learning, AI Programming, and many other tasks
Uses in Popular Apps: Google, Spotify, Instagram

Java:
Java Language is Mostly used for Mobile Application Development
Some popular uses of Java are: Facebook, Messenger, Whatsapp

C / C++:
C is general purpose language that also works well with microcomputers. It is useful for developing operating systems, spreadsheets programs, database programs, Browsers. C++ is a version of C that incorporates object-oriented technologies. Some Popular uses are: Windows, Mozilla Firefox

C#:
C# (C-sharp) is Mostly used in Developing Games and Web Applications Some Popular Uses are: Temple Run, Pubg

JavaScript:
Used in Developing Websites Some Popular Uses are: Netflix, Facebook Web

Pascal:
A language that is widely used on microcomputers and easy to learn is Pascal, named after Blaise Pascal, a French mathematician. This language has become quite popular in computer science educational programs.

COBOL:
COBOL which stands for Common Business-Oriented Language is one of the most frequently used programming languages in business. Though harder to learn than BASIC its logic is easier to understand for a person who is not a trained programmer.

FORTRAN:
Short for Formula Translation, FORTRAN is a widely used scientific and mathematical language. It is very useful for processing complex formulas. That’s why many scientific and engineering programs have been written in this language.

BASIC:
BASIC, in full Beginner’s All-purpose Symbolic Instruction Code, computer programming language is One of the simplest high-level languages, with commands similar to English, it can be learned with relative ease even by schoolchildren and novice programmers. Its small size and simplicity made BASIC a popular language for early personal computers.

Q.13: What is the difference between declaration and initialization?
Ans: Difference Between Declaration And Initialization:
Declaration:
tells the compiler about the existence of an entity in the program and its location. When you declare a variable, you should also initialize it.

Initialization:

is the process of assigning a value to the Variable. Every programming language has its own method of initializing the variable. If the value is not assigned to the Variable, then the process is only called a Declaration.

Q.14: Differentiate between constant and variables?
Ans: Difference Between Constants and Variables
S.NO. Constant Variables
1. A constant does not change its value during program execution. A variable, on the other hand, changes its value depending on instructions.
2. Constants are usually written in numbers and may be define as identifiers. Variables are always written in letters or symbols.
3. Constants usually represent the known values in an equation, expression or in line of programming. Variables, on the other hand, represent the unknown values.
4. Constants are used in computer programming. Variables also have its uses in computer programming and applications.

Q.15: List out different reserved words commonly used in C++ program.
Ans: RESERVED WORDS:
The following list the keywords or reserved words of the C++ language:
 and new
 asm nullptr
 auto Operator
 bool Or
 break private
 case protected
 catch public
 char register
 class reinterpret_cast
 const requires
 const_cast return
 continue short
 default  signed
 delete sizeof
 do static
 double static_cast
 dynamic_cast struct
 else switch
 enum template
 explicit this
 export Throw
 extern true
 false try
 float typedef
 for typeid
 friend typename
 goto union
 if unsigned
 inline Using
 int virtual
 long void
 mutable volatile
 namespace wchar_t
 not while

Q.16: Use different data types in a C++ program.
Ans: C++ DATA TYPES:
The data types supported in C++ are:



Source: Special Thanks To Sir Syed Arif Ali



4 comments:

  1. Awesome 👍

    ReplyDelete
  2. thanks for helping sir

    ReplyDelete
  3. 𝑻𝒉𝒂𝒏𝒌 𝒚𝒐𝒖 𝒇𝒐𝒓 𝒉𝒆𝒍𝒑𝒊𝒏𝒈

    ReplyDelete
  4. JAZAKALLAH 🤍

    ReplyDelete