[go: up one dir, main page]

0% found this document useful (0 votes)
31 views13 pages

RT2 Logical Que - Solution

The document consists of a series of multiple-choice questions and answers related to Java programming concepts, including data types, access specifiers, encapsulation, and array manipulation. It covers topics such as wrapper classes, method initialization, and string operations, along with logical programming tasks. The document serves as a quiz or study guide for understanding Java fundamentals.

Uploaded by

asha.bluebirds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views13 pages

RT2 Logical Que - Solution

The document consists of a series of multiple-choice questions and answers related to Java programming concepts, including data types, access specifiers, encapsulation, and array manipulation. It covers topics such as wrapper classes, method initialization, and string operations, along with logical programming tasks. The document serves as a quiz or study guide for understanding Java fundamentals.

Uploaded by

asha.bluebirds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Choose the correct option.

1. Which of the following is a composite data type?


a. int b. String
c. char d. float
Ans. b. String
2. Which of the following is not a wrapper class?
a. Byte b. Int
c. Long d. Float
Ans. b. Int
3. Which of the following is valid method of initialising?
a. Integer a=new (5); b. Integer a=Integer(5);
c. Integer a=new Integer(5); d. Integer a=new (“5”);
Ans. d. Integer a=new (“5”);
4. If s=“123”, which among the following will convert it to an integer?
a. int a=Integer(s); b. int a=(int)s;
c. int a=parseInt(s); d. int a=Integer.parseInt(a);
Ans. d. int a=Integer.parseInt(a);
5. Which among the following is valid character initialisation?
a. Character c=new Character(‘c’); b. Character c=new Character(“c”);
c. Both a and b d. None of these
Ans. a. Character c=new Character(‘c’);
6. Which among the following methods return a true or a false?
a. toLowerCase() b. toUpperCase()
c. parseInt() d. isUpperCase()
Ans. d. isUpperCase()
7. What package is a part of the wrapper class which is imported by default into all Java programs?
a. java.util b. java.lang
c. java.awt d. None of these
Ans. b. java.lang
8. Which class is inherited by the Character and Boolean wrapper classes?
a. Object b. Number
c. Both a and b d. None of these
Ans. a. Object
9. What is, converting a primitive value into an object of the corresponding wrapper class called?
a. Autoboxing b. Unboxing
c. Type Casting d. All of these
Ans. a. Autoboxing
10. Which among the following function is used to check whether a given character is a tab spaceor not?
a. isTab() b. isTabSpace()
c. isSpace() d. isWhitespace()
Ans. d. isWhitespace()

11. Which access specifier allows accessibility of a member only within the same class where it isdeclared?
a. public b. private
c. protected d. default
Ans. b. private
12. Wrapping up of data and method into a single unit is called .
a. Encapsulation b. Abstraction
c. Inheritance d. Polymorphism
Ans. a. Encapsulation
13. An OOP feature that allows all members of a class to be the members of another class.
a. Encapsulation b. Abstraction
c. Inheritance d. Polymorphism
Ans. c. Inheritance
14. Which keyword allows Inheritance?
a. extend b. extends
c. for d. protected
Ans. b. private
15. Which access specifier allows accessibility by all classes in the same package, but only by
subclasses in a different package?
a. public b. private
c. protected d. default
Ans. c. protected
16. A static member a is declared in a class named ‘Myclass’, write the statement to initialise it with5, from a
function in another class.
a. a=5; b. a of Myclass=5;
c. Myclass.a=5; d. a.Myclass=5;
Ans. c. Myclass.a=5;
17. Which among the following best describes encapsulation?
a. It is a way of combining various data members into a single unit
b. It is a way of combining various member functions into a single unit
c. It is a way of combining various data members and member functions into a single unit whichcan
operate on any data
d. It is a way of combining various data members and member functions that operate on thosedata
members into a single unit
Ans. d. It is a way of combining various data members and member functions that operate onthose
data members into a single unit
18. If data members are private, what can we do to access them from the class object?
a. Create public member functions to access those data members
b. Create private member functions to access those data members
c. Create protected member functions to access those data members
d. Private data members can never be accessed from outside the class
Ans. a. Create public member functions to access those data members
19. Which feature can be implemented using encapsulation?
a. Inheritance b. Abstraction
c. Polymorphism d. Overloading
Ans. b. Abstraction
20. How can Encapsulation be achieved?
a. Using Access Specifiers b. Using only private members
c. Using inheritance d. Using Abstraction
Ans. a. Using Access Specifiers
21. Name the package that contains wrapper classes:
(a) java.lang
(b) java.util
(c) java .io
(d) java.awt
Ans. java.lang
22. Constructor overloading follows which principle of Object Oriented programming?
(a) Inheritance
(b) Polymorphism
(c) Abstraction
(d) Encapsulation
Ans. Polymorphism
23. Name the package that contains Scanner class:
a) java.lang
b) java.util
c)java .io
d) java.awt
Ans. java.util
24. Name the package that contains InputStreamReader class:
a) java.lang
b) java.util
c) java .io
java.awt Ans java .io

25. The second last element of an array of size 100, has index:
a. 97
b. 99
c. 98
d. 100
26. Given an array ar[]={10,20,30,40,50}, the statement ar[3]-1 will return:
a. 40
b. 30
c. 39
d. 29
27. What is the return type of a method that does not return any value?
a. int
b. void
c. float
d. null
28. How many values can return statement take back to the caller method?
a. 0
b. 1
c. 2
d. Many
29. Java statement to access the 5th element of an array is:
a. X[4]
b. X[5]
c. X[3]
d. X[0]
30. Give the output of the following statements:
String x[ ] = {"Apple", "Blueberry", "Grapefruit", "Tomato", "Watermelon"};
System.out.println(x[3].length());
a. 10
b. 5
c. 6
d. 9
31. Name the search algorithm which compares the sought key value with the key value of middle element of the array at
each stage.
(a) Bubble sort
(b) Linear search
(c) Binary search
(d) Selection sort
32. Which of the following statement contains an error?
a. int a[]= int[10];
b. float d[]={1,2,3};
c. int a[]= {1,2}; int b[]; b=a;
d. All the above
33. Predict the output:
System.out.println(Character.toLowerCase(‘E’));
a. e (b) 69E (c) false (d) None of these
34. What does the statement “int a=20” signifies?
a. It is a declaration statement where “a” is a variable holding 20 as its value.
b. The variable “a” is of type integer
c. All of these
d. None of these

35. int f= 10,m=9;


String e=(m%f==9)?"YES":"NO";
System.out.print(e);
a. YES
b. NO
c. YESNO
d. NOYES

36. How many bytes of memory will the following array use?
int b[ ] = new int[10];
(a) 40 bytes
(b) 24 bytes
(c) 48 bits
(d) 24 bits

37. How many bytes of memory will the following array use?
char b[ ] = new char[6];
(a) 48 bytes
(b) 12 bytes
(c) 48 bits
(d) 24 bits
38. How many bytes of memory will the following array use?
double b[ ] = new double[6];
(a) 48 bytes
(b) 24 bytes
(c) 48 bits
(d) 24 bits
39. How many bytes of memory will the following array use?
byte b[ ] = new byte[10];
(a) 10 bytes
(b) 24 bytes
(c) 48 bits
24 bits
40. The method compareTo() returns when two strings are equal andin lowercase :
(a) true
(b) 0
(c) 1
(d) False
41. The output of “Remarkable”.substring(6) is:
(a) mark
(b) emark
(c) marka
(d) able
42. Which of the following is the wrapper class for the data type char?
(a) String
(b) Char
(c) Character
(d) Float
43. Name the type of error, if any in the following statement:System.out.print("HELLO")
(a) logical
(b) no error
(c) runtime
(e) syntax

Logical questions.
1. Answer as directed:
a. Assign a integer 123 to an int variable a.
b. Convert the variable a to a String and store it in the variable s.
Ans. a. int a=123;
b. String s=Integer.toString(a);
2. Answer as directed:
a. Assign an string “123” to a String variable a.
b. Convert the variable a to an integer and store it in the variable s.
Ans. a. String a=”123”;
b. int s=Integer.parseInt(a);
3. Write one word answer for the following:
(i) A method that converts a string to a primitive integer data type
(ii) The default initial value of a boolean variable data type
Ans. (i) Integer.parseInt()
(ii) false
4. Write the output of the following:
(i) System.out.println (Character.isUpperCase(‘R’));
(ii) System.out.println(Character.toUpperCase(‘j’));
Ans. (i) true
(ii) J
5. State the method that:
(i) Converts a string to a primitive float data type
(ii) Determines if the specified character is an uppercase character
Ans. (i) Float.parseFloat()
(ii) Character.isUpperCase()
6. State the data type and the value of y after the following is executed:
char x =‘7’;
y = Character.isLetter(x);
Ans. y is of boolean type its value is false.
7. Write the return type of the following library functions:
(i) isLetterOrDigit(char)
(ii) parseInt()
Ans. (i) boolean
(ii) int
8. State the data type and value of res after the following is executed:
char ch=‘t’;
res=Character.toUpperCase(ch);
Ans. res is of char type is value is ‘T’.
9. 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);
Ans. Output:
Result 1= 26100200
Result 2=126
10. What is the difference between these two statements:
i. int sum[]=new int[10] ii. sum[1]=10;
Ans. The first statement declares an integer array of size 10 having the name ‘sum’. The second
statement initialises the second element of the array ‘sum’ with 10.
11. Write the correct form of the following arrays:
i. A1(2) ii. Name[2,5]
iii. Roll[2;] iv. Matrix(5)(5)
Ans. i. A1[2]
ii. Name[2][5]
iii. Roll[2]
iv. Matrix[5][5]
12. Give the proper array declaration for the following:
i. Declare an integer array, which can hold 25 values.
ii. Declare a two dimensional array called mat 3×4 of integer.
iii. Declare and initialise a two dimensional array of size 5×5 to 0.
Ans. i. int a[]=new int[25];
ii. int mat[][]=new int[3][4];
iii. int a[][]={{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}};
13. Give the output of the following program fragment:String s=new
String(“He went to the market”); String r;
r=s.replace(“went”,“is going”);
System.out.println(r);
Ans.
Output:
He is going to the market
14. Give the output of the following program fragment:String
s=“String”;
int a=12,b=45;
System.out.println(s+a+b);
System.out.println(a+s+b);
System.out.println(a+b+s);
Ans.
Output:
String1245
12String45
57String
15. Give the output of the following program fragment:String
s=“india”,s1=“IndIA”,s2=s;
System.out.println(s.equals(s1));
System.out.println(s.equalsIgnoreCase(s1));
System.out.println(s2==s);
System.out.println(s.toUpperCase()==s1.toUpperCase());
System.out.println(s.startsWith(“IN”.toLowerCase()));
System.out.println(s1.endsWith(“iA”.toUpperCase()));
Ans.
Output:
false
true
true
false
true
true

16. What will be the output for the following program segment?
String s = new String(“abc”);
System.out.println(s.toUpperCase());
Ans. ABC
17. What will be the output of the following code?
char x = ‘A’; int m;
m=(x= =’a’) ? ‘A’ : ‘a’;
System.out.println(“m=”+m);
Ans. m=97

18. Write a statement each to perform the following task on a string:


Find and display the position of the last space in a string s.
Convert a number stored in a string variable x to double data type
Ans.
(i) System.out.println(s.lastIndexOf(“ ”));
(ii) double d=Double.parseDouble(x);
19. Write a statement each to perform the following task on a string:
Extract the second last character of a word stored in the variable wd.
Check if the second character of a string str is in uppercase.
Ans.
(i) char sl=wd.charAt(wd.length()-2);
(ii) if(Character.isUpperCase(str.charAt(1)))
20. Give the output of the following string functions:
“ACHIEVEMENT”.replace(‘E’,‘A’)
“DEDICATE”.compareTo(“DEVOTE”)

21. If double age[] = {17, 15, 18, 16, 17), then write the value of (i) age[0] and (i) age{2].
Ans: (i) 17
(ii) 18
22. If String vowels| = {A", "E", "I", "O", "U'), then
i) How many elements are there in the array?
(ii) What is the value of vowels[2]?
(ii) What is the variable name of "0"?
Ans:
i. 5
ii. I
iii. vowels[3]
23. Consider the following code:
for(int i = 0; i< = 20; i++)
double arr[i] = Math.pow(i, 3);
Write the values of (i) arr[0], (ii) arr[2], (ii) arr[9].
Ans: (i) 0
(ii) 8
(ii) 729
24. If 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]+n[7]);
Ans: x = 343.0
Y=5.0
25. Write a statement to create an array containing
1.1, 1.7, 1.9, 1.2, 1.5, 1.2
Ans: float arr[]= {1.1, 1.7, 1.9, 1.2, 1.5, 1.2);
26. If the initial array in 90, 40, 64, 22, 81, 47, 68,20 .Write the next two arrays after the first two
iterations of a bubble sort technique used for sorting the array in ascending order.
Ans: 1st Iteration: 40, 90, 64, 22, 81, 47, 68, 20,
2nd Iteration: 40, 64, 90, 22, 81, 47, 68, 200
27. An array is defined as follows:
int A[][] ={{1,2), 13, 4}, (5, 6}};
Write the array as a matrix.
Ans:1 2
34
56
28. What will be the outupt of the following code?
char chars[] = {‘u’,’v’,’ w’,’ k’} ;
String str = new String(chars);
String s new String(str);
System.out.println(str);
System.out.printin(s);
Ans: uvwk
uvwk
29. Which elementis S[7] in
char S = {M', 'U,'G, 'H', 'A, L, 'E, 'H', T, 'N', 'D};
Ans: H
30. If array[] = {1,9, 8, 5, 2};
(a) What is array length?
(b) What is array|2]?
Ans: (a) 5
(b) 8
31. Given that int x[][]= {{2, 4, 6}, {3, 5, 7} };
what will be the value of x[1][0] and x[0][2]?
Ans: x[1][10] = 3 and x[0][2] = 6
32. State the total size in bytes, of the arraysa[4] of char data type and p[4] of float data type.
Ans: Size of a[4] is 8 types.
Size of p[4] is 16 bytes.
33. Give the outupt of the following:
int m[] = {2, 4, 6, 8};
System.out.println(m[1]+” ”+m[2]);
Ans: 4 6
34. int a[] = {2, 4,6, 8, 10};
a[0] = 23;
a3= a[1] ;
int c =a[0]+ a[1];
System.out.println("Sum = " +c);
Ans: Sum = 27
35. int a[]= new int[5];
a[0]= 4;
a[1]= 8;
a[2]= 7;
a[3] =12;
a[4]=3;
System.out.printin(a[2+2]);
Ans: 3
36. int a[4]= {2,4,6, 8};
for(int i = 0; i< = 1; i++)
int s=(a[i] +a[3-i]);
System.out.println(s);
Ans: 10
37. What will be the output of the following program segment?
String st = "PROGRAM";
System.out.printin(st.indexof(st.charAt(4));
Ans: 1
44. Consider the following String array and give the output:
String arr[]={“DELHI”, “CHENNAI”, “MUMBAI”, “LUCKNOW”, “JAIPUR”};
System.out.println(arr[0].length()>arr[3].length());
System.out.print(arr[4].substring(0,3));
Ans.false
JAI
45. String x[] = {“SAMSUMG”, “NOKIA”, “SONY”, “MICROMAX”, “BLACKBERRY”};
Give the output of the following statements:
a. System.out.println(x[1]);
b. System.out.println(x[3].length());
46. 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”);
Ans.0
Today i Holiday
47. Give the ouput of the following string functions:
a. “MISSISSIPPI”.indexOf(‘S’)+“MISSISSIPPI”.lastIndexOf(‘I’)
b. “CABLE”.compareTo(“CADET”)
Ans.
(i) 12
(ii) -2
48. State the output of the following program segment when executed:
String a = “Smartphone”, b = “Graphic Art”;
String h = a.substring(2, 5);
String k = b.substring(8).toUpperCase();
System.out.println(h);
System.out.println(k.equalsIgnoreCase(h));
Ans. art
True
49. State the output of the following program segment:
String str1 = “great”; String str2 = “minds”;
System.out.println(strl.substring(0,2).concat(str2.substring(1)));
System.out.println((“WH” + (strl.substring(2).toUpperCase())));
Ans.grinds
WHEAT
50. 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));
Ans. false
true
51. What will the following code output?
String s = “malayalam”;
System.out.println(s.indexOf(‘m’));
System.out.println(s.lastIndexOf(‘m’));
Ans.
0
8
52. 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”));
Ans Computer Applications
true
53. Predict the output:
a. “ARTIFICIAL”.endsWith(“AL”);
b. “Java_Basics”.trim().length();
Ans.(i) true
(ii)11
54. Predict the output:?
System.out.println(“four :” + 4 + 2);
System.out.println(“four :” + (2 + 2));
Ans four:42
four:4

55. What will be the output of the following program:


class Output
{
public static void main(String args[])
{
int a[]={6,5,4,3,2,1};
int x;
for(x=5;x>=0;x--)
{System.out.println(a[x]);
}}}
Ans.Output: 1
2
3
4
5
6

56. What will be the output of the following program:


class First
{
public static void main(String args[])
{
int a[]={5,1,15,20,25};
int i,j;
int m;
i=++a[1];
j=a[2]++;
m=a[i++];
System.out.print(i+“ ”+j+“ ”+m);
}
}
Ans. Output:
2 15 16
57. What will be the output of the following program:
class Output
{
public static void main(String args[])
{
int a,b=0;
int c[]={1,2,3,4,5,6,7,8,9,10};
for(a=0;a<10;a++)
{
if(a%2==0)
b+=c[a];
}
System.out.print(b);
}
}
In what statement, state what the above program is doing?
Ans. Output:
25
58. If int n[] = {1,2,3,4,5,6,7,9,13,16}, what are the values of x and y,
If x = Math.pow(n[4], n[2]);
y = Math.sqrt(n[6] + n[7]);
Ans.x=25.0 y=4.0
59. Give the output of the following code:
int a[4]={2,4,6,8};
for(i=0;i<=1;i++)
{
s=a[i]+a[3-i];
System.out.println(s);
}
Ans.
10
10
60. If double[] age = {17, 15, 24 , 56 , 22 ,78, 18, 16, 7,30); then write the value of
a) age[3]
b) age[9].
Ans.
a) age[3] is 56
b) age[9] is 30

61. Consider the following code:


for(int i = 0; i< = 20; i++)
double arr[i] = Math.pow(i, 3);
Write the values of
Ans.
a) arr[0] is 0
b) arr[2] is 8
c) arr[9] is 729
62. Given that int x[ | ={ {2, 4, 6}, {3, 5, 7}}
what will be the value of x[1][0] and x[0][2]?
Ans. x[1][0] is 3
x[0][2] is 6

You might also like