a * Math.pow(x, 5) + b * Math.pow(x, 3) + c
(b) What is the value of x1 if x=5?
x1= ++x – x++ + --x
x1 = ++x – x++ + --x
⇒ x1 = 6 - 6 + 6
⇒ x1 = 0 + 6
⇒ x1 = 6
(c) Why is an object called an instance of a class?
A class can create objects of itself with different characteristics and common behaviour. So, we can say that an Object represents a specific state of the class. For these reasons, an Object is called an Instance of a Class.
(d) Convert following do-while loop into for loop.
int i = 1;
int d = 5;
do {
d=d*2;
System.out.println(d);
i++ ; } while ( i<=5);
int i = 1;
int d = 5;
for (i = 1; i <= 5; i++) {
d=d*2;
System.out.println(d);
}
(e) Differentiate between constructor and function.
| Constructor | Function |
|---|
| Constructor is a block of code that initializes a newly created object. | Function is a group of statements that can be called at any point in the program using its name to perform a specific task. |
| Constructor has the same name as class name. | Function should have a different name than class name. |
| Constructor has no return type not even void. | Function requires a valid return type. |
(f) Write the output for the following:
String s="Today is Test";
System.out.println(s.indexOf('T'));
System.out.println(s.substring(0,7) + " " +"Holiday");
Output of the above code is:
0
Today i Holiday
As the first T is present at index 0 in string s so s.indexOf('T') returns 0. s.substring(0,7) extracts the characters from index 0 to index 6 of string s. So its result is Today i. After that println statement prints Today i followed by a space and Holiday.
(g) What are the values stored in variables r1 and r2:
- double r1 = Math.abs(Math.min(-2.83, -5.83));
- double r2 = Math.sqrt(Math.floor(16.3));
- r1 has 5.83
- r2 has 4.0
- Math.min(-2.83, -5.83) returns -5.83 as -5.83 is less than -2.83. (Note that these are negative numbers). Math.abs(-5.83) returns 5.83.
- Math.floor(16.3) returns 16.0. Math.sqrt(16.0) gives square root of 16.0 which is 4.0.
(h) Give the output of the following code:
String A ="26", B="100";
String D=A+B+"200";
int x= Integer.parseInt(A);
int y = Integer.parseInt(B);
int d = x+y;
System.out.println("Result 1 = " + D);
System.out.println("Result 2 = " + d);
Output of the above code is:
Result 1 = 26100200
Result 2 = 126
- As A and B are strings so
String D=A+B+"200"; joins A, B and "200" storing the string "26100200" in D. - Integer.parseInt() converts strings A and B into their respective numeric values. Thus, x becomes 26 and y becomes 100.
- As Java is case-sensitive so D and d are treated as two different variables.
- Sum of x and y is assigned to d. Thus, d gets a value of 126.
(i) Analyze the given program segment and answer the following questions:
for(int i=3;i<=4;i++ ) {
for(int j=2;j<i;j++ ) {
System.out.print("" ); }
System.out.println("WIN" ); }
- How many times does the inner loop execute?
- Write the output of the program segment.
Inner loop executes 3 times.
(For 1st iteration of outer loop, inner loop executes once. For 2nd iteration of outer loop, inner loop executes two times.)
Output:
WIN
WIN
(j) What is the difference between the Scanner class functions next() and nextLine()?
| next() | nextLine() |
|---|
| It reads the input only till space so it can read only a single word. | It reads the input till the end of line so it can read a full sentence including spaces. |
| It places the cursor in the same line after reading the input. | It places the cursor in the next line after reading the input. |