1 - Vocational Training Council
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: 8 |
|2. | |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. [16 marks] |
| | | |
| | |if (florida == 1) { |
| | |System.out.println("Number 1 Ranking!"); |
| | |} |
| | |else if(florida == 2) { |
| | |System.out.println("Almost there ..."); |
| | |} |
| | |else { |
| | |System.out.println("Unacceptable"); |
| | |} |
| | | |
| | |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 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; |
| | |What value is stored in ch by the following statement? |
| | |ch = str.charAt(0); |
| | |The value stored in ch is ‘G’. |
| | |What value is stored in ch by the following statement? |
| | |ch = str.charAt(11); |
| | |The value stored in ch is ‘e’. |
| | |What value is stored in len by the following statement? |
| | |len = str.length(); |
| | |The value stored in len is 28. |
| | |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? |
| | | |
| | |str.indexOf(“module”) |
| | |str.substring(5, 16); |
| | |str.startsWith(“Java”) |
| | |str.startsWith(“J”) |
| | |str.endsWith(“.”) |
| | |str.regionMatches(48, str1, 0, str1.length()); |
| | |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 |
| | |programming |
| | |true |
| | |true |
| | |false |
| | |true |
| | |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 |
| | | |
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.