August 28, 2007

Java Question - 4

Q 31- Using the symbols 1 and 0 construct a truth table showing the four possible combinations of 1 and 0. Using a 1 or a 0, show the result of the bitwise inclusive or operation on these four
combinations on these four combinations of 1 and 0.
A - The answer for the inclusive or is:
1 or 1 produces 1
1 or 0 produces 1
0 or 1 produces 1
0 or 0 produces 0
Q 32- Using the symbols 1 and 0 construct a truth table showing the four possible combinations of 1 and 0. Using a 1 or a 0, show the result of the bitwise exclusive or operation on these four
combinations on these four combinations of 1 and 0.
A - The answer for the exclusive or is:
1 xor 1 produces 0
1 xor 0 produces 1
0 xor 1 produces 1
0 xor 0 produces 0
Q 33- For the exclusive or, if the two bits are different, the result is a 1. If the two bits are the same, the result is a 0. True or False? If your answer is False, explain why.
A - True.
Q 34- Is the assignment operator a unary operator or a binary operator. Select one or the other.
A - The assignment operator is a binary operator.
Q 35- In Java, when using the assignment operator, the value stored in memory and represented by the right operand is copied into the memory represented by the left operand: True or False? If your answer is False, explain why.
A - True.
Q 36- Show two of the shortcut assignment operators and explain how they behave by comparing them with the regular (nonshortcut) versions. Hint: The (^=) operator is a shortcut assignment operator.
A - Java supports the following list of shortcut assignment operators. These operators allow you to perform an assignment and another operation with a single operator. += -= *= /= %= &= |= ^= <<= >>= >>>=
For example, the two statements which follow perform the same operation.
x += y; x = x + y;
The behavior of all the shortcut assignment operators follows this same pattern.
Q 37 - Write a Java application illustrates the difference between the prefix and the postfix versions of the increment operator
class prog3{
static public void main(String[] args){
int x = 3;
int y = 3;
int z = 10;
System.out.println("Prefix version gives " + (z + ++x));
System.out.println("Postfix version gives " + (z + y++));
}//end main
}//end class
Q38 Write a Java application that illustrates the use of the following relational operators: < > <= >= == !=
class Prog4 { //define the controlling class
public static void main(String[] args){ //define main method
System.out.println("The relational 6<5 is " + (6<5 ) );
System.out.println("The relational 6>5 is " + (6>5 ) );
System.out.println("The relational 5>=5 is " + (5>=5 ) );
System.out.println("The relational 5<=5 is " + (5<=5 ) );
System.out.println("The relational 6==5 is " + (6==5 ) );
System.out.println("The relational 6!=5 is " + (6!=5 ) );
}//end main
}//End prog4 class. Note no semicolon required
Q39 - Write a Java application that illustrates the use of the following logical or conditional operators:&& || !
class prg5 { //define the controlling class
public static void main(String[] args){ //define main method
System.out.println("true and true is " + (true && true) );
System.out.println("true and false is " + (true && false) );
System.out.println("true or true is " + (true || true) );
System.out.println("true or false is " + (true || false) );
System.out.println("false or false is " + (false || false) );
System.out.println("not true is " + (! true) );
System.out.println("not false is " + (! false) );
}//end main
}
Q40 - Java supports a constant type: True or False. If false, explain why
A - Java does not support a constant type. However, in Java, it is possible to achieve the same result by declaring and initializing a variable and making it final.
Q41 Provide a code fragment that illustrates the syntax for creating a named constant in Java.
A - The syntax for creating a named constant in Java is as follows: final float PI = 3.14159;
Q42 - What is the common method of controlling the order of evaluation of expressions in Java?
A - you can control the order of evaluation by the use of parentheses.
Q43 - What are the three actions normally involved in the operation of a loop (in addition to executing the code in the body of the loop)?
A - The operation of a loop normally involves the following three actions in addition to executing the code in the body of the loop:
Initialize a control variable.
Test the control variable in a conditional expression.
Update the control variable.

No comments:

Post a Comment