Java Output Question For Practise (Looping)
Java Output Question For Practise (Looping)
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);
}
}
2.
public class Question2 {
}
/*
* 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.
Options:
1. FRIENDS
2. ENEMY
3. No Output
4. FRIENDS (Infinitely)
The answer is option (4)
Explanation
Because there is no updation
---------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------------------------
}
Output :- Minimum possible product is 6
System.out.println();
}
Output:- Total fine collected on date 9 is 700
------------------------------------------------------------------------------------------------------------------------------
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{
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) {
// codepoint at index 1
int val1 = str.codePointAt(1);
// codepoint at index 9
int val2 = str.codePointAt(9);
………………………………………………………………………………………………………………
25. public class Question25{
public static void main(String[] args)
{
String str1 = "This is Exercise 1";
String str2 = "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' };
}
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
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 = ' ';
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
………………………………………………………………………………………………………………
}
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";
------------------------------------------------------------------------------------------------------------------------------
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";
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
………………………………………………………………………………………………………………
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.
------------------------------------------------------------------------------------------------------------------------------