Question 3

Single Choice
Views 101

Answer:

Question 3

(a) State the number of bytes and bits occupied by a character array of 10 elements.

Answer

As size of char is 2 bytes, a character array of 10 elements occupies 2 x 10 = 20 bytes. 1 byte = 8 bits so 20 bytes convert to 8 x 20 = 160 bits.

(b) Differentiate between Binary Search and Linear Search techniques.

Answer

Linear Search Binary Search
Linear search works on sorted and unsorted arrays Binary search works on only sorted arrays (ascending or descending)
Each element of the array is checked against the target value until the element is found or end of the array is reached Array is successively divided into 2 halves and the target element is searched either in the first half or in the second half
Linear Search is slower Binary Search is faster

(c) What is the output of the following:

String a="Java is programming language \n developed by \t\'James Gosling\'";
System.out.println(a);

Answer

Below is the output of the program:

Java is programming language
developed by    'James Gosling'

(d) Differentiate between break and System.exit(0).

Answer

break System.exit(0)
break statement is used to jump out of the loop or switch-case System.exit(0) terminates the entire program
break is a keyword System.exit(0) is a method provided by Java standard library

(e) Write a statement in Java for √((a+b)3÷|a-b|)

Answer

Math.sqrt(Math.pow(a + b, 3) / Math.abs(a - b));

(f) What is the value of m after evaluating the following expression:
     m -= 9%++n + ++n/2; when int m=10,n=6

Answer

m = m - (9 % ++n + ++n / 2)
m = 10 - (9 % 7 + 8 / 2)
m = 10 - (2 + 4)
m = 10 - 6
m = 4

(g) Predict output of the following:

(i) Math.pow(25,0.5)+Math.ceil(4.2)

Answer

10.0
Explanation

Math.pow(25,0.5) ⇒ √25 = 5.0
Math.ceil(4.2) = 5.0
5.0 + 5.0 = 10.0

(ii) Math.round ( 14.7 ) + Math.floor ( 7.9)

Answer

22.0
Explanation

Math.round ( 14.7 ) = 15
Math.floor ( 7.9) = 7.0
15 + 7.0 = 22.0

(h) Give the output of the following java statements:

(i) "TRANSPARENT".toLowerCase();

Answer

transparent

(ii) "TRANSPARENT".compareTo("TRANSITION")

Answer

7
Explanation

compareTo method will return the difference in ASCII codes of the first differing characters of both strings. The first differing characters of TRANSPARENT and TRANSITION are P and I, respectively at the 6th position of the strings. Output will be:
ASCII code of 'P' - ASCII code of 'I'
= 80 - 73
= 7

(i) Write a java statement for each to perform the following task:

(i) Find and display the position of the last space in a string str.

Answer

System.out.println(str.lastIndexOf(' '));

(ii) Extract the second character of the string str.

Answer

System.out.println(str.charAt(1));

(j) State the type of errors if any in the following statements:
(i) switch ( n > 2 )

Answer

Syntax Error

Explanation

The expression of switch statement must resolve to byte, short, int, char or String. It cannot resolve to a boolean type so this statement will result in a syntax error.

(ii) System.out.println(100/0);

Answer

Runtime Error

Explanation

The program will compile successfully but when this line is executed it will result in "division by zero" error as dividing any number by 0 is an invalid division operation.

 

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.