0% found this document useful (0 votes)
5 views6 pages

Compiler Design Assignment-Week 8 Type of Question:Mcq Number Ofquestions:13 Total Mark: 13 X 1 13 1. Ans: B) Explanation

The document is an assignment for an NPTEL online certification course on Compiler Design, consisting of 13 multiple-choice questions focused on type checking, type conversion, and semantic analysis in programming languages. Each question includes an answer and a detailed explanation of the concepts involved, such as static vs. dynamic type checking, type equivalence, and type casting. The assignment aims to assess understanding of key compiler design principles related to type systems.

Uploaded by

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

Compiler Design Assignment-Week 8 Type of Question:Mcq Number Ofquestions:13 Total Mark: 13 X 1 13 1. Ans: B) Explanation

The document is an assignment for an NPTEL online certification course on Compiler Design, consisting of 13 multiple-choice questions focused on type checking, type conversion, and semantic analysis in programming languages. Each question includes an answer and a detailed explanation of the concepts involved, such as static vs. dynamic type checking, type equivalence, and type casting. The assignment aims to assess understanding of key compiler design principles related to type systems.

Uploaded by

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

NPTEL Online Certification Courses Indian

Institute of Technology Kharagpur

Compiler Design
Assignment- Week 8
TYPE OF QUESTION:MCQ
Number ofquestions:13 Total mark: 13 X 1 = 13

1.
Ans: b)

Explanation:

Type checking is a part of semantic analysis in a compiler, where it ensures that operations
are performed on compatible data types.

• Lexical Analysis (Option a) → Handles tokenization (e.g., recognizing keywords,


identifiers, operators). It does not check types.
• Syntax Analysis (Option c) → Ensures correct structure based on grammar rules
(e.g., matching parentheses, valid statement formation), but not type correctness.
• Semantic Analysis (Option b) → Checks meaning, including type checking, to
ensure valid operations.

Example of Type Checking Error (Semantic Error):

int x = "hello"; // Error: assigning a string to an integer

2. Ans: c)
Explanation:

The type expression for an argument list follows the order in which the arguments are passed
to the function. Given that:

• The first argument is an Integer


• The second argument is a Real number
• The third argument is an Integer

The correct notation for the type expression is: Integer X Real X Integer

3. Ans: c)
Explanation:

Array bound checking ensures that an array index is within valid limits to prevent out-of-
bounds errors. This can be done in two ways:

1. Static Array Bound Checking (Compile-time)


o If the array size and indices are known at compile time, some compilers can
detect out-of-bounds errors.
o Example (C/C++ with static analysis)

int arr[5];
arr[10] = 20; // Compiler may warn about out-of-bounds access

• • Languages like Ada, Rust, and some static analysis tools in C/C++ support static
bound checking.

• Dynamic Array Bound Checking (Runtime)

• When arrays are allocated dynamically or indices depend on user input, bound
checking happens at runtime.
• Example (Python – automatic bound checking)

arr = [1, 2, 3]
print(arr[5]) # Raises Index Error at runtime
Languages like Python, Java, and C++ (with std::vector::at() method) perform dynamic
bound checking.

4. Ans: a)

Explanation:

Type equivalence is the process of determining whether two type expressions represent the
same type. This is an important part of type checking in programming languages.

• Structural Equivalence: Two types are considered equivalent if they have the same
structure.
• Name Equivalence: Two types are equivalent only if they have the same declared
name.

Example of Type Equivalence (Structural Equivalence in C-like languages)

typedef struct {

int x;

float y;

} A;

typedef struct {

int x;

float y;

} B;

A var1;
B var2;

// Structural equivalence: var1 and var2 have the same structure, but name equivalence would
say they are different.

Since type equivalence checks if two type expressions are the same.

5. Ans: c)
Explanation:

In programming languages, a statement typically performs an action but does not return a
value. Therefore, its type is usually void. However, if the statement contains an invalid
operation, it results in a type error.

Cases:

1. Valid Statement → Type is Void


o Example (C++/Java)

int x = 10; // This is a valid statement, type is void

Invalid Statement → Type Error

• Example (Python)

x = "hello" + 5 # Type Error: can only concatenate str (not "int") to str

The operation is not valid, so a type error occurs.

6. Ans: a)
Explanation:

Type checking ensures that operations are performed on compatible data types. The type
checking done by the compiler is called static type checking because it happens at compile
time before the program runs.

Key Points:

• Static Type Checking (Done by Compiler)


o Performed at compile time.
o Helps catch type errors before execution.
o Used in statically typed languages like C, C++, Java, Rust, Go.
o Example (C++):

int x = "hello"; // Compilation error: invalid conversion

Dynamic Type Checking (Done at Runtime)

• Performed at runtime by the interpreter.


• Used in dynamically typed languages like Python, JavaScript.
• Example (Python):

x = "hello" + 5 # Raises TypeError at runtime

7. Ans: c)

Explanation:

A weakly typed language is one where type rules are more flexible, allowing implicit type
conversions (type coercion) and catching some type errors only at runtime.

1. Less Constraints on the Programmer


o Weakly typed languages allow implicit conversions between data types,
making coding easier but potentially leading to unexpected behaviors.
o Example (JavaScript – automatic type conversion)

let x = "10" + 5; // "105" (string + number → string)

Some Type Errors are Caught Dynamically

• In weakly typed languages, type errors may not be detected at compile time and are
instead caught at runtime.
• Example (Python – runtime type checking):

x = "hello" + 5 # Raises TypeError at runtime

8. Ans: c)

9. Ans: b)
Explanation:

Type casting is the process of converting a value from one data type to another. It can be
explicit (manual) or implicit (automatic, called type coercion).

• Type Coercion refers to the automatic conversion of a value from one data type to
another by the language itself.
o Example (JavaScript - implicit coercion)

let x = "5" * 2; // x = 10 (string "5" is coerced to a number)

• • The language automatically converts "5" (string) to 5 (integer).

• Explicit Type Casting is a manual conversion where the programmer enforces type
conversion.

• Example (C++ - explicit casting):

double num = 5.7;

int x = (int) num; // Explicit casting: x = 5


10. Ans: d)

Explanation:

Consider a function F with the given type:

F: (Integer, Real) → (Integer → Real)

F takes two arguments: an Integer and a Real.F returns another function (G), where G:

o Takes an Integer
o Returns a Real

Thus, this matches option d:

11.

Type inference in programming languages refers to:

a) Explicitly specifying types for all variables


b) Automatically deducing the type of an expression at compile-time
c) Checking types dynamically during runtime
d) Ignoring type checking in a program

Ans: (b)
Explanation: Type inference is the process by which a compiler automatically
determines the type of an expression without explicit type annotations. Many modern
languages (e.g., Haskell, Scala, and TypeScript) use type inference to make coding more
concise while still maintaining strong typing.
12.
Which of the following statements about type conversion is TRUE?

a) Implicit type conversion always results in data loss


b) Explicit type conversion is also called type coercion
c) Implicit type conversion is performed automatically by the compiler
d) Both (b) and (c)

Ans: (c )
Explanation: Implicit type conversion, also known as type promotion, is automatically
handled by the compiler when a smaller data type is converted to a larger data type
(e.g., int to float). Explicit type conversion (also called type casting, not coercion)
requires manual intervention by the programmer.

13.
Dynamic type checking is necessary in languages that:

a) Perform all type checking at compile-time


b) Allow variables to change their type during execution
c) Do not support type inference
d) Have a very strict static type system

Ans: (b)
Explanation: Dynamic type checking is used in languages where variables can hold
values of different types at runtime (e.g., Python, JavaScript). It ensures type safety
during execution, unlike static type checking, which happens at compile-time.

END of Assignment

You might also like