|
No. |
Java Question |
Answer/Output |
Explanation |
|
1 |
String s1=Float.toString(); |
Compile Time Error |
Float.toString
method is overloaded
, one of the overloaded method declares no parameters
and
returns the value
wrapped by Float
instance and the other method acepts a
primitive
of type
float. In this case,
1.0 is double so it
cannot be implicity
narrowed to
type float. |
|
2 |
StringBuffer sb=new StringBuffer(-10); |
Run Time Error as NegativeArraySizeException
|
- |
|
3 |
static String s;
SOP(s instanceOf String); |
False |
s is refering to null. |
|
4 |
String s = new String();
SOP(s instanceOf String); |
True |
- |
|
5 |
String s = null;
SOP(s instanceOf String); |
False |
- |
|
6 |
StringBuffer sb = "abcd"; |
Compile Time Error |
- |
|
7 |
int x = 0 x 1234; |
No Error |
- |
|
8 |
String str = new String ("Computer");
if(str.equals("Computer"))
SOP("Success"); |
Success |
- |
|
9 |
Byte a = new Byte ("127");
SOP(a.toString() = = a.toString()); |
False |
- |
|
10 |
SOP("john" = = "john"); |
True |
- |
|
11 |
SOP("john".equals("john")); |
True |
- |
|
12 |
SOP("String".toString() = = "String"); |
Equal |
- |
|
13 |
SOP("String".trim() = = "String"); |
Not Equal |
- |
|
14 |
SOP("String".trim() = = "String ".trim()); |
Equal |
- |
|
15 |
SOP("String".substring(0) = ="String"); |
Equal |
- |
|
16 |
SOP("String".substring(0,6) = ="String"); |
Equal |
- |
|
17 |
SOP("String".replace('t','t') = = "String"); |
Equal |
- |
|
18 |
StringBuffer sb = new StringBuffer(25);
sb.setLength(0);
SOP(sb.length()); |
0 |
- |
|
19 |
StringBuffer sb = new StringBuffer(25);
sb.setLength(-1);
SOP(sb.length()); |
StringIndexOutOfBoundsException |
charAt() defined in StringBuffer class
throws an IndexOutOf
BoundsException if index
is negative or greater than or equal to length() |
|
20 |
StringBuffer sb = new StringBuffer(78);
SOP(sb.charAt(23)); |
StringIndexOutOfBoundsException |
Because sb.length()=0 |
|
21 |
String a = "some";
int b = 3;
SOP(a = = b); |
Compile Time Error |
If it is impossible to convert the type of either operand to the type of other by
a casting conversion while using = = or !=, then compile time error occurs. |
|
22 |
SOP("String".replace('T','t') = = "String"); |
Equal |
- |
|
23 |
SOP("String".replace('g','G') = = "String".replace('g','G');); |
False |
- |
|
24 |
SOP("String".endsWith("")); |
True |
- |
|
25 |
SOP("String",ertndsWith(" ")); |
False |
- |
|
26 |
SOP("String".startsWith("")); |
True |
- |
|
27 |
SOP("String".startsWith(" ")); |
False |
- |
|
28 |
public class A {
static String a;
static String b;
psvms(){
b = b + a;
SOP(b);
} |
nullnull |
- |
|
29 |
StringBuffer sb = new StringBuffer();
SOP(sb.toString()); |
No Compile Time Error and Runtime Error. The output is an empty line. |
- |
|
30 |
String a [] = new String [4];
String str = s[0].toUpperCase();
SOP(str); |
NullPointerException |
- |
|
31 |
String str = "abc" + 'd';
SOP(str); |
Compile Time Error |
- |
|
32 |
String str = "abc" + 1;
SOP(str); |
abc1 |
- |
|
33 |
String str = "abc" +"def";
SOP(str); |
abcdef
|
- |
|
34 |
String str = "abc" + 6.9;
SOP(tr); |
abc6.9 |
- |
|
35 |
String str = "abc" + null;
SOP(str); |
abcnull |
- |
|
36 |
char c= 'A';
string str = "sample";
c+=s; |
Compile Time Error |
- |
|
37 |
static String str;
SOP(s instatnceOf Sstring); |
False |
No Compile Time Error because s referes to null |
|
38 |
String str = new String();
SOP(str); |
Nothing |
Only empty line. |
|
39 |
StringBuffer sb = new StringBuffer();
SOP(sb); |
Nothing |
Only empty line. |
|
40 |
SOP(a.equals(a)); |
True |
- |
|
41 |
String str = "'\u0041'";
SOP(str); |
'A' |
- |
|
42 |
String str = "\u0041";
SOP(str); |
A |
- |
|
43 |
String str = "\uD7AF";
SOP(str); |
? |
- |
|
44 |
String str = "'null'";
SOP(str); |
'null' |
- |
|
45 |
String str = "'great'";
SOP(str); |
'great' |
- |
|
46 |
String a = new String();
String b = new String();
SOP(a+b); |
Empty Line |
- |
|
47 |
StringBuffer b1 = new StringBuffer();
StringBuffer b2 = new StringBuffer();
SOP(b1+b2); |
Compile Time Error |
+ cannot be applied to StringBuffer- |
|
48 |
static Object obj;
static String str;
static StringBuffer sb;
static int[] i;
SOP(obj); |
null |
- |
|
49 |
static Object obj;
static String str;
static StringBuffer sb;
static int[] i;
SOP(str); |
null |
- |
|
50 |
static Object obj;
static String str;
static StringBuffer sb;
static int[] i;
SOP(sb); |
null |
- |
|
51 |
static Object obj;
static String str;
static StringBuffer sb;
static int[] i;
SOP(i); |
null |
- |