Assignment 3
Assignment 3
BSCS 3 – 1
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.
Syntax refers to the structure/form of the code that a specific programming language specifies
but Semantics deal with the meaning assigned to the symbols, characters and words. For example,
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.
2. What does the computer do when it executes a variable declaration statement? Give an example.
When the computer executes a variable declaration, it creates the box in memory and associates a
name (in this case, x) with that box. For example, to say that a variable is of type int says that
integer values in a certain range can be stored in that variable.
3. What is a type, as this term relates to programming?
A data type or simply type is an attribute of data which tells the compiler or interpreter how the
programmer intends to use the data.
4. One of the primitive types in Java is boolean. What is the Boolean type? Where are boolean values
used? What are its possible values?
The only values of type boolean are true and false. Expressions of type boolean are used in places
where true/false values are expected, such as the conditions in while loops and if statements.
5. Give the meaning of each of the following Java operators:
a) ++ - used to add 1 to the value of a variable
b) && - represents the word and. It can be used to combine two boolean values.
c) != - means "is not equal to"
6. Explain what is meant by an assignment statement, and give an example. What are assignment
statements used for?
An assignment statement gives a value to a variable. For example, x = 5; gives x the value 5. It
is used to change the value of a variable as the program is running.
7. What is meant by precedence of operators?
Operator precedence determines the grouping of terms in an expression and decides how an
expression is evaluated.
8. What is a literal?
A literal is the idea of expressing a non-changing value in a computer program's source code.
9. In Java, classes have two fundamentally different purposes. What are they?
Class represents the set of properties or methods that are common to all objects of one type and it
provides template for creating objects, which can bind code into data.
10. What is the difference between the statement “x = TextIO.getDouble();” and the statement “x =
TextIO.getlnDouble();”
Either statements will read a real number input by the user, and store that number in the variable,
x. They would both read and return exactly the same value. The difference is that in the second
statement, using getlnDouble, after reading the value, the computer will continue reading
characters from input up to and including the next carriage return. These extra characters are
discarded.
11. Explain why the value of the expression2 + 3 + "test" is the string "5test" while the value of the
expression "test" + 2 + 3is the string"test23". What is the value of "test" + 2 * 3?
The difference is due to the order of evaluation. When several + operators are used in a row,
with no parentheses, they are evaluated from left to right. 2 + 3 + "test" is interpreted as (2 + 3) +
"test", so 2 and 3 are added together, giving 5, and then the 5 is concatenated onto the string
"test". On the other hand, "test" + 2 + 3 is interpreted as ("test" + 2) + 3, so the 2 is first
concatenated onto the "test", giving "test2", and then the 3 is concatenated onto that. In the case of
"test" + 2 * 3, the precedence rules for + and * come into play. Since * has higher precedence, this
expression is interpreted as "test" + (2 * 3), which evaluates to "test6".
12. Integrated Development Environments such as Eclipse often use syntax coloring, which assigns
various colors to the characters in a program to reflect the syntax of the language. A student
notices that Eclipse colors the word String differently from int, double, and boolean. The student
asks why String should be a different color, since all these words are names of types. What’s the
answer to the student’s question?
String is the name of a class, while int, double, and boolean are primitive types. Eclipse colors
all class names in the same way that it does String, and it uses a different color for the
primitive types.