You are on page 1of 24

######Question 1- Give one example each of a primitive data type and a 

composite data type. 

ANSWER. Primitive Data Types – byte, short, int, long, float, double, char, 
boolean 

Composite Data Type – Class, Array, Interface 

######Question 2- Give one point of difference between unary and binary 


operators.  

ANSWER. A unary operator requires a single operand whereas a binary operator 


requires two operands. 

Examples of Unary Operators – Increment ( ++ ) and Decrement ( — ) Operators 

Examples of Binary Operators – +, -, *, /, % 

######Question 3- Differentiate between call by value or pass by value and 


call by reference or pass by reference. 

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. 

######Question 4- Write a Java expression for (under root of 2as+u2) 

ANSWER. Math.sqrt ( 2 * a * s + Math.pow ( u, 2 ) ) 

( or ) 

Math.sqrt ( 2 * a * s + u * u ) 

######Question 5- Name the types of error (syntax, runtime or logical error) 


in each case given below: 
(i) Division by a variable that contains a value of zero. 

(ii) Multiplication operator used when the operation should be division. 

(iii) Missing semicolon.  

ANSWER. 

(i) Runtime Error 

(ii) Logical Error 

(iii) Syntax Error 

######Question 6- Create a class with one integer instance variable. 


Initialize the variable using: 

(i) default constructor 

(ii) parameterized constructor.  

ANSWER. 

public class Integer  

int x; 

public Integer() { 

x = 0; 

public Integer(int num) { 

x = num; 


######Question 7- Complete the code below to create an object of Scanner 


class. 

Scanner sc = ___________ Scanner( ___________ ) 

ANSWER. Scanner sc = new Scanner ( System.in ); 

######Question 8- What is an array? Write a statement to declare an integer 


array of 10 elements. 

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 - 

int arr[] = new int[10]; 

######Question 9- Name the search or sort algorithm that: 

(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. (i) Selection Sort 

(ii) Binary Search 

######Question 10- Differentiate between public and private modifiers for 


members of a class. 

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?   

int a = 63, b = 36; 

boolean x = (a < b ) ? true : false; int y= (a > b ) ? a : b ; 

ANSWER. x = false 

y = 63 

######Question 12- State the values of n and ch 

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 += (x++) + (++x) + x; 

ANSWER. x = x + (x++) + (++x) + x 

x = 4 + 4 + 6 + 6 

x = 20 

######Question 14- Give the output of the following program segment: 

double x = 2.9, y = 2.5; 

System.out.println(Math.min(Math.floor(x), y)); 
System.out.println(Math.max(Math.ceil(x), y)); 

ANSWER.  

2.0 

3.0 

Explanation : Math.min(Math.floor(x), y) = Math.min ( 2.0, 2.5 ) = 2.0 

Math.max(Math.ceil(x), y)) = Math.max ( 3.0, 2.5 ) = 3.0 

######Question 15- State the output of the following program segment. 

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 

s.startsWith(s.substring(5, n)) = s.startsWith ( “nation” ) = false 

( s.charAt(2) == s.charAt(6) ) = ( ‘a’== ‘a’ ) = true 

######Question 16- State the method that: 

(i) Converts a string to a primitive float data type 

(ii) Determines if the specified character is an uppercase character 

ANSWER. (i) Float.parseFloat(String) 


(ii) Character.isUpperCase(char) 

######Question 17- State the data type and values of a and b after the 
following segment is executed. 

String s1 = "Computer", s2 = "Applications"; 

a = (s1.compareTo(s2)); 

b = (s1.equals(s2)); 

ANSWER. Data type of a is int and b is boolean. 

ASCII value of ‘C’ is 67 and ‘A’ is 65. So compare gives 67-65 = 2. 

Therefore a = 2 

b = false 

######Question 18- What will the following code output? 

String s = "malayalam"; 

System.out.println(s.indexOf('m')); 

System.out.println(s.lastIndexOf('m')); 

ANSWER. 

######Question 19- Rewrite the following program segment using while instead 
of for statement 

int f = 1, i; 

for (i = 1; i <= 5; i++)  


f *= i; 

System.out.println(f); 

ANSWER. 

int f = 1, i = 1; 

while (i <= 5)  

f *= i; 

System.out.println(f); 

i++; 

######Question 20- In the program given below, state the name and the value of 
the 

(i) method argument or argument variable 

(ii) class variable 

(iii) local variable 

(iv) instance variable 

class myClass { 

static int x = 7; 

int y = 2; 

 
public static void main(String args[]) { 

myClass obj = new myClass(); 

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); 

ANSWER. (i) name = n value =5 

(ii) name = y value = 7 

(iii) name = a value = 6 

(iv) name = obj value = new MyClass() 

######Question 21- What is meant by precedence of operators? ANSWER. 


Precedence of operators refers to the order in which the operators are applied 
to the operands in an expression. For example, * has higher precedence than +. 
So, the expression 8 + 2 * 5 will evaluate to 8 + 10 = 18 

######Question 22- What is a literal? ANSWER. A literal is a constant data 


item. There are different literals like integer literals, floating point 
literals and character literals. 
 

######Question 23- State the Java concept that is implemented through: i) a 


super class and a subclass. ii) the act of representing essential features 
without including background details. ANSWER. i) Inheritance ii) Abstraction. 

######Question 24- Give a difference between constructor and method. ANSWER. 


i)A constructor has no return type which a method has a return type. ii) The 
name of the constructor should be the same as that of the class while the name 
of a method can be any valid identifier. iii) A constructor is automatically 
called upon object creation while methods are invoked explicitly. 

######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 27- What is the difference between break and continue 


statements when they occur in a loop. ANSWER. The break statement terminates 
the loop while the continue statements current iteration of the loop to be 
skipped and continues with the next iteration. 

 
 

######Question 28- Write statements to show how finding the length of a 


character array and char[] differs from finding the length of a String object 
str. ANSWER. The length of a character array is found by accessing the length 
attribute of the array as shown below: char[] array = new char[7]; int 
lengthOfCharArray = array.length; The length of a String object is found by 
invoking the length() method which returns the length as an int. String str = 
“java”; int lengthOfString = str.length(); 

######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 30- What is an exception? ANSWER. An exception is an unforeseen 


situation that occurs during the execution of a program. In simpler words, 
they are the errors that occur during the execution of a program. The JRE 
throws an Exception object to indicate an exception which contains the 
information related to that exception. 

######Question 31- Write Java statement to create an object mp4 of class 


digital. ANSWER. digital mp4 = new digital(); 

######Question 32- State the values stored in variables str1 and str2 

String s1 = "good"; 
 

String s2="world matters"; 

String str1 = s2.substring(5).replace('t','n'); 

String str2 = s1.concat(str1); 

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 33- What does a class encapsulate? ANSWER. A class encapsulated 


the data (instance variables) and methods. 

######Question 34- Rewrite the following program segment using the if..else 
statement. comm =(sale>15000)?sale*5/100:0; ANSWER. 

if ( sale > 15000 ) { 

comm = sale * 5 / 100; 

} 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 37- Write a Java expression for ut + ½ ft2 ANSWER. u * t + 0.5 


* f * Math.pow ( t, 2) 

######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&lt;=5;i++) 

for(int j=1;j&lt;=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 41. What is the difference between an object and a class? 


ANSWER. 1. A class is a blueprint or a prototype of a real world object. It 
contains instance variables and methods whereas an object is an instance of a 
class. 2. A class exists in the memory of a computer while an object does not. 
3. There will be only one copy of a class whereas multiple objects can be 
instantiated from the same class. 

 
######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 45. What is difference between / and % operator? ANSWER. / is 


the division operator whereas % is the modulo (remainder) operator. a / b 
gives the result obtained on diving a by b whereas a % b gives the remainder 
obtained on diving a by b. 

######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: 

String n = “Computer Knowledge”; 

String m = “Computer Applications”; 

System.out.println(n.substring (0,8). concat (m.substring(9))); 

System.out.println(n.endsWith(“e”)); 

ANSWER. n.substring(0,8) gives “Computer”. m.substring(9) gives 


“Applications”. These two on concatenation gives “ComputerApplications”. n 
ends with “e”. So, it gives true. The output is: 

ComputerApplications 

true 

######Question 49. (d) Write the output of the following: (i) 


System.out.println (Character.isUpperCase(‘R’)); (ii) 
System.out.println(Character.toUpperCase(‘j’)); ANSWER. (i) true (ii) J 

######Question 50. (e) What is the role of keyword void in declaring 


functions? ANSWER. void indicates that the function doesn’t return any value. 

 
######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; 

k += k++ – ++j + k; 

System.out.println("k= " +k); 

System.out.println("j= " +j); 

ANSWER. 

k=6 

j = 10 

Explanation: k += k++ – ++j + k k = k + k++ – ++j + k k = 5 + 5 – 10 + 6 k = 6 


j = 10 as it has been incremented in the ++j operation. 

######Question 53. Output the given code 

double b = -15.6; 

double a = Math.rint (Math.abs (b)); 

System.out.println("a= " +a); 

 
ANSWER. 

a = 16.0 

Maths.abs(-15.6) will give 15.6 and Math.rint(15.6) gives 16.0. 

######Question 54. Explain the concept of constructor overloading with an 


example . ANSWER. A class can have more than one constructor provided that the 
signatures differ. This is known as constructor overloading. Example: 

class Age 

int age; 

public Age() 

age = -1; 

 
public Age(int age) 

this.age = age; 

######Question 55. Give the prototype of a function search which receives a 


sentence sentnc and a word wrd and returns 1 or 0 ? [2] ANSWER. 

public boolean function ( sentence sentnc, word wrd ) 

or 

public int function ( sentence sentnc, word wrd ) 

######Question 56. Write an expression in Java for z = (5×3 + 2y ) / ( x + y) 


[2] ANSWER. 

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 

######Question 59. What are library classes ? Give an example.  

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. 

######Question 61- Define the term Byte Code. 

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) 

######Question 62- What do you understand by type conversion?  


 

ANSWER. Type conversion or casting is the conversion of the data type of a 


literal from one type to another. There are tow types of types of casting – 
implicit casting and explicit casting. 

######Question 63- Name two jump statements and their use.  

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 64- What is Exception ? Name two Exception Handling Blocks.  

ANSWER. An Exception is an error which occurs during the execution of a 


program. The exception handling blocks are try, catch and finally. 

######Question 65- Write two advantages of using functions in a program.  

ANSWER. i) Function make code reusable. 

ii) Functions improve modularity and facilitate easy debugging. 

 
 

######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. 

Return type is int. 

ii) compareTo() lexicographically compares the String passed as an argument to 


the String on which it is invoked. 

Return type is int. 

######Question 67- What is the result stored in x, after evaluating the 


following expression  

int x = 5; 

x = x++ * 2 + 3 * –x; 

 
ANSWER. x = 5 * 2 + 3 * -6 

x = 10 – 18 

x = -8 

You might also like