Q14. What does the computer do when it executes a variable declaration statement? Give an example.
Ans. A variable is a box, or location, in the computer’s memory that has name. The box holds a values of some specified type. A variable declaration statement is a statement such as
which creates the variable x. When the computer excutes 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.
Q13. What is a literal?
Ans. A literal is a sequence of characters used in program to represent a constant value. For example, ‘A’ is a literal that represents the value A, of type char, and 17L is a literal that represents the number 17 as a value of type long. A literal is a way of writing a value, and should not be confused with the value itself.
Q12. One of the primitive types in Java is boolean. What is the boolean type? Where are boolean values used? What are its possible values?
Ans. The only values of type boolean are true and false. Expressions of type boolean are used in place where true/false values are expected.
Q11. What is a type, as this term relates to programming?
Ans. A type or datatype represents a set of possible values. When we specify that a variable has a certain type, we are saying what values it can hold.When we say that an expression is of a certain type, you are saying what values the expression can have. 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.
Q10. Suppose x1 and x2 are two double type variables that you want to add as integers and assign to an integer variable. Construct a Java statement for doing so.
Ans. Assuming that target variables is res of type int.
res = ( int ) ( x1 + x2 ) ;
Q9. State the rules of operator precedence.
Ans. All expressions are evaluated according to an operator precedence hierarchy that establishes the rules that govern the order in which operations are evaluated.
Operator (type). *, /, and the remainder operator % are performed before + and -.
Any expression in parentheses is evaluated first
The assignment operator has a lower precedence than any of the arithmetic operators.
Q8. What is casting, when do we need it?
Ans. Casting is a form of conversion, which uses the cast operator to specify by a type name in parenthesis and is placed in front of the value to be converted. For example:
result = ( float ) total / count ;
They are helpful in situations where we temporarily need to treat a value as another type.
Q7. Given the following set of identifiers:
Identify the datatype of the following expressions:
b.) intval * longval – ch
c.) fl + longval / sh
a.) int because
b.) long because
c.) float because
Q6. What output will the following code fragment produce?
int val, res, n=1000;
res = n + val > 1750 ? 400 : 200 ;
i.) if the input is 2000
ii.) if the input is 1000
iii.) if the input is 500.
i) 400 because the arithmetic operator + has higher precedence than ? : operator thus the condition before ? is taken as (n+val) and (1000+2000)>1750 is true.
ii) 400 the reason is the same as explained above((1000+1000)>1750 is true).
iii) 200 because (1000+500)>1750 is false.
Q5. Construct an expression that is equal to the absolute value of an variable. That is, if a variable p is positive, the value of the expression is just p, but if p is negative, the value of the expression is -p, which would be positive. Do it in two ways:
i.) using a mathematical function
ii.) using a conditional operator.(Do not use the mathematical function here).
ii) p > 0 ? p : -p