Download assign1Ans

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Assignment1 Answer(More than the assignment needed!!)
1.
Suppose a and b are int variables. What is the value of b?
a = 3;
b = 4 + (++a);
Answer:
2.
8
Given the following assignments. What is the value in str?
String str;
str
= “Hello”;
str = str + “ World”;
Answer: Hello World
3.
The method showInputDialog belongs to the class ____.
Answer: JOptionPane
4.
The tokens of a string are usually delimited by ____ characters.
Answer: whitespace
5.
What is the value of the expression:
(10 >= 5) && (‘B’ < ‘C’)
Answer: true
6.
With short-circuit evaluation, when the value of an entire expression is found, evaluation ____.
Answer: stops
7.
Write a Java statement to accomplish each of the following tasks
a) Declare variables x, y, z, thisIsAVariable, q76354 and number to be of type int
Answer:
int x, y, z, thisIsAVariable, q76354, number;
b) Declare the variables xVal, yVal and zVal to be of type String.
Answer:
String xVal, yVal, zVal;
c) Convert xVal to an int and store the result in the variable x
Answer:
x = Integer.parseInt(xVal);
8.
Rewrite the following if-else statement using switch statement.
if (florida == 1) {
System.out.println("Number 1 Ranking!");
}
else if(florida == 2) {
System.out.println("Almost there ...");
}
else {
System.out.println("Unacceptable");
[16 marks]
}
Answer :
switch(florida)
{
case 1: System.out.println("Number 1 Ranking!");
break;
case 2: System.out.println("Almost there ... ");
break;
default: System.out.println("Unacceptable");
}
9.
What is the result of running the following piece of code?
import java.util.*;
public class Test{
public static void main(String args[]){
String s = new String("Java~see~Java~do");
StringTokenizer st = new StringTokenizer(s, "/");
int count = 1;
while(st.hasMoreTokens()){
System.out.println("Token number " + count + " : " + st.nextToken());
count++;
}
}
}
Answer :
Java~see~Java~do
10
Assume that the way the StringTokenizer is created in the above sample program is changed as follows:
StringTokenizer st = new StringTokenizer(s, "~");
What is the result of the sample program?
import java.util.*;
public class Test{
public static void main(String args[]){
String s = new String("Java~see~Java~do");
StringTokenizer st = new StringTokenizer(s, "~");
int count = 1;
while(st.hasMoreTokens()){
System.out.println("Token number " + count + " : " + st.nextToken());
count++;
}
}
}
Answer :
Token number 1 : Java
Token number 2 : see
Token number 3 : Java
Token number 4 : do
11
Rewrites the following program segment by using a while and a do … while loop to have the same output.
j = 2;
For(i = 1;i<=5;i++){
System.out.print(j + “ ”);
j= j+5;
}
System.out.println();
Ans
a.
j = 2;
i = 1;
while(i <= 5)
{
System.out.print(j + " ");
j = j + 5;
i++;
}
System.out.println();
b.
j = 2;
i = 1;
do
{
System.out.print(j + " ");
j = j + 5;
i++;
} while(i <= 5);
System.out.println();
12
What is the output of the following Java code?
x=100;
y=200;
if(x>100 && y<=200)
System.out.println(x + “ ” + y + “ ” + (x+y));
Else
System.out.println(x + “ ” + y + “ ” + (2*x-y));
Ans:
100 200 0
Suppose that you have the following declaration:
int j = 0;
The output of the statement
if ((8>4 || (j++ ==7)))
System.out.println(“j =” +j);
is:
j=0
while the output of the statement
if ((8>4 | (j++ ==7)))
System.out.println(“j =” +j);
is:
j=1
Explain why?
Ans: The expression ((8 > 4) || (j++ == 7)) is evaluated using short circuit. Because 8 > 4 is true, the
expression (j++ == 7) is not evaluate. Therefore, the value of j stays 0. On the other hand, to evaluate the
expression ((8 > 4) | (j++ == 7)) no short circuit is used. Therefore, both the expressions (8 > 4) and (j++
== 7)) are evaluated. The expression j++ increments the value j by 1.
13
Suppose that str is a String variable. Write a Java statement that uses the operator new to instantiate the object
str and assign the string “Java Programming” to str.
Ans:
str = new String (“Java Programming”)
14
Consider the following statements:
String str = “Going to the amusement park.”;
char ch;
int len;
int position;
a.
What value is stored in ch by the following statement?
ch = str.charAt(0);
The value stored in ch is ‘G’.
b.
What value is stored in ch by the following statement?
ch = str.charAt(11);
The value stored in ch is ‘e’.
c.
What value is stored in len by the following statement?
len = str.length();
The value stored in len is 28.
d.
What value is stored in position by the following statement?
position = str.indexOf(‘t’);
The value stored in position is 6
15
Rewrite the following decision statement using the conditional operator (?:).
If(page == odd)
System.out.println(“This is an odd page.”);
else
System.out.println(“This is an even page.”);
(5 marks)
Ans: System.out.println(“This is an “ + (page==odd ? “odd” : “even ”) + “page.”);
16
Assume the following statements:
String str;
String str1 = “diploma”;
String str2;
String str3;
str
= “Java programming: second programming module for diploma courses”;
What are the value of the following expressions?
(a) str.indexOf(“module”)
(b) str.substring(5, 16);
(c) str.startsWith(“Java”)
(d) str.startsWith(“J”)
(e) str.endsWith(“.”)
(f) str.regionMatches(48, str1, 0, str1.length());
(g) str.regionMatches(true, 31, “module”, 0, 6);
Note: the method header of regionMathces() are
Boolean regionMatches(int index, String str, int strIndex, int len);
Boolean regionMatches(boolean ignorcase, int index, String str, int strIndex, int len);
Ans:
(a) 37
(b) programming
(c) true
(d) true
(e) false
(f) true
(g) false
17
What is the result of the following piece of code?
public class C8Q9{
public static void main(String args[]){
if( 3 > 4 | 4 > 1 | 1 == 0){
System.out.println(" OR condition satisfied");
}
else{
System.out.println(" OR condition not satisfied");
}
}
}
Ans:
OR condition satisfied
18
What is the output of the following piece of code?
public class C8Q3{
public static void main(String args[]){
int i = 100;
System.out.println("Result: " + (( i < 100 ) ? 20.8 : 10 ));
}
}
Result: 10.0
19
The difference between char and Character is
Ans:
char is a primitive type; Character is a class