Q.1 What is a typed language in the context of compilers?
A language where variable types are explicitly declared or inferred
A language without any type checking
A language that does not require compilation
A language that uses only integer variables
Explanation - Typed languages enforce type rules either through explicit declarations or type inference to prevent type errors during compilation.
Correct answer is: A language where variable types are explicitly declared or inferred
Q.2 Which of the following is an example of an untyped language?
C
Python
Assembly language
Java
Explanation - Assembly language is considered untyped because it does not enforce data types for its variables, leaving type interpretation to the programmer.
Correct answer is: Assembly language
Q.3 What is the main challenge for compilers of untyped languages?
Performing type checking at compile-time
Generating machine code
Parsing syntax
Handling memory allocation
Explanation - Untyped languages do not provide type information, so type checking must be deferred until runtime or requires additional runtime checks.
Correct answer is: Performing type checking at compile-time
Q.4 Which technique can compilers use to infer types in a typed language?
Lexical analysis
Type inference
Code optimization
Dead code elimination
Explanation - Type inference allows the compiler to deduce the types of variables automatically without explicit type declarations.
Correct answer is: Type inference
Q.5 In typed languages, which of the following errors can a compiler catch?
Syntax errors
Type mismatch errors
Memory leaks
Logical errors in algorithms
Explanation - Typed language compilers can detect mismatches between expected and actual data types at compile time, preventing type errors.
Correct answer is: Type mismatch errors
Q.6 Dynamic typing is a feature of:
C
Java
Python
C++
Explanation - Python uses dynamic typing, where variable types are determined at runtime, unlike statically typed languages like C or Java.
Correct answer is: Python
Q.7 Static typing is advantageous because:
It allows faster runtime execution
It delays error detection
It eliminates the need for compilers
It allows untyped variable usage
Explanation - Static typing enables the compiler to perform type checking and optimization at compile time, improving runtime efficiency.
Correct answer is: It allows faster runtime execution
Q.8 Which of the following is a statically typed language?
JavaScript
C#
PHP
Ruby
Explanation - C# is statically typed, meaning all variable types are known at compile-time and must conform to type rules.
Correct answer is: C#
Q.9 Which part of a compiler is responsible for enforcing type rules?
Lexer
Parser
Semantic analyzer
Code generator
Explanation - The semantic analyzer performs type checking and ensures variables and expressions are used consistently with their types.
Correct answer is: Semantic analyzer
Q.10 An untyped language often requires:
Compile-time type checking
Runtime type checking
No type checking at all
Static analysis only
Explanation - Since untyped languages lack compile-time type information, type checks are deferred to runtime to prevent errors.
Correct answer is: Runtime type checking
Q.11 Which of these languages uses type inference?
Haskell
C
Assembly
Java
Explanation - Haskell uses a strong type system with type inference, allowing the compiler to deduce types automatically.
Correct answer is: Haskell
Q.12 What is type coercion?
Automatically converting a value from one type to another
Declaring a variable without a type
Checking syntax errors
Generating optimized code
Explanation - Type coercion allows a compiler or interpreter to convert values between compatible types automatically during computation.
Correct answer is: Automatically converting a value from one type to another
Q.13 Which is a key disadvantage of untyped languages?
Slower compilation
Increased runtime errors
Complex syntax
Limited platform support
Explanation - Untyped languages may allow operations that are type-unsafe, leading to potential runtime errors that typed languages can prevent.
Correct answer is: Increased runtime errors
Q.14 Strongly typed languages:
Allow implicit type conversions freely
Prevent operations between incompatible types
Do not enforce types
Only check types at runtime
Explanation - Strongly typed languages strictly enforce type rules, disallowing operations between incompatible types.
Correct answer is: Prevent operations between incompatible types
Q.15 Weakly typed languages may:
Automatically convert between types
Never convert types
Reject all type mismatches at compile time
Require manual memory management
Explanation - Weakly typed languages perform implicit type conversions, which can lead to unexpected behaviors if not carefully managed.
Correct answer is: Automatically convert between types
Q.16 Which of the following compilers would have an easier time optimizing code?
Compiler for untyped language
Compiler for typed language
Compiler for assembly language
Compiler for interpreted language only
Explanation - Typed languages provide type information that allows compilers to perform more aggressive and safe optimizations.
Correct answer is: Compiler for typed language
Q.17 Which of these is NOT typically handled by the semantic analyzer?
Type checking
Scope resolution
Code generation
Function signature verification
Explanation - The semantic analyzer ensures type correctness and scope rules, while code generation is a separate compiler phase.
Correct answer is: Code generation
Q.18 Duck typing is associated with which type of language?
Statically typed
Dynamically typed
Untyped assembly language
Strongly typed C++
Explanation - Duck typing allows an object to be used as long as it has the required methods or properties, common in dynamically typed languages like Python.
Correct answer is: Dynamically typed
Q.19 Which statement is true for strongly typed languages?
They ignore type rules
They require explicit type conversions
They allow variables to change type dynamically
They perform no runtime checks
Explanation - In strongly typed languages, incompatible types cannot be combined without explicit conversion to ensure type safety.
Correct answer is: They require explicit type conversions
Q.20 Why might compilers for untyped languages insert runtime checks?
To improve compilation speed
To ensure type correctness
To optimize code
To reduce memory usage
Explanation - Since untyped languages provide no compile-time type information, runtime checks are necessary to catch type errors during execution.
Correct answer is: To ensure type correctness
Q.21 Which language feature complicates compilation for typed languages?
Explicit type declarations
Overloading and polymorphism
Type inference
Simple arithmetic operations
Explanation - Features like overloading and polymorphism require the compiler to resolve correct types and method calls, complicating compilation.
Correct answer is: Overloading and polymorphism
Q.22 Type errors detected at compile time are called:
Runtime errors
Syntax errors
Static type errors
Logical errors
Explanation - Static type errors are type mismatches detected during compilation in statically typed languages.
Correct answer is: Static type errors
Q.23 Which statement about untyped languages is false?
They require runtime type checking
They allow more flexibility in coding
They prevent type-related runtime errors
They do not enforce type rules at compile time
Explanation - Untyped languages do not prevent type-related runtime errors; in fact, these errors are more likely due to lack of compile-time type enforcement.
Correct answer is: They prevent type-related runtime errors
Q.24 Which scenario benefits most from strong typing?
Rapid prototyping with dynamic code
Writing large-scale, critical software
Scripting short automation tasks
Learning assembly language
Explanation - Strong typing helps catch errors early and ensures type safety, which is crucial for large, complex, and critical software systems.
Correct answer is: Writing large-scale, critical software
