Compiler Design Assignment-Week 8 Type of Question:Mcq Number Ofquestions:13 Total Mark: 13 X 1 13 1. Ans: B) Explanation
Compiler Design Assignment-Week 8 Type of Question:Mcq Number Ofquestions:13 Total Mark: 13 X 1 13 1. Ans: B) Explanation
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.
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 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:
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.
• 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.
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:
• Example (Python)
x = "hello" + 5 # Type Error: can only concatenate str (not "int") to str
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:
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.
• In weakly typed languages, type errors may not be detected at compile time and are
instead caught at runtime.
• Example (Python – runtime type checking):
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)
• Explicit Type Casting is a manual conversion where the programmer enforces type
conversion.
Explanation:
F takes two arguments: an Integer and a Real.F returns another function (G), where G:
o Takes an Integer
o Returns a Real
11.
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?
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:
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