0% found this document useful (0 votes)
8 views4 pages

Computer Science Questions and Answers

The document explains key concepts in computer science, focusing on identifiers, keywords, variable declaration and definition, and type casting. It differentiates between reserved words and identifiers, outlines rules for naming variables, and describes implicit and explicit type casting. Additionally, it highlights the purpose of comments in programming and concludes with the distinction between declaring and defining a variable.

Uploaded by

fahadtariq7704
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Computer Science Questions and Answers

The document explains key concepts in computer science, focusing on identifiers, keywords, variable declaration and definition, and type casting. It differentiates between reserved words and identifiers, outlines rules for naming variables, and describes implicit and explicit type casting. Additionally, it highlights the purpose of comments in programming and concludes with the distinction between declaring and defining a variable.

Uploaded by

fahadtariq7704
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Computer Science Questions and Answers

1. What is an Identifier? Give an Example.

An identifier is the name given to variables, functions, arrays, or other user-


defined elements in a programming language.

Example:

int studentAge = 20; // 'studentAge' is an identifier

2. Differentiate Between Keyword and Identifier?

Feature Keyword Identifier


A keyword is a reserved word in a An identifier is the name given
Definition programming language that has a to variables, functions, and user-
predefined meaning. defined elements.
Purpose Defines syntax and structure. Names variables and functions.
Predefined? Yes No
Example int, return, if, else studentName, totalMarks

3. Write Two Rules for Declaring Naming Variables in C.

1️⃣ A variable name must start with a letter (A-Z, a-z) or an underscore (_), but
not a digit (0-9). 2️⃣ A variable name cannot be a keyword in C.

Example:

int age = 25; // Valid


int 1num = 10; // Invalid

4. What are Reserved Words?

Reserved words are special words in a programming language that have


predefined meanings and cannot be used as variable names or function names.

Examples in C: int, float, return, while, if, else.

5. Define Type Casting and Its Types.

Type casting is the process of converting one data type into another.
Types of Type Casting:

1️⃣ Implicit Type Casting (Automatic Conversion) – Done by the compiler,


safe conversion. 2️⃣ Explicit Type Casting (Manual Conversion) – Done by the
programmer, may lead to data loss.

Example:

float num = 10.5;


int result = (int) num; // Explicit type casting

6. Define Implicit Type Casting.

Implicit type casting is an automatic type conversion performed by the compiler


when a smaller data type is converted into a larger data type.

Example:

int num = 10;


float result = num; // Implicit conversion from int to float

7. Where Can Comments Be Added in Programs?

Comments can be added anywhere in a program to explain the code.

Types of Comments:

1️⃣ Single-line Comments (//) – Used for short explanations. 2️⃣ Multi-line
Comments (/* ... */) – Used for longer explanations.

Example:

// This is a single-line comment


/* This is a
multi-line comment */

What is a Variable?

A variable is a named memory location used to store data that can change during
the execution of a program. It acts as a container for values and allows
programmers to perform operations on stored data.
Example in C:

c
CopyEdit
int age = 25; // 'age' is a variable storing the value 25
float price = 10.5; // 'price' is a variable storing the value 10.5

Difference Between Declaring and Defining a Variable


Feature Declaration Definition
Declaring a variable tells the Defining a variable
Meaning compiler about its name and type allocates memory and may
but does not allocate memory. assign an initial value.
Memory
No Yes
Allocation?
Used when a variable is declared Used when a variable is
Usage
globally and defined later. initialized and ready to use.
Example extern int num; int num = 10;

Example in C:
c
CopyEdit
// Variable Declaration (No memory allocated)
extern int number;

// Variable Definition (Memory allocated and value assigned)


int number = 100;

✅ The extern keyword is used for declaration without defining (allocating


memory).

Conclusion:

 A variable is a named storage location for data.


 Declaring a variable only informs the compiler about the variable name
and type but does not allocate memory.
 Defining a variable allocates memory and may assign an initial value.

You might also like