0% found this document useful (0 votes)
121 views29 pages

Java Output Question For Practise (Looping)

Uploaded by

amar.khera1504
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)
121 views29 pages

Java Output Question For Practise (Looping)

Uploaded by

amar.khera1504
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/ 29

Questions

1.
public class Question1 {
public static void main(String[] args) {
int[] arr= {-124,-11187,-128,-1157,-152,-21};
if(arr.length==0) {
System.out.println("No integers in the array");
System.exit(0);
}
int max=Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
if(max<arr[i]) {
max=arr[i];
}

}
System.out.println("Largest number is "+max);
}
}

Output:- Largest number is -21


------------------------------------------------------------------------------------------------------------------------------

2.
public class Question2 {

public static void main(String[] args) {


//BitwiseOR between current and next number in an array

int[] arr= {11,20,14,1,53,12};


int[] bitarr=new int[arr.length-1];

for (int i = 0; i < bitarr.length; i++) {


bitarr[i]=arr[i]|arr[i+1];
}
for (int i = 0; i < bitarr.length; i++) {
System.out.print(bitarr[i]+" ");
}
}

}
/*
* Explanation:-
* 11 | 20 = 31 stored at index 0 of resultant array
*/

Output:- 31 30 15 53 61

------------------------------------------------------------------------------------------------------------------------------
3. public class Question3 {
public static void main(String[] args) {
String str = "Java Programming";
char arr[] = new char[10];
str.getChars(0, 4, arr, 3);
System.out.println(arr);

}
}
Output
Java
Explanation
The syntax of the method is:
getChars(startindex, numOfCharacters, arrayName, space).
So from the string, starting from 0th index, first four characters are taken and 3 spaces are provided.
-----------------------------------------------------------------------------------------------------------------------------
4. public class Question4 {
public static void main(String[] args) {
String str = "Java Programming";
System.out.println(str.replace('a', '9'));
}
}
Output
J9v9 Progr9mming
Explanation
Here all the a in str = “Java Programming” are replaced with 9.
------------------------------------------------------------------------------------------------------------------------------
5. public class Question5 {
public static void main(String[] args) {
String test = "a1b2c3";
String[] tokens = test.split("\\d");
for(String s: tokens)
System.out.print(s);
}

}
Output
abc
Explanation
Split method available in String class uses Regular Expressions to split the strings.
//d refers to split the string based on digits
String tokens[] = s.split("//d") gives the array as {a,b,c}

------------------------------------------------------------------------------------------------------------------------------
6. public class Question6 {
public static void main(String[] args) {
String s1 = "s1 = " + "123" + "456";
String s2 = "s2 = " + (123+456);
System.out.println(s1);
System.out.println(s2);
}

}
Output
s1 = 123456
s2 = 579

Explanation

In string s1 123 and 456 are string as they are stored between double quotes so they are not added
but in string s2 123 and 456 are added as they are not string i.e they are inside bracket.

------------------------------------------------------------------------------------------------------------------------------
7. public class Question7
{
public int getData() //getdata() 1
{
return 0;
}
public long getData() //getdata 2
{
return 1;
}
public static void main(String[] args)
{
Test obj = new Test();
System.out.println(obj.getData());
}
}
Output
Compilation error

Explanation

For method overloading, methods must have different signatures. Return type of
methods does not contribute towards different method signature, so the code above give
compilation error. Both getdata 1 and getdata 2 only differ in return types and NOT signatures.

------------------------------------------------------------------------------------------------------------------------------
8.

public class Question8{


public static void main(String[] args)
{
int x = 1, y = 2;
do
System.out.println("FRIENDS");
while (x < y);
System.out.println("ENEMY");
}
}

Options:
1. FRIENDS
2. ENEMY
3. No Output
4. FRIENDS (Infinitely)
The answer is option (4)
Explanation
Because there is no updation
---------------------------------------------------------------------

9. public class Question9 {


public static void main(String[] args) {
// Divide arrays into two equal parts then multiply their sum
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
int firstPart=0,secondPart=0;
for (int i = 0,j=arr.length-1; i < j; i++,j--) {
firstPart+=arr[i];
secondPart+=arr[j];
}
System.out.println("Product of first and second sub-array is "+(firstPart*secondPart));
}
}
Output :- Product of first and second sub-array is 260
------------------------------------------------------------------------------------------------------------------------------

10 class Question10 {
public static void main(String[] args)
{
do {
System.out.print(1);
do {
System.out.print(2);
} while (false);
} while (false);
}
}
Output :- 12
------------------------------------------------------------------------------------------------------------------------------

12. public class A {


public static void main(String[] args)
{
System.out.println('j' + 'a' + 'v' + 'a');
}
}
Output
418
(106 + 97 + 118 + 97 = 418)
---------------------------------------------------------------------

13. public class Question4 {


public static void main(String[] args) {
// Find minimum product possible in array
int[] arr= {2, 7, 3, 4, 8, 5};
int min=Integer.MAX_VALUE;
// if we initialise min with 0 it will be always less than any of the product.
for (int i = 0; i < arr.length-1; i++) {
for (int j = i+1; j < arr.length; j++) {
int product=arr[i]*arr[j];
if(min>product)min=product;
}
}
System.out.println("Minimum possible product is "+min);

}
Output :- Minimum possible product is 6

14. class MainClass {


public static void main(String[] args)
{
int x = 10;
int y = 20;
switch (x) {
case 10:
System.out.println("HELLO");
break;
case y:
System.out.println("GEEKS");
break;
}
}
}
Output
Compile time error
(case label should be constant)

15. class Test1 {


public static void main(String[] args)
{
int arr[] = { 11, 22, 33 };
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");

System.out.println();

int arr2[] = new int[3];


arr2[] = { 11, 22, 33 };
for (int i = 0; i < arr2.length; i++)
System.out.print(arr2[i] + " ");
}
}
Output
Error
(arr2 is not a valid syntax)
------------------------------------------------------------------------------------------------------------------
16. public class Question16 {

public static void main(String[] args) {


/* Date fine array and car numbers in array is given.
* Collect fine from odd number car if date is even and vice versa.
* Calculate total fine collected.
*/
int[] cars= {2375, 7682, 2325, 2352};
int[] fine = {250, 500, 350, 200};
int date = 1+(int)(Math.random()*31);
boolean oddNumber=date%2==0;
int totalFine=0;
for (int i = 0; i < cars.length; i++) {
if(oddNumber==(cars[i]%2==1)) {
totalFine+=fine[i];
}
}
System.out.println("Total fine collected on date "+date+" is "+totalFine);
}

}
Output:- Total fine collected on date 9 is 700
------------------------------------------------------------------------------------------------------------------------------

17. public class Question17 {


//This Question is quite difficult
public static void main(String[] args) {
// rotate array to the left
// [1,2,3,4,5] -> [2,3,4,5,1] -> [3,4,5,1,2] just like this if rotate by is 3

int[] arr= {1,2,3,4,5};


int[] res=new int[arr.length];
int rotateBy=2;
int size=arr.length;
for (int i = size-1; i >=0; i--) {
if(i-rotateBy<0) {//if rotation goes before index 0 rotate it to rightmost;
res[size - Math.abs(rotateBy - i)]=arr[i];
}
else {
res[i-rotateBy]=arr[i];
}
}
for (int i = 0; i < res.length; i++) {
System.out.print(res[i]+" ");
}
}
}
Output:- 3 4 5 1 2
Explanation [1,2,3,4,5] -> [2,3,4,5,1] -> [3,4,5,1,2]
-
18.
public class Question18 {
public static void main(String[] args) {
// Find maximum and minimum numbers in array
int[] arr={1, 345, 234, 21, 56789,-3, 2, -1, 56, 10000, 167};
int max=Integer.MIN_VALUE,min=Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++) {
if(arr[i]<min)min=arr[i];
else if(arr[i]>max)max=arr[i];
// else if is used because either the number is maximum or minimum
}
System.out.println("Maximum number is "+max+" Minimum number is "+min);
}
}
Output:- Maximum number is 56789 Minimum number is -345
19.
public class Question19{

public static void main(String[] args) {


String org = "This is my java program";
String search = "is";
String sub = org.substring(5, 7);
boolean label = true;
if (sub == search) {
System.out.print(label);
} else {
System.out.print(!label);
}
}
}
Output:- False
………………………………………………………………………………………………….
20.
public class Question20{

public static void main(String[] args)


{
String s = "Banana Panama Vikrama Akram Dwakra";
print(s.indexOf("na"));
print(s.indexOf("na", s.indexOf("na")));
print(s.lastIndexOf("na"));
print(s.lastIndexOf("akr"));
print(s.lastIndexOf("akr", s.lastIndexOf("akr")));
System.out.println(s.substring(s.indexOf("ik")-1, s.lastIndexOf("wa")-s.indexOf("Pana")-1));
}

private static void print(int value)


{
System.out.print(value + "~");
}
}
Output:-
2~2~9~30~30~Vikrama
………………………………………………………………………………………………………………

21.
public class Question21{
public static void main(String args[])
{
String str = "Good Day.\n This is java program.";
System.out.println(removeCharAt(str, 9));
}
public static String removeCharAt(String s, int pos)
{
return s.substring(0, pos) + s.substring(pos + 1);
}
}
Output:-
Good Day. This is java program.
………………………………………………………………………………………………
22.
public class Question22{

public static void main(String a[]){


String result = frontTimes("Chocolate", 5);
System.out.println(result);
}
public static String frontTimes(String str, int n){
String result = "";
if (str.length() > 3){
for (int i = 0; i < n; i++){
result += str.substring(0, 3);
}
}
else {
for (int i = 0; i < n; i++) {
result += str;
}
}
return result;
}
Output:-

ChoChoChoChoCho
---------------------------------------------------------------------------------------------------------------------
23.
public class Question23{
public static void main(String[] args) {
String myStr = "Hello planet earth, you are a great planet.";
System.out.println(myStr.indexOf("e", 5));
}
}
Output:-
10
………………………………………………………………………………………………………………
24.
public class Question24{
public static void main(String[] args) {

String str = "w3resource.com";


System.out.println("Original String : " + str);

// codepoint at index 1
int val1 = str.codePointAt(1);

// codepoint at index 9
int val2 = str.codePointAt(9);

// prints character at index1 in string


System.out.println("Character(unicode point) = " + val1);
// prints character at index9 in string
System.out.println("Character(unicode point) = " + val2);
}
}
Output:-
Original String : w3resource.com
Character(unicode point) = 51
Character(unicode point) = 101

………………………………………………………………………………………………………………
25. public class Question25{
public static void main(String[] args)
{
String str1 = "This is Exercise 1";
String str2 = "This is Exercise 2";

System.out.println("String 1: " + str1);


System.out.println("String 2: " + str2);

// Compare the two strings.


int result = str1.compareTo(str2);

// Display the results of the comparison.


if (result < 0)
{
System.out.println("\"" + str1 + "\"" +
" is less than " +
"\"" + str2 + "\"");
}
else if (result == 0)
{
System.out.println("\"" + str1 + "\"" +
" is equal to " +
"\"" + str2 + "\"");
}
else // if (result > 0)
{
System.out.println("\"" + str1 + "\"" +
" is greater than " +
"\"" + str2 + "\"");
}
}
}
Output:-
String 1: This is Exercise 1
String 2: This is Exercise 2
"This is Exercise 1" is less than "This is Exercise 2"

………………………………………………………………………………………………
26.
public class Question26{
public static void main(String [] args) {
String str1 = "example.com", str2 = "Example.com";
String cs = "example.com";
System.out.println("Comparing "+str1+" and "+cs+": " + str1.contentEquals(cs));
System.out.println("Comparing "+str2+" and "+cs+": " + str2.contentEquals(cs));
}
}
Output:-
Comparing example.com and example.com: true
Comparing Example.com and example.com: false
………………………………………………………………………………………………………………
27.
public class Question27{
public static void main(String[] args)
{
// Character array with data.
char[] arr_num = new char[] { '1', '2', '3', '4' };

// Create a String containig the contents of arr_num


// starting at index 1 for length 2.
String str = String.copyValueOf(arr_num, 1, 3);

// Display the results of the new String.


System.out.println("\nThe book contains " + str +" pages.\n");
}
}
Output:-
The book contains 234 pages.
………………………………………………………………………………………………………………
28.
public class Question28{
public static void main(String[] args)
{
String str = "The quick brown fox jumps over the lazy dog.";

// Get a substring of the above string starting from


// index 10 and ending at index 26.
String new_str = str.substring(10, 26);

// Display the two strings for comparison.


System.out.println("old = " + str);
System.out.println("new = " + new_str);
}
}
Output:-
old = The quick brown fox jumps over the lazy dog.
new = brown fox jumps

29. public class Question29{


public static void main(String[] args)
{
String str = "The Quick BroWn FoX!";

// Convert the above string to all lowercase.


String lowerStr = str.toLowerCase();

// Display the two strings for comparison.


System.out.println("Original String: " + str);
System.out.println("String in lowercase: " + lowerStr);
}
}
Output:-
Original String: The Quick BroWn FoX!
String in lowercase: the quick brown fox!
………………………………………………………………………………………………………………
30.
public class Question30{
public static void main(String[] args) {
String str1 = "gibblegabbler";
System.out.println("The given string is: " + str1);
for (int i = 0; i < str1.length(); i++) {
boolean unique = true;
for (int j = 0; j < str1.length(); j++) {
if (i != j && str1.charAt(i) == str1.charAt(j)) {
unique = false;
break;
}
}
if (unique) {
System.out.println("The first non repeated character in String is: " + str1.charAt(i));
break;
}
}
}

}
Output:-
The given string is: gibblegabbler
The first non repeated character in String is: i

………………………………………………………………………………………………………………
31.
public class Question31{
public static void main(String[] args) {
String str1 = "the quick brown fox";
String str2 = "queen";
System.out.println("The given string is: " + str1);
System.out.println("The given mask string is: " + str2);
char arr[] = new char[str1.length()];
char[] mask = new char[256];
for (int i = 0; i < str2.length(); i++)
mask[str2.charAt(i)]++;
System.out.println("\nThe new string is: ");
for (int i = 0; i < str1.length(); i++) {
if (mask[str1.charAt(i)] == 0)
System.out.print(str1.charAt(i));
}
}

}
Output:-
The given string is: the quick brown fox
The given mask string is: queen

The new string is:


th ick brow fox
………………………………………………………………………………………………………………

32.
public class Question32{
static final int N = 256;
static char MaxOccuringChar(String str1) {
int ctr[] = new int[N];
int l = str1.length();
for (int i = 0; i < l; i++)
ctr[str1.charAt(i)]++;
int max = -1;
char result = ' ';

for (int i = 0; i < l; i++) {


if (max < ctr[str1.charAt(i)]) {
max = ctr[str1.charAt(i)];
result = str1.charAt(i);
}
}

return result;
}
public static void main(String[] args) {
String str1 = "test string";
System.out.println("The given string is: " + str1)
System.out.println("Max occurring character in the given string is: " + MaxOccuringChar(str1));
}

}
Output:-
The given string is: test string
Max occurring character in the given string is: t
………………………………………………………………………………………………………………
33.
public class Question33
public static void main(String args[])
{
String str1 = "java";
char arr[] = { 'j', 'a', 'v', 'a', ' ', 'p',
'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g' };
String str2 = new String(arr);
System.out.println(str1);
System.out.println(str2);
}
}
Output:-
java
java programming
………………………………………………………………………………………………………………

34 .public class Question34{

public static void main(String[] args) {


// Check for Arithmetic Progression in array
int d = 31;
int[] arr= {97,128,159,190,221,252,283,314,345};
int a=arr[0];
boolean inAP=true;
for (int i = 0,j=a; i < arr.length; i++,j+=d) {
if(arr[i]!=j) {
inAP=false;
break;
}
}
System.out.println("Is this array in AP ? "+inAP);
}
}

Output :- Is this array in AP ? true


35.
………………………………………………………………………………………………………………
public class Question35{
public static void main(String args[]){
//there are 2 's' characters in this sentence
String s1="this is index of example";
//returns last index of 's' char value
int index1=s1.lastIndexOf('s');
System.out.println(index1); //6
}
}
Output:-
6
………………………………………………………………………………………………………………
36.Which of these is not the correct answer?
public static void main(String[] args) {
for(int i=0;i<3;i++) {
for(int j=0;j<2;j++) {
int number = (int)(Math.random()*5);
System.out.println(number);
}
}

}
A.2
B.3
C.4
D.5
Correct answer-D
………………………………………………………………………………………………………………
37.What will be the value of y?
public static void main(String[] args) {
int x=2,y=50;
do
{
++x;
y-=x++;
}
while(x<=10);
System.out.println(y);

}
Output-15
-----------------------------------------------------------------------------------------------------------------------------
38.What will be 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));
Output-
false
JAI
………………………………………………………………………………………………………………
39. What will be the output?
public class Test
{
public static void main(String[] args)
{
for (int i = 0; i < 1; System.out.println("WELCOME"))
{
System.out.println("GEEKS");
}
}
}
Output-
GEEKS WELCOME(Infinitely)

------------------------------------------------------------------------------------------------------------------------------
40.public class Question40 {
public static void main(String[] args){

String s = "friends";
int x = 0;

do {
System.out.print(s.charAt(x));
x++ ;
} while (x < 2);

}
}

(A) friends
(B) friend
(C) fr
(D) compilation Error

Correct Answer-C
---------------------------------------------------------------------------------------------------------------------------
38.
public class Question38 {
public static void main(String args[]){
//there are 2 's' characters in this sentence
String s1="this is index of example";
//returns last index of 's' char value
int index1=s1.lastIndexOf('s');
System.out.println(index1);//6
}
}
Output:-
6
------------------------------------------------------------------------------------------------------------------------------
39.
public class Question39 {
public static void main(String[] args) {
String str = "This is last index of example";
int index = str.lastIndexOf("of");
System.out.println(index);
}
}
Output:-
19
------------------------------------------------------------------------------------------------------------------------------

40.
public class Question40 {
public static void main(String[] args) {
String str = "This is last index of example";
int index = str.lastIndexOf("of", 25);
System.out.println(index);
index = str.lastIndexOf("of", 10);
System.out.println(index); // -1, if not found
}
}
Output:-
19
-1

------------------------------------------------------------------------------------------------------------------------------
41.
public class Question41 {
public static void main(String[] args) {
String s1="javatpoint";
String s2="javatpoint";
String s3="JAVATPOINT";
String s4="python";

//true because content and case both are same


System.out.println(s1.equalsIgnoreCase(s2));

//true because case is ignored


System.out.println(s1.equalsIgnoreCase(s3));

//false because content is not same


System.out.println(s1.equalsIgnoreCase(s4));
}
}
Output:-
true
true
false

------------------------------------------------------------------------------------------------------------------------------
42.
public class Question42 {
public static void main(String[] args) {
String s1="hello";
String s2="hello";
String s3="meklo";
String s4="hemlo";
String s5="flag";

//0 because both are equal


System.out.println(s1.compareTo(s2));

//-5 because "h" is 5 times lower than "m"


System.out.println(s1.compareTo(s3));

//-1 because "l" is 1 times lower than "m"


System.out.println(s1.compareTo(s4));

//2 because "h" is 2 times greater than "f"


System.out.println(s1.compareTo(s5));
}
}
Output:-
0
-5
-1
2
------------------------------------------------------------------------------------------------------------------------------
43. What is the output of the following program?
public class Question43
{
public int getData()
{
return 0;
}
public long getData()
{
return 1;
}
public static void main(String[] args)
{
Test obj = new Test();
System.out.println(obj.getData());
}
}
a) 1
b) 0
c) Runtime error
d) Compilation error
Ans. (d)
………………………………………………………………………………………………………………
44. public class Question44{

public static void main(String[] args) {


String s1 = "HELLO ITERIANS";
String s2 = " Hello Iterians ";
System.out.print(s1.equals(s2.trim()));
System.out.print(s1.equalsIgnoreCase(s2.trim()));
System.out.print(s1.equals(s2)); //false
System.out.print(s1.equalsIgnoreCase(s2));
}
}

Output : falsetruefalsetrue
………………………………………………………………………………………………………………
45.public class Question45{
public static void main(String[] args) {

S1String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
if(s1 == s2 ) {
if(s1 == s3)
System.out.println("s1, s2 and s3 are references to the same string object.");
else
System.out.println("s1 and s2 are references to the same string object but not s3");
}
else
System.out.println("s1 and s2 are references to two different string object.");
}
}

Output: s1 and are reference to the same String object but not s3
………………………………………………………………………………………………………………

46. public class Question46{


public static void main(String[] args) {

char[] ch = {'s', 'o', 'a', 'i', 't', 'e', 'r'};


String str = new String(ch);
for(int i = 0; i < str.length(); i++) {
System.out.print(str.substring(++i) + " ");
}
System.out.println(str);
}
}
Output: oaiter iter er soaiter
------------------------------------------------------------------------------------------------------------------------------
47.
public class Question4{
public static void main(String[] args) {
String str = new String("hahaha");
System.out.print(str.contains("haha"));
String s1 = " " + str.substring(0, 4).toUpperCase();
s1 += "....";
System.out.println(s1);
}
}

Output: true HAHA....


………………………………………………………………………………………………………………
48.
public class Question48{
public static void main(String[] args) {
int i = 4;
int[] arr = new int[i];
arr[i-1] = 56;
arr[i-2] = 67;
arr[i-4] = 34;
for(int j = 0; j < arr.length; j++) {
System.out.print(arr[j] + " ");
}
}
}

Output: 34 0 67 56
………………………………………………………………………………………………………………
49.public class Question49{
public static void main(String[] args) {
char[] arr = new char[10];
for(int i = 0; i < arr.length; i++) {
System.out.print((int)arr[i] + " ");

Output: 0 0 0 0 0 0 0 0 0 0
------------------------------------------------------------------------------------------------------------------------------
50.
public class Question50{
public static void main(String[] args) {
String[] arr = {"ITER", "KIIT", "CET", "IIIT", "IITBBSR"};
for(int i = 1; i <= arr.length; i++) {
System.out.println(arr[i-1].substring(0, i));
}
}
}
Output:
I
KI
CET
IIIT
IITBB
………………………………………………………………………………………………………………
51.
public class Question51{
public static void main(String[] args) {
int[] arr = {1, 2, 3};
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
for(int j = 0; j <= i; j++)
System.out.print("Hello");
System.out.print(" ");

}
}
}
Output: 1Hello 2HelloHello 3HelloHelloHello
Explanation: For method overloading, methods must have different signatures.
Return type of methods does not contribute towards different method signature, so the code
above gives compilation error.
Both getdata 1 and getdata 2 only differ in return types and NOT signatures.
………………………………………………………………………………………………………………
52.What is the output of the following program?
public class Question52
{
void show()
{
System.out.println("SHOW Method..");
return;
}
public static void main(String[] args)
{
TestingMethods2 t2 = new TestingMethods2();
t2.show();
}
}
A) SHOW Method
B) No output
C) Compiler error
D) None
A
Explanation:
Yes. A void method can use an empty return statement.
………………………………………………………………………………………………………………
53.What is the output of the below Java program with a "this" operator?
public class Question53
{
int cakes=5;
void order(int cakes)
{
this.cakes = cakes;
}
public static void main(String[] args)
{
TestingMethods4 t4 = new TestingMethods4();
t4.order(10);
System.out.println("CAKES=" + t4.cakes);
}
}
A) CAKES=5
B) CAKES=0
C) CAKES=10
D) Compile error
C
Explanation:
In the program, this.cakes refers to the instance variable cakes.

------------------------------------------------------------------------------------------------------------------------------

54.
public class Question54{
public static void main(String[] args)
{
StringBuffer s = new StringBuffer("Geeksfor");
s.append("Geeks");
// returns GeeksforGeeks
System.out.println(s);
s.append(1);

// returns GeeksforGeeks1
System.out.println(s);
}
}
Output:-
GeeksforGeeks
GeeksforGeeks1
………………………………………………………………………………………………………………
55.
public class Question55 {
public static void main(String[] args)
{
StringBuffer s = new StringBuffer("GeeksGeeks");
s.reverse();
System.out.println(s); // returns skeeGrofskeeG
}
}
Output:-
skeeGskeeG
………………………………………………………………………………………………………………
56.
public class Question56 {
public static void main(String[] args)
{
for (int k = 0; k < 20; k+=2) {
if (k % 3 == 1)
System.out.println(k + " ");
}
A. 0 2 4 6 8 10 12 14 16 18
B. 0 6 12 18
C. 1 4 7 10 13 16 19
D. 4 10 16
Answer-D
This will loop with k having a value of 0 to 18 (it will stop when k = 20).
It will print out the value of k followed by a space when the remainder of dividing k by 3 is 1.
------------------------------------------------------------------------------------------------------------------------------

You might also like