Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

Question 1: Briefly explain what is meant by the syntax and the semantics of a

programming language. Give an example to illustrate the difference between a

syntax error and a semantics error.

Answer: The syntax of a language is its grammar, and the semantics is its

meaning. A program with a syntax error cannot be compiled. A program with a

semantic error can be compiled and run, but gives an incorrect result. A missing

semicolon in a program is an example of a syntax error, because the compiler will

find the error and report it. If N is an integer variable, then the statement "frac =

1/N;" is probably an error of semantics. The value of 1/N will be 0 for any N greater

than 1. It's likely that the programmer meant to say 1.0/N.

Question 2: What does the computer do when it executes a variable declaration

statement. Give an example.

Answer: A variable is a "box", or location, in the computer's memory that has a

name. The box holds a value of some specified type. A variable declaration

statement is a statement such as

int x;

which creates the variable x. When the computer executes a variable declaration, it

creates the box in memory and associates a name (in this case, x) with that box.

Later in the program, that variable can be referred to by name.

You might also like