Font size
WorksheetsQUIZ-5 static, String and StringBuffer
Total questions: 20
Worksheet time: 20mins
The keyword "static" in Java is used with
Method
Block
Variable
All the Above
Which of the following variable is declared as static?
Home address of a particular Student
Account number of a Customer
Company name of all Employees in an Organization
Name of a Employee
What is the output of the following code snippet?
public class Demo
{
public static void main(String[] args)
{
System.out.println("Done ");
}
static
{
System.out.print("Hi ");
}
static
{
System.out.print("Well ");
}
}
Done Hi Well
Hi Well Done
Well Done Hi
Done Well Hi
What is the output of the following code?
public class Demo
{
public void show(int a)
{
System.out.println(a);
}
public static void main(String[] args)
{
show(20);
}
}
20
No Output Displayed
a constant is passed as argument
Compile Time Error
Which of the following is not a correct statement?
A class can have any number of static blocks
Static method is accessed directly using class name
Separate memory is allocated for each object for all static variables
Static method is executed before main() method once a class is loaded
Static variables belongs to an Object?
Yes
No
What is the output of the following code?
public class Demo
{
static int x=10;
int y =15;
public Demo()
{
x++;
y++;
}
public static void main(String[] args)
{
Demo d1= new Demo();
Demo d2= new Demo();
Demo d3= new Demo();
System.out.println(d3.x + " , " +d3.y);
}
}
13,15
13,16
13,18
10, 18
What is the output of the code segment?
public class Demo
{
static int a = 20;
public static void main(String args[])
{
add();
System.out.print(a);
}
private static void add()
{
a = a++ + 20;
}
}
40
41
20
21
Which of the following is Mutable object
String
StringBuffer
The method of String class used to obtain index of the first occurrence of a specified string or character.
charAt()
getString()
substring()
indexOf()
What is the output of the following code snippet?
public class Demo{
public static void main(String args[]) {
String s = "String is ";
s.concat("Immutable ");
System.out.println(s+"Immutable");
}
}
String is
String is Immutable
String is Immutable Immutable
String is Immutable String is
What is the output of the following String Program?
public class Demo
{
public static void main(String args[]){
String s1 = "Wel Done";
x=s1.startsWith("Done");
System.out.println(x);
}
}
True
False
Done
Wel Done
The output displayed is………
public class Demo
{
public static void main(String args[]){
String str="CoronaVirus";
System.out.println(str.substring(2,6));
}
}
ronavirus
rona
ronaV
Corona
Predict the output of the following code segment.
public class Demo
{
public static void main(String args[]){
String s1 ="welcome";
String s2 ="welcome";
if(s1==s2)
System.out.println("Pune");
else
System.out.println("Hyderbad");
}
}
Pune
Hyderabad
Cannot be used with strings
Compile Time Error
The output displayed after the completion of the code?
public class Demo{
public static void main(String args[]){
String str= "welcome to Java Strings";
String[] s= str.split(" ");
System.out.println(s.length);
}
}
23
20
7
4
The result of the following program is……
public class Demo
{
public static void main(String args[]){
String str1="Welcome";
String str2="Weldone";
int x= str1.compareTo(str2);
System.out.println(x);
}
}
1
-1
0
Invalid Method
The result of the following code
public class Demo
{
public static void main(String args[]){
String str1="Welcome"; //Line-1
String str2=new String(str1); //Line-2
if(str1.equals(str2))
System.out.println("Java");
else
System.out.println("Python");
}
}
Python
Java
Error in Line2
Error in Line1
Identify the result of the following code.
public class Demo{
public static void main(String args[]){
String str = "Student";
System.out.println(str.reverse()); // Line-1
}
}
a) Student
a) tnedutS
a) Error in Line-1
a) Compile Time Error
The output of the following code
public class Demo
{
public static void main(String args[]){
String str = "Java is Life";
//single space between words
StringBuffer sb = new StringBuffer(str);
sb.delete(4,8);
System.out.println(sb);
}
}
Java Life
Javas Life
JavaLife
Java is
Which of the following method is defines in String class?
insert(index,string)
replace(char1,char2)
reverse()
delete(start,end)
