Mock Test 04
Mock Test 04
(iii) Read the following text, and choose the correct answer:
A class encapsulate Data Members that contains the information necessary
to represent the class and Member methods that perform operations on
the data member.
What does a class encapsulate?
a) Information and operation
b) Data members and Member methods
c) Data members and information
d) Member methods and operation
(iv) Assertion (A): In Java, statements written in lower case letter or upper
case letter are treated as the same.
Reason (R): Java is a case sensitive language.
a) Both Assertion (A) and Reason (R) are true and Reason (R) is a correct
explanation of Assertion (A)
b)Both Assertion (A) and Reason (R) are true and Reason (R) is not a
correct explanation of Assertion (A)
c) Assertion (A) is true and Reason (R) is false
d) Assertion (A) is false and Reason (R) is true
(v) The method compareTo() returns ............... when two strings are equal
and in lowercase :
a) true
b) 0
c) 1
d) false
vi) Which of the following is a valid Integer constant:
i. 4
ii. 4.0
iii. 4.3f
iv. "four"
a) Only i
b) i and iii
c) ii and iv
d) i and ii
xii) A single dimensional array contains N elements. What will be the last
subscript?
a) N
b) N – 1
c) N – 2
d) N + 1
xiii) Method that converts a character to uppercase is ............... .
a) toUpper()
b)ToUpperCase()
c) toUppercase()
d) toUpperCase(char)
xiv) The Scanner class method used to accept words with space:
a) next()
b) nextLine()
c) Next()
d) nextString()
xx) A method which does not modify the value of variables is termed as:
a) Impure method
b) Pure method
c) Primitive method
d) User defined method
2. Answer the following questions:[20]
a) Write the Java expression for (p + q)2.
c) The following code segment should print "You can go out" if you have
done your homework (dh) and cleaned your room (cr). However, the code
has errors. Fix the code so that it compiles and runs correctly.
d) Sam executes the following program segment and the answer displayed
is zero irrespective of any non zero values are given. Name the error. How
the program can be modified to get the correct answer?
e) How many times will the following loop execute? What value will be
returned?
int x = 2;
int y = 50;
do{
++x;
y -= x++;
}
while(x <= 10);
return y;
i) When there is no explicit initialization, what are the default values set for
variables in the following cases? (a) Integer variable (b) String variable
2. Write a program that reads a string and a character. Each occurrence of the
given character (regardless of its case) is converted to opposite case.
For example:
Input string: Amrit Pal Aggarwal
Input character: A
Output: amrit Pal aggArwAl
3. Using the switch statement, write a menu driven program to print the
following pattern depending on the user choosing 1 or 2 for an incorrect
choice, an appropriate error message should be displayed:
1) AEIOU
AEIO
AEI
AE
A
2) 1
1 10
1 10 100
1 10 100 1000
1 10 100 1000 10000
4. Define a class to accept values into a 3x3 array and check if it is a special
array or not. An array is special array if the if any one element is special
element. 95 108 13
Example: 10 999 145
Fig (1): 145 is a special element, because 1! +4! +5!
505 365 2
=145. Therefore, the 3x3 array is a SPECIAL!
Fig (2): Since 145 is not present in the array therefore it isn’t a special array.
951 245 100
895 514 89
598 111 112
5. Write a program in Java to accept a string. First display its capital letter
followed by small letters, digits and then other characters.
Ex: Input = ChiefTwit@Twitter:ElonMusk2022
Output = CTTEMhiefwititterlonusk2022@
Example 2:
Input: 103
Output: Duck number