Professional Documents
Culture Documents
ANSWER. Primitive Data Types – byte, short, int, long, float, double, char,
boolean
ANSWER. In call by value, a copy of the data item is passed to the method
which is called whereas in call by reference, a reference to the original data
item is passed. No copy is made. Primitive types are passed by value whereas
reference types are passed by reference.
( or )
Math.sqrt ( 2 * a * s + u * u )
ANSWER.
ANSWER.
{
int x;
public Integer() {
x = 0;
}
x = num;
}
}
ANSWER. An array is a reference data used to hold a set of data of the same
data type. The following statement declares an integer array of 10 elements -
(i) Makes several passes through the array, selecting the next smallest item
in the array each time and placing it where it belongs in the array.
(ii) At each stage, compares the sought key value with the key value of the
middle element of the array.
ANSWER. Variables and Methods whwith the public access modie the class also.
######Question 11- ######Question - What are the values of x and y when the
following statements are executed?
ANSWER. x = false
y = 63
char c = 'A':
int n = c + 1;
ANSWER. The ASCII value for ‘A’ is 65. Therefore, n will be 66.
######Question 13- What will be the result stored in x after evaluating the
following expression?
int x=4;
x = 4 + 4 + 6 + 6
x = 20
System.out.println(Math.min(Math.floor(x), y));
System.out.println(Math.max(Math.ceil(x), y));
ANSWER.
2.0
3.0
String s = "Examination";
int n = s.length();
System.out.println(s.startsWith(s.substring(5, n)));
System.out.println(s.charAt(2) == s.charAt(6));
ANSWER.
false
true
Explanation : n = 11
######Question 17- State the data type and values of a and b after the
following segment is executed.
a = (s1.compareTo(s2));
b = (s1.equals(s2));
ASCII value of ‘C’ is 67 and ‘A’ is 65. So compare gives 67-65 = 2.
Therefore a = 2
b = false
String s = "malayalam";
System.out.println(s.indexOf('m'));
System.out.println(s.lastIndexOf('m'));
ANSWER.
0
8
######Question 19- Rewrite the following program segment using while instead
of for statement
int f = 1, i;
{
f *= i;
System.out.println(f);
}
ANSWER.
int f = 1, i = 1;
{
f *= i;
System.out.println(f);
i++;
}
######Question 20- In the program given below, state the name and the value of
the
class myClass {
int y = 2;
public static void main(String args[]) {
System.out.println(x);
obj.sampleMethod(5);
int a = 6;
System.out.println(a);
}
void sampleMethod(int n) {
System.out.println(n);
System.out.println(y);
}
}
######Question 25- What are the types of casting shown by the following
examples? i) double x =15.2; int y =(int) x; ii) int x =12; long y = x;
ANSWER. i) Explicit casting ii) Implicit casting
######Question 26- Name any two wrapper classes. ANSWER. Byte, Short, Integer,
Long, Float, Double, Boolean, Character
######Question 29- Name the Java keyword that: (i) indicates a method has no
return type. (ii) stores the address of the currently calling object. ANSWER.
i) void ii) this
######Question 32- State the values stored in variables str1 and str2
String s1 = "good";
ANSWER. s2.substring(5) gives ” matters”. When ‘t’ is replaced with ‘n’, we
get ” manners”. “good” when concatenated with ” manners” gives “good manners”.
So, str1 = ” manners” and str2 = “good manners”.
######Question 34- Rewrite the following program segment using the if..else
statement. comm =(sale>15000)?sale*5/100:0; ANSWER.
} else {
comm = 0;
}
######Question 35- How many times will the following loop execute? What value
will be returned?
int x = 2, y = 50;
do{
++x;
y-=x++;
}while(x<=10);
return y;
ANSWER. In the first iteration, ++x will change x from 2 to 3. y-=x++ will
increase x to 4. And y becomes 50-3=47. In the second iteration, ++x will
change x from 4 to 5. y-=x++ will increase x to 6. And y becomes 47-5=42. In
the third iteration, ++x will change x from 6 to 7. y-=x++ will increase x to
8. And y becomes 42-7=35. In the fourth iteration, ++x will change x from 8 to
9. y-=x++ will increase x to 10. And y becomes 35-9=26. In the fifth
iteration, ++x will change x from 10 to 11. y-=x++ will increase x to 12. And
y becomes 26-11=15. Now the condition x<=10 fails. So, the loop executes five
times and the value of y that will be returned is 15.
######Question 36- What is the data type that the following library functions
return? i) isWhitespace(char ch) ii) Math.random() ANSWER. i) boolean ii)
double
######Question 38- If int n[] ={1, 2, 3, 5, 7, 9, 13, 16} what are the values
of x and y?
x=Math.pow(n[4],n[2]);
y=Math.sqrt(n[5]+[7]);
ANSWER.
n[4] is 7 and n[2] is 3. So, Math.pow(7,3) is 343.0. n[5] is 9 and n[7] is 16.
So Math.sqrt(9+16) will give 5.0. Note that pow() and sqrt() return double
values and not int values.
######Question 39- What is the final value of ctr after the iteration process
given below, executes?
int ctr=0;
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j+=2)
++ctr;
ANSWER.
Outer loop runs five times. For each iteration of the outer loop, the inner
loop runs 3 times. So, the statement ++ctr executes 5*3=15 times and so the
value of ctr will be 15.
######Question 40- Name the methods of Scanner class that: i) is used to input
an integer data from standard input stream. ii) is used to input a string data
from standard input stream. ANSWER. i) scanner.nextInt() ii) scanner.next()
######Question 42. What does the token ‘keyword’ refer to in the context of
Java? Give an example for keyword. ANSWER. Keywords are reserved words which
convey special meanings to the compiler and cannot be used as identifiers.
Example of keywords : class, public, void, int
######Question 43. State the difference between entry controlled loop and exit
controlled loop. ANSWER. In an entry controlled loop, the loop condition is
checked before executing the body of the loop. While loop and for loop are the
entry controlled loops in Java. In exit controlled loops, the loop condition
is checked after executing the body of the loop. do-while loop is the exit
controlled loop in Java.
######Question 44. What are the two ways of invoking functions? ANSWER. If the
function is static, it can be invoked by using the class name. If the function
is non-static, an object of that class should be created and the function
should be invoked using that object.
######Question 46. State the total size in bytes, of the arrays a [4] of char
data type and p [4] of float data type. ANSWER. char is two bytes. So a[4]
will be 2*4=8 bytes. float is 4 bytes. So p[4] will be 4*4=16 bytes.
######Question 47. (i) Name the package that contains Scanner class. (ii)
Which unit of the class gets called, when the object of the class is created?
ANSWER. (i) java.util (ii) Constructor
######Question 48. Give the output of the following:
System.out.println(n.endsWith(“e”));
ComputerApplications
true
######Question 51. Analyse the following program segment and determine how
many times the loop will be executed and what will be the output of the
program segment ?
int p = 200;
while(true)
{
if (p<100)
break;
p=p-20;
}
System.out.println(p);
ANSWER.p values changes as follows : 200, 180, 160, 140, 120, 100, 80. So, the
loop executes six times and value of p is 80.
######Question 52. What will be the output of the following code? (i)
int k = 5, j = 9;
ANSWER.
k=6
j = 10
double b = -15.6;
ANSWER.
a = 16.0
class Age
{
int age;
public Age()
{
age = -1;
}
public Age(int age)
{
this.age = age;
}
}
or
z = ( 5 * Math.pow ( x, 3 ) + 2 * y ) / ( x + y )
######Question 57. Write a statement each to perform the following task on a
string: (i) Find and display the position of the last space in a string s.
(ii) Convert a number stored in a string variable x to double data type
ANSWER. (i) System.out.println(s.lastIndexOf(” “); (ii) Double.parseDouble(x)
######Question 58. Name the keyword that: (i) informs that an error has
occurred in an input/output operation. (ii) distinguishes between instance
variable and class variables. ANSWER. (i) throw (ii) static
ANSWER. Library classes are the predefined classes which are a part of java
API. Ex: String, Scanner
######Question 60. Write one difference between Linear Search and Binary
Search . ANSWER. Linear search can be used with both sorted and unsorted
arrays. Binary search can be used only with sorted arrays.
ANSWER. The Java compiler compiles the source programs into an intermediate
code called the Java Byte Code which is interpreted by the Java Virtual
Machine (JVM)
ANSWER. break and continue are the two jump statements in Java. break is used
to force early termination of a loop. continue is used to move to the next
iteration of the loop while skipping the remaining code in the current
iteration.
######Question 66- State the purpose and return data type of the following
String functions:
(i) indexOf ( )
(ii) compareTo ( )
ANSWER. i) indexOf() returns the index of the character or String passed as
the parameter in the string on which is invoked.
int x = 5;
x = x++ * 2 + 3 * –x;
ANSWER. x = 5 * 2 + 3 * -6
x = 10 – 18
x = -8