Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Download 1.52 Mb.
Pdf ko'rish
bet18/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   14   15   16   17   18   19   20   21   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

036.
037.
CHAPTER
04
Operators
In the code snippet below, which lines will cause a
compilation error and why?
String str = “Hello”;
long lng = 99;
double dbl = 1.11;
int i = 1;
int j = 0;
j = i << str; //line 1
j = i << j; //line 2
j = i << dbl; //line 3
j = i << lng; //line 4
Answer:
Line 1 and Line 3 will cause a compilation error because they use
incompatible primitives. Line 1 uses the << with an int and
a String value whereas Line 3 uses the << operator with an int
and a double value.
What will be the output of the following code and why?
int x =–1;
x = x>>>24;
System.out.println(x);
Answer:


038.
039.
This code prints 255
Here, x is set to–1, which will set all 32 bits to 1 converting into
binary. The value is then shifted right by 24 bits, filling the first 24
bits with 0s, not considering the sign extension. This sets x to
255.
11111111 11111111 11111111 11111111 represents–1 in
binary >>>24
00000000 00000000 00000000 00000000 is the binary
version of 255.
Explain the operator available for string concatenation
with a code sample.
Answer:
The ‘+’ operator when used with Strings can be used for String
concatenation. The following code demonstrates this:
String sum = “one” + “two”;
In the above code, the Strings one and two are concatenated
and assigned to the String object sum.
What will be the output of the following code?
public class Test {
public static void main (String argument[]) {
String name = “Java”;
int iInt1 = 100;
int iInt2 = 200;
System.out.println(name + iInt1 + iInt2);
}
}



Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   14   15   16   17   18   19   20   21   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling