wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

QUIZ-5 static, String and StringBuffer

Total questions: 20

Worksheet time: 20mins

Name
Class
Date
1.

The keyword "static" in Java is used with

a)

Method

b)

Block

c)

Variable

d)

All the Above

2.

Which of the following variable is declared as static?

a)

Home address of a particular Student

b)

Account number of a Customer

c)

Company name of all Employees in an Organization

d)

Name of a Employee

3.

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 ");

}

}

a)

Done Hi Well

b)

Hi Well Done

c)

Well Done Hi

d)

Done Well Hi

4.

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);

}

}

a)

20

b)

No Output Displayed

c)

a constant is passed as argument

d)

Compile Time Error

5.

Which of the following is not a correct statement?

a)

A class can have any number of static blocks

b)

Static method is accessed directly using class name

c)

Separate memory is allocated for each object for all static variables

d)

Static method is executed before main() method once a class is loaded

6.

Static variables belongs to an Object?

a)

Yes

b)

No

7.

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);

}


}

a)

13,15

b)

13,16

c)

13,18

d)

10, 18

8.

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;

}

}

a)

40

b)

41

c)

20

d)

21

9.

Which of the following is Mutable object

a)

String

b)

StringBuffer

10.

The method of String class used to obtain index of the first occurrence of a specified string or character.

a)

charAt()

b)

getString()

c)

substring()

d)

indexOf()

11.

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");

}

}

a)

String is

b)

String is Immutable

c)

String is Immutable Immutable

d)

String is Immutable String is

12.

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);

}

}

a)

True

b)

False

c)

Done

d)

Wel Done

13.

The output displayed is………

public class Demo

{

public static void main(String args[]){

String str="CoronaVirus";

System.out.println(str.substring(2,6));

}

}

a)

ronavirus

b)

rona

c)

ronaV

d)

Corona

14.

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");

}

}

a)

Pune

b)

Hyderabad

c)

Cannot be used with strings

d)

Compile Time Error

15.

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);

}

}

a)

23

b)

20

c)

7

d)

4

16.

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);

}

}

a)

1

b)

-1

c)

0

d)

Invalid Method

17.

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");

}

}

a)

Python

b)

Java

c)

Error in Line2

d)

Error in Line1

18.

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)

a) Student

b)

a) tnedutS

c)

a) Error in Line-1

d)

a) Compile Time Error

19.

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);

}

}

a)

Java Life

b)

Javas Life

c)

JavaLife

d)

Java is

20.

Which of the following method is defines in String class?

a)

insert(index,string)

b)

replace(char1,char2)

c)

reverse()

d)

delete(start,end)