w3resource

Prepare for the PCEP-30-02 Exam: Lexis, Syntax, and Semantics Mastery

PCEP Certification Practice Test - Questions, Answers and Explanations

This comprehensive set of questions and explanations covers the fundamental terms and definitions related to lexis, syntax, and semantics, providing a diverse set of interactions to enhance understanding and prepare for the PCEP examination.

Question 1: What is the main focus of lexical analysis in programming?

  1. The meaning of statements and expressions
  2. The structure of statements and expressions
  3. The identification and categorization of tokens
  4. The optimization of code for performance

Answer: C) The identification and categorization of tokens

Explanation: Lexical analysis is the phase in which the source code is converted into tokens, which are the basic building blocks of the program.

Question 2: Which term refers to the rules that define the structure of a programming language?

  1. Lexis
  2. Syntax
  3. Semantics
  4. Compilation

Answer: B) Syntax

Explanation: Syntax defines the set of rules that determine the structure of statements and expressions in a programming language.

Question 3: Which of the following are true about semantics in programming? (Select all that apply)

  1. Semantics deals with the meaning of the code.
  2. Semantics focuses on the logical errors in the program.
  3. Semantics is concerned with how code is structured.
  4. Semantics involves the process of converting code into tokens.

Answer: A) Semantics deals with the meaning of the code. B) Semantics focuses on the logical errors in the program.

Explanation: Semantics is about the meaning of the code and ensures that the code logically makes sense.

Question 4: Match the following programming terms to their descriptions.

Terms Descriptions
Lexis 1. The meaning and behavior of code
Syntax 2. The vocabulary of a programming language
Semantics 3. The process of translating code into machine language
Compilation 4. The set of rules defining the structure of code

Answer:

  • Lexis -> 2. The vocabulary of a programming language
  • Syntax -> 4. The set of rules defining the structure of code
  • Semantics -> 1. The meaning and behavior of code
  • Compilation -> 3. The process of translating code into machine language

Explanation: Lexis refers to the vocabulary, syntax to the structure, semantics to the meaning, and compilation to the translation process.

Question 5: Fill in the blanks: __________ refers to the vocabulary of a programming language, __________ refers to the rules that define the structure, and __________ refers to the meaning of the statements.

Answer: Lexis refers to the vocabulary of a programming language, syntax refers to the rules that define the structure, and semantics refers to the meaning of the statements.

Explanation: Lexis is the set of tokens, syntax is the grammar, and semantics is the meaning.

Question 6: Arrange the following phases of the compiler in the correct order of their execution.

Phases Order
Syntax Analysis 1
Lexical Analysis 2
Code Generation 3
Semantic Analysis 4

Answer:

  1. Lexical Analysis
  2. Syntax Analysis
  3. Semantic Analysis
  4. Code Generation

Explanation: The compiler first performs lexical analysis, followed by syntax analysis, then semantic analysis, and finally code generation.

Question 7: Fill in the code to print "Hello, World!" ensuring correct syntax and semantics.

def greet():
    _______

greet()

Answer: print("Hello, World!")

Explanation: The print function correctly outputs the string "Hello, World!".

Question 8: Insert the missing line of code to define a function that returns the sum of two numbers.

def add(a, b):
    _______

result = add(3, 4)
print(result)

Answer: return a + b

Explanation: The return statement correctly returns the sum of a and b.

Question 9: Which of the following is an example of a syntax error?

  1. Using an undefined variable
  2. Missing a colon at the end of a function definition
  3. Dividing a number by zero
  4. Using incorrect indentation

Answer: B) Missing a colon at the end of a function definition

Explanation: Syntax errors occur when the code structure violates the rules of the programming language, such as missing punctuation.

Question 10: What is the output of the following code snippet if executed correctly?

def say_hello():
    print("Hello")

say_hello()
  1. Hello
  2. hello
  3. Error
  4. None

Answer: A) Hello

Explanation: The function say_hello correctly prints "Hello" when called.

Question 11: Which of the following are considered lexical elements in Python? (Select all that apply)

  1. Keywords
  2. Operators
  3. Indentation
  4. Identifiers

Answer: A) Keywords B) Operators D) Identifiers

Explanation: Lexical elements include keywords, operators, and identifiers, but indentation relates to syntax.

Question 12: Match the following examples to the correct terms.

Examples Terms
if, while 1. Keywords
+, - 2. Whitespace
x, y, name 3. Operators
, \t 4. Identifiers

Answer:

  1. if, while -> 1. Keywords
  2. +, - -> 3. Operators
  3. x, y, name -> 4. Identifiers
  4. , \t -> 2. Whitespace

Explanation: Keywords are reserved words, operators perform operations, identifiers name variables, and whitespace includes spaces and tabs.

Question 13: Fill in the blanks: __________ are reserved words in Python, __________ are symbols that represent computations, and __________ are names given to variables.

Answer: Keywords are reserved words in Python, operators are symbols that represent computations, and identifiers are names given to variables.

Explanation: Keywords have special meaning, operators are used for calculations, and identifiers are used to name variables.

Question 14: Arrange the following code snippets to form a correct Python function that calculates the factorial of a number.

Code Snippets Order
return 1: 1
else: 2
def factorial(n) 3
if n == 0: 4
return n * factorial(n-1) 5

Answer:

  • def factorial(n):
  • if n == 0:
  • return 1
  • else:
  • return n * factorial(n-1)

Explanation: The function first defines the base case where n is 0, returning 1, and recursively calls itself otherwise.

Question 15: Complete the following code to define a function that checks if a number is even or odd.

def check_even_odd(num):
    if num % 2 == 0:
        _______
    else:
        _______

check_even_odd(5)

Answer:

		print("Even")
    else:
        print("Odd")
		

Explanation: The if statement checks for even numbers, and the else handles odd numbers.

Question 16: Insert the missing line of code to define a function that prints "Learning Python".

def print_message():
    _______

print_message()

Answer: print("Learning Python")

Explanation: The print function correctly outputs the string "Learning Python".

Question 17: Which phase of compilation involves checking the meaning of the statements?

  1. Lexical Analysis
  2. Syntax Analysis
  3. Semantic Analysis
  4. Code Generation

Answer: C) Semantic Analysis

Explanation: Semantic analysis checks the meaning and logical consistency of the statements.

Question 18: What is the role of syntax analysis in programming?

  1. To convert code into tokens
  2. To check the structure of the code
  3. To check the meaning of the code
  4. To generate machine code

Answer: B) To check the structure of the code

Explanation: Syntax analysis ensures that the code follows the grammatical rules of the programming language.

Question 19: Which of the following are true about lexical analysis? (Select all that apply)

  1. It converts code into tokens.
  2. It checks the structure of the code.
  3. It involves pattern matching.
  4. It generates machine code.

Answer: A) It converts code into tokens. C) It involves pattern matching.

Explanation: Lexical analysis converts the code into tokens and involves pattern matching to identify these tokens.

Question 20: Match the following programming phases with their descriptions.

Phases Descriptions
Lexical Analysis 1. Converts code into tokens
Syntax Analysis 2. Checks the meaning of the code
Semantic Analysis 3. Translates code into machine language
Code Generation 4. Checks the structure of the code

Answer:

  • Lexical Analysis -> 1. Converts code into tokens
  • Syntax Analysis -> 4. Checks the structure of the code
  • Semantic Analysis -> 2. Checks the meaning of the code
  • Code Generation -> 3. Translates code into machine language

Explanation: Each phase has a specific role in the compilation process, starting from converting code into tokens to generating machine language.

Question 21: Fill in the blanks: __________ analysis involves converting code into tokens, __________ analysis checks the structure of the code, and __________ analysis checks the meaning of the code.

Answer: Lexical analysis involves converting code into tokens, syntax analysis checks the structure of the code, and semantic analysis checks the meaning of the code.

Explanation: Lexical analysis tokenizes the code, syntax analysis ensures grammatical correctness, and semantic analysis verifies the meaning.

Question 22: Arrange the following phases of the compilation process in the correct order.

Phases Order
Semantic Analysis 1
Lexical Analysis 2
Code Generation 3
Syntax Analysis 4

Answer:

  • Lexical Analysis
  • Syntax Analysis
  • Semantic Analysis
  • Code Generation

Explanation: The compilation process begins with lexical analysis, followed by syntax and semantic analysis, and ends with code generation.

Question 23: Fill in the code to define a function that checks if a string is a palindrome.

def is_palindrome(s):
    return s == _______

print(is_palindrome("madam"))

Answer: s[::-1]

Explanation: The slice s[::-1] reverses the string s, and the function checks if the string is the same forwards and backwards.

Question 24: Insert the missing line of code to define a function that returns the maximum of two numbers.

def max_num(a, b):
    if a > b:
        _______
    else:
        _______

result = max_num(3, 5)
print(result)

Answer:

      return a
    else:
      return b

Explanation: The if statement returns the greater number, and the else statement returns the other number.

Question 25: Which of the following is an example of a semantic error?

  1. Using incorrect indentation
  2. Missing a closing parenthesis
  3. Adding an integer to a string
  4. Misspelling a keyword

Answer: C) Adding an integer to a string

Explanation: Semantic errors occur when the code's logic is incorrect, such as adding incompatible types.

Question 26: What is the output of the following code snippet?

def greet(name):
    return "Hello, " + name

print(greet("Alice"))
  1. Hello, Alice
  2. Hello, name
  3. Error
  4. None

Answer:A) Hello, Alice

Explanation: The function greet correctly concatenates "Hello, " with the name provided and returns the result.

Question 27: Which of the following are part of syntax analysis? (Select all that apply)

  1. Tokenizing the code
  2. Checking for proper nesting of code blocks
  3. Ensuring correct operator usage
  4. Verifying variable types

Answer: B) Checking for proper nesting of code blocks C) Ensuring correct operator usage

Explanation: Syntax analysis involves checking the structure and correct usage of operators and nested blocks.

Question 28: Match the following terms to their roles in programming.

Terms Roles
Lexical Analysis 1. Ensures code structure follows language rules
Syntax Analysis 2. Converts code into machine-executable form
Semantic Analysis 3. Checks code logic and meaning
Code Generation 4. Tokenizes the code

Answer:

  • Lexical Analysis -> 4. Tokenizes the code
  • Syntax Analysis -> 1. Ensures code structure follows language rules
  • Semantic Analysis -> 3. Checks code logic and meaning
  • Code Generation -> 2. Converts code into machine-executable form

Explanation: Each term matches its role in the process of interpreting or compiling code.

Question 29: Fill in the blanks: During __________ analysis, code is tokenized; during __________ analysis, code structure is checked; and during __________ analysis, code logic is verified.

def print_message():
    ____

print_message()

Answer: During lexical analysis, code is tokenized; during syntax analysis, code structure is checked; and during semantic analysis, code logic is verified.

Explanation: Lexical analysis tokenizes the code, syntax analysis checks structure, and semantic analysis verifies logic.

Question 30: Arrange the following steps of a Python program's execution into the correct order.

Steps Order
Interpretation 1
Lexical Analysis 2
Syntax Analysis 3
Semantic Analysis 4

Answer:

  • Lexical Analysis
  • Syntax Analysis
  • Semantic Analysis
  • Interpretation

Explanation: The steps follow the order of lexical analysis, syntax analysis, semantic analysis, and finally interpretation for execution.

Question 31: Complete the following code to check if a number is positive, negative, or zero.

def check_number(n):
    if n > 0:
        _______
    elif n < 0:
        _______
    else:
        _______

check_number(-1)

Answer:

		print("Positive")
    elif n < 0:
        print("Negative")
    else:
        print("Zero")
		

Explanation: The code correctly prints "Positive", "Negative", or "Zero" based on the value of n.

Question 32: Insert the missing line of code to define a function that prints "Welcome to Python".

def welcome_message():
    _______

welcome_message()

Answer: print("Welcome to Python")

Explanation: The print function correctly outputs the string "Welcome to Python".

Question 33: Which of the following phases comes first in the compilation process?

  1. Code Generation
  2. Semantic Analysis
  3. Syntax Analysis
  4. Lexical Analysis

Answer: D) Lexical Analysis

Explanation: Lexical analysis is the first phase where the code is converted into tokens.

Question 34: What is checked during semantic analysis?

  1. Token classification
  2. Structure of code
  3. Logical consistency of code
  4. Conversion to machine code

Answer: C) Logical consistency of code

Explanation: Semantic analysis checks the logical consistency and meaning of the code.

Question 35: Which of the following are examples of syntax rules in Python? (Select all that apply)

  1. Indentation
  2. Variable declaration
  3. Use of colons in function definitions
  4. Assignment of values

Answer: A) Indentation C) Use of colons in function definitions

Explanation: Syntax rules include indentation and the use of colons, while variable declaration and value assignment are more related to semantics.

Question 36: Match the following programming concepts to their descriptions.

Concepts Descriptions
Token 1. Ensuring variables are used with compatible types
Syntax Tree 2. Basic building block of code, identified during lexical analysis
Type Checking 3. Converting code into machine-executable form
Compilation 4. Hierarchical structure representing the syntax of code

Answer:

  • Token -> 2. Basic building block of code, identified during lexical analysis
  • Syntax Tree -> 4. Hierarchical structure representing the syntax of code
  • Type Checking -> 1. Ensuring variables are used with compatible types
  • Compilation -> 3. Converting code into machine-executable form

Explanation: Tokens are identified during lexical analysis, syntax trees represent code structure, type checking ensures type compatibility, and compilation converts code to machine form.

Question 37: Fill in the blanks: A __________ is a basic building block of code identified during lexical analysis, while a __________ tree represents the hierarchical structure of the code.

Answer: A token is a basic building block of code identified during lexical analysis, while a syntax tree represents the hierarchical structure of the code.

Explanation: Tokens are identified during lexical analysis, and the syntax tree is created during syntax analysis.

Question 38: Arrange the following elements of a Python program into the correct order of execution.

Elements Order
Function Definition 1
Function Call 2
Return Statement 3
Print Output 4

Answer:

  1. Function Definition
  2. Function Call
  3. Return Statement
  4. Print Output

Explanation: A function is defined first, called next, then returns a value, and finally prints the output.

Question 39: Complete the following code to define a function that returns the square of a number.

def square(n):
    _______

result = square(4)
print(result)

Answer: return n * n

Explanation: The return statement correctly returns the square of the number n.

Question 40: Insert the missing line of code to define a function that prints "Goodbye, World!".

def goodbye():
    _______

goodbye()

Answer: print("Goodbye, World!")

Explanation: The print function correctly outputs the string "Goodbye, World!".

Question 41: Which term refers to the rules that define the structure of a programming language?

  1. Lexis
  2. Syntax
  3. Semantics
  4. Compilation

Answer: B) Syntax

Explanation: Syntax defines the set of rules that determine the structure of statements and expressions in a programming language.

Question 42: What is the main focus of semantic analysis in programming?

  1. The meaning of statements and expressions
  2. The structure of statements and expressions
  3. The identification and categorization of tokens
  4. The optimization of code for performance

Answer: A) The meaning of statements and expressions

Explanation: Semantic analysis focuses on the meaning and logical correctness of the code.

Question 43: Which of the following statements about syntax are true? (Select all that apply)

  1. Syntax defines how programs should be structured.
  2. Syntax deals with the meaning of statements.
  3. Syntax ensures that code follows grammatical rules.
  4. Syntax analysis occurs before semantic analysis.

Answer: A) Syntax defines how programs should be structured. C) Syntax ensures that code follows grammatical rules. D) Syntax analysis occurs before semantic analysis.

Explanation: Syntax defines structure and grammatical rules, and syntax analysis occurs before semantic analysis.

Question 44: Match the following programming terms to their descriptions.

Terms Descriptions
Lexis 1. The meaning and behavior of code
Syntax 2. The set of rules defining the structure of code
Semantics 3. The vocabulary of a programming language
Interpretation 4. The process of executing code line-by-line

Answer:

  • Lexis -> 3. The vocabulary of a programming language
  • Syntax -> 2. The set of rules defining the structure of code
  • Semantics -> 1. The meaning and behavior of code
  • Interpretation -> 4. The process of executing code line-by-line

Explanation: Lexis refers to the vocabulary, syntax to the structure, semantics to the meaning, and interpretation to executing code line-by-line.

Question 45: Fill in the blanks: __________ refers to the vocabulary of a programming language, __________ refers to the rules that define the structure, and __________ refers to the meaning of the statements.

Answer: Lexis refers to the vocabulary of a programming language, syntax refers to the rules that define the structure, and semantics refers to the meaning of the statements.

Explanation: Lexis is the set of tokens, syntax is the grammar, and semantics is the meaning.

Question 46: Arrange the following phases of the compiler in the correct order of their execution.

Phases Order
Syntax Analysis 1
Lexical Analysis 2
Code Generation 3
Semantic Analysis 4

Answer:

  • Lexical Analysis
  • Syntax Analysis
  • Semantic Analysis
  • Code Generation

Explanation: The compiler first performs lexical analysis, followed by syntax analysis, then semantic analysis, and finally code generation.

Question 47: Fill in the code to print "Hello, World!" ensuring correct syntax and semantics.

def greet():
    _______

greet()

Answer: print("Hello, World!")

Explanation: The print function correctly outputs the string "Hello, World!".

Question 48: Insert the missing line of code to define a function that returns the sum of two numbers.

def add(a, b):
    _______

result = add(3, 4)
print(result)

Answer: return a + b

Explanation: The return statement correctly returns the sum of a and b.

Question 49: Which of the following is an example of a syntax error?

  1. Using an undefined variable
  2. Missing a colon at the end of a function definition
  3. Dividing a number by zero
  4. Using incorrect indentation

Answer: B) Missing a colon at the end of a function definition

Explanation: Syntax errors occur when the code structure violates the rules of the programming language, such as missing punctuation.

Question 50: What is the output of the following code snippet if executed correctly?

def say_hello():
    print("Hello")
say_hello()
  1. Hello
  2. hello
  3. Error
  4. None

Answer: A) Hello

Explanation: The function say_hello correctly prints "Hello" when called.



Follow us on Facebook and Twitter for latest update.