0% found this document useful (0 votes)
47 views

ND23 QB3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

ND23 QB3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 134

ND23 QB2

Additional Instructions:
None

Q1. Which package contains the PrintStream class?

java.lang.ref

java.util.*

java.io.*

None of the listed options

Q2. class is used to increase the efficiency of input operations.

DataInputStream

FileInputStream

BufferedInputStream

PipeInputStream

Q3. A Java program can run on which of the following operating systems?

Unix

Windows

Linux

All of the listed options

Q4. In Java byte, short, int and long all of these are
Signed

Unsigned

Both signed and unsigned

None of the listed options

Q5. Which of the following is true of a Java bytecode file?

It can be run on any computer with a compatible JVM.

It can only be executed on the same type of computer that it was


created on.

It can be easily read and modified in a standard text editor.

It requires the corresponding .java that created it to execute.

Q6. Which statements about calling the compilation command javac and the execution command java are true?

I. java may use a period to separate packages.


II. javac takes a .java file and returns a .class file.
III. java may use a slash (/) to separate packages.

I only

II only

I and II

I, II, and III

Q7. In Java main method returns a value of type

void

int

real

string

Q8. Which of these is used to perform all input & output operations in Java?
Streams

Variables

Classes

Methods

Q9. System class is defined in .

java.util package

java.lang package

java.io package

java.awt package

Q10. If a variable is marked as , every time the variable is used it must be read from the main memory. Similarly,
every time a variable is written, the value must be stored in the main memory.

shared

static

final

volatile

Q11. Which package is imported into every Java class by default?

java.util

java.lang

system.lang

java.system

Q12. Which of the following is not a property of a JVM?

It prevents Java bytecode from being easily decoded/decompiled.

It supports platform independence.


It manages memory for the application.

It translates Java instructions to machine instructions.

Q13. Java source code will be compiled into file.

.java

.class

.exe

.byte

None of the listed options

Q14. Evaluate (True or False) for each of the following

expressions: 14 <= 14
14 < 14
-9 > -25
-25 > -9

Tru
e
Tru
e
Tru
e
True
False
False
False

True
False
True
True

True
False
True
False

Q15. With x = 0, which of the following are legal lines of Java code for changing the value of x to 1?

1. x++;
2. x = x + 1;
3. x += 1;
4. x =+ 1;

1, 2 & 3
1&4

1, 2, 3 & 4

3&2

Q16. Increment operator ++ increases the value of the variable by what number?

Q17. Decrement operator −− decreases the value of the variable by what number?

Q18. Determine the value of the expression: 11 + 5 * 3 % (1 + 1)?

12

11

15

None of the listed options

Q19. x=x+10; can be written as:

x+=10;

x=+10;

x++10;
None of the listed options

Q20. Which statement about the ^ operator is correct?

If one of the operands of ^ is true, then the result is always true.

There is a conditional form of the operator, denoted as ^^.

If both operands of ^ are true, the result is true.

The ^ operator can be applied to int type.

Q21. What is the value of y after executing the following code snippet?
1 int x = 5;
2 int y = 2;
3 int z = x + (x>6 ? ++y : --y);

None of the listed options

Q22. What will be the output of the program?


1 class Equals
2 {
3 public static void main(String [] args)
4 {
5 int x = 100;
6 double y = 100.1;
7 boolean b = (x = y);
8 System.out.println(b);
9 }
10 }

True

False

Compilation fails
An exception is thrown at runtime

Q23. What is the output of the following program?


1 package transporter;
2 public class CarVsBus
3 {
4 public static void main(String[] arguments)
5 {
6 int car = 20 * (5 + (1 + 2) / 2);
7 int bus = car < 1 ? 5 : 20;
8 System.out.print(car < bus ? "Bus wins!" : "Car wins!");
9 }
10 }

Bus wins!

Car wins!

The code does not compile.

The code compiles but throws a division by zero error at runtime.

Q24. What will be the output of the program?


1 class Bitwise
2 {
3 public static void main(String [] args)
4 {
5 int x = 11 & 9;
6 int y = x ^ 3;
7 System.out.println( y | 12 );
8 }
9 }

14

Q25. Which of the following can be operands of arithmetic operators?

Numeric

Boolean

Characters

Both Numeric & Characters


Q26. Modulus operator, %, can be applied to which of these?

Integers

Floating–point numbers

Both Integers and floating–point numbers.

None of the listed options

Q27. What is the output of the following application?


1 public class Baby {
2 public static String play(int toy, int age) {
3 final String game;
4 if(toy<2)
5 game = age > 1 ? 1 : 10; // p1
6 else
7 game = age > 3 ? "Ball" : "Swim"; // p2
8 return game;
9 }
10 public static void main(String[] variables) {
11 System.out.print(play(5,2));
12 }
13 }

Ball

Swim

The code does not compile due to p1.

The code does not compile due to p2.

Q28. What variable type of red allows the following application to compile?
1 package tornado;
2 public class Kansas {
3 public static void main(String[] args) {
4 int colorOfRainbow = 10;
5 red = 5;
6 switch(colorOfRainbow) {
7 default:
8 System.out.print("Home
");
9 break;
10 case red:
11 System.out.print("Away");
12 }
13 }
14 }
long

double

int

None of the above

Q29. How many 1s are outputted when the following application is compiled and run?
1 package city;
2 public class Road {
3 public static void main(String... in) {
4 int intersections = 100;
5 int streets = 200;
6 if (intersections < 150) {
7 System.out.print("1");
8 }
9 else if (streets && intersections > 1000) {
10 System.out.print("2");
11 }
12 if (streets < 500)
13 System.out.print("1");
14 else
15 System.out.print("2");
16 }
17 }

The code doesn't compile.

One

Two

None of the listed options

Q30. What is the output of the following application?


1 package dessert;
2 public class IceCream {
3 public final static void main(String... args) {
4 int flavors = 30;
5 int eaten = 0;
6 switch(flavors) {
7 case 30: eaten++;
8 case 40: eaten+=2;
9 default: eaten--;
10 }
11 System.out.print(eate
n);
12 }
13 }

The code doesn't compile.


Q31. Which statement about case statements of a switch statement is not true?

A case value can be final.

A case statement must be terminated with a break statement.

A case value can be a literal expression.

A case value must match the data type of the switch variable, or
be able to be promoted to that type.

Q32. Given the following code snippet, assuming dayOfWeek is an int, what variable type of Saturday is not permitted?
1 final saturday = 6;
2 switch(dayOfWeek) {
3 default:
4 System.out.print("Another Weekday");
5 break;
6 case saturday:
7 System.out.print("Weekend!"); }

byte

long

int

None of the listen options

Q33. What is the output of the following code?


1 int hops = 0;
2 int jumps = 0;
3 jumps = hops++;
4 if(jumps)
5 System.out.print("Jump!");
6 else
7 System.out.print("Hop!");

Jump!

Hop!

The code doesn't compile.

Exception

Q34. statement provides an easy way to dispatch execution to different parts of your code based on
the value of an expression.
if-else

switch

if

while

Q35. A switch statement can have case statements and default statements.

at most one, at least one

any number of, at most one

at least one, any number of

at least one, at most one

Q36. Find the output of the Java program.


1 int a=15;
2 int b=25;
3 if ((a<b) || (a=5)>15)
4 System.out.println(a);
5 else
6 System.out.println(b);

Error

15

25

No Output

Q37. What is the output of the following code?


1 package planning;
2 public class ThePlan {
3 public static void main(String[] input) {
4 int plan = 1;
5 plan = plan++ + --plan;
6 if(plan==1) {
7 System.out.print("Plan
A");
8 }
9 else {
10 if(plan==2)
11 System.out.print("Plan
B");
12 }
13 else
14 System.out.print("Plan
C");
15 }
16 }
Plan A

Plan B

Plan C

The code doesn't compile.

Q38. What is the output of the following application?


1 package dinosaur;
2 public class Park {
3 public static void main(String[] args) {
4 int pterodactyl = 6;
5 long triceratops = 3;
6 if(pterodactyl % 3 >= 1)
7 triceratops++;
8 triceratops--;
9 System.out.print(triceratop
s);
10 }
11 }

The code doesn't compile.

Q39. Which of these are selection statements in Java?

if()

for()

continue

break

Q40. What will happen when you compile and run the following code?

1 public class Test{


2
3 public static void main(String[] args){
4 for(int i = 0; i < 3; i++){
5 for(int j = 0; j < i; j++){
6 System.out.print(i + " " + j
+ ", ");
7 }
8 }
9 }
10 }

0 0, 0 1, 0 2, 1 0, 1 1, 1 2, 2 0, 2 1, 2 2,
0 0, 0 1, 0 2, 0 3, 1 0, 1 1, 1 2, 1 3, 2 0, 2 1, 2 2, 2 3,

1 0, 2 0, 2 1,

None of the listed options

Q41. How many of the loop types (while, do while, traditional for, and enhanced for) allow you to write code that
creates an infinite loop?

One

Two

Three

Four

Q42. Which of the following iterates a different number of times than the others?

for (int k=0; k < 7; k++) {}

for (int k=1; k <= 7; k++) {}

int k=0; do { } while(k++ < 7)

int k=0; while (k++ < 7) {}

Q43. What will be the output of the following program?

1 public class Test {


2 public static void main(String[]
args) { 3
4 for (int i = 0; i < 10; i++)
5 int x = 10;
6 }
7 }

No Output

10

Compile Time Error

10 (10 times)

Q44. What will happen when you compile and run the following code?
1 public class Test{
2 public static void main(String []args){
3 int i = 0;
4 for(i = 0; i < 10; i+
+){
5 continue;
6 }
7 System.out.println(
i);
8 }
9 }

10

11

Compile Time Error

Q45. How many of these statements can be inserted after the println to have the code flow follow the arrow in
this diagram? break;
break label1;
break label2;

Three

One

Two

None of the listed options

Q46. What will happen when you compile and run the following code?

1 public class Test{


2 public static void main(String[] args){
3 int i = 0;
4 for(; i < 10; i++){
5 break;
6 }
7 System.out.println(
i);
8 }
9 }

1
9

Compile Time Error

Q47. Which of the following can loop through an array without referring to the elements by index?

do-while loop

for (traditional)

for-each

while

Q48. What will happen when you compile and run the following code?

1 public class Test{


2 public static void main(String[] args){
3 for(char c = 'a' ; c < 'd';
c++){
4 System.out.print(c);
5 }
6 }
7 }

Code will not compile

Code will print abc

Code will print 012

None of the listed options

Q49. What is the result of the following code?


1 do
2 {
3 int count = 0;
4 do {
5 count++;
6 }
7 while (count < 2);
8 System.out.println(count);
9 break;
10 } while
(true); 11

The code does not compile.


This is an infinite loop.

Q50. In while and do-while loops, a statement causes control to be transferred directly to the conditional
expression that controls the loop.

break

pause

start

continue

Q51. What will happen when you compile and run the following code?

1 public class Test{


2
3 public static void main(String[] args){
4 int i = 0;
5 for(i = 100; i < = 0; i -=
10 ){
6 System.out.print(i +
", ");
7 }
8 }
9 }

100, 90, 80, 70, 60, 50, 40, 20, 10, 0,

100, 90, 80, 70, 60, 50, 40, 20, 10,

90, 80, 70, 60, 50, 40, 20, 10, 0,

None of the listed options

Q52. Which of these is an incorrect statement?

It is necessary to use new operator to initialize an array

Array can be initialized using comma separated expressions


surrounded by curly braces

Array can be initialized when they are declared

None of the listed options

Q53. Predict the output of the following program.


1 class Test {
2 public static void main(String args[]) {
3 int arr[] = new int[2];
4 System.out.println(arr[0]);
5 System.out.println(arr[1]);
6 }
7 }
0
0

garbage value
garbage value

Compile Time Error

Exception

Q54. Unlike C-Arrays, the Java-Arrays have .

Names

Values

Methods and Fields

None of the listed options

Q55. The Java Virtual Machine (JVM) implements arrays as type.

Primitive

Object

Q56. Predict the output of the following program.


1 public class Main {
2 public static void main(String args[]) {
3 int arr[] = {10, 20, 30, 40,
50};
4 for(int i=0; i < arr.length; i+
+)
5 {
6 System.out.print(" " +
arr[i]);
7 }
8 }
9 }

10 20 30 40 50

10 20 30 40

10 20 30 40 50 60

Compile Time Error


Q57. What will be the output of the following Java code?
1 int arr[] = new int [5];
2 System.out.print(arr);

value stored in arr[0]

00000

Class name@ hashcode in hexadecimal form

Q58. Which of these is an incorrect array declaration?

int arr[] = new int[5]

int [] arr = new int[5]

int arr[5] = new int[]

int arr[] = int[5] new

Q59. How many of the following are legal

declarations? float[] lion = new float[];


float[] tiger = new
float[1]; float[] bear =
new[] float; float[] ohMy
= new[1] float;

Q60. What will be the output of the following Java code?


1 class evaluate
2 {
3 public static void main(String args[])
4 {
5 int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
6 int n = 6;
7 n = arr[arr[n] / 2];
8 System.out.println(arr[n] / 2);
9 }
10 }
11
3

Q61. Which of the following declarations of the array contains the error?

int x[ ]= int[10];

int [ ] y=new int[5];

float d[ ]= {1,2,3};

int a[ ] = {1, 2,3; int b[ ]; b=a;

Q62. What is the output of the following code?


1 class array_output
2 {
3 public static void main(String args[])
4 {
5 char array_variable [] = new char[10];
6 for (int i = 0; i < 10; ++i)
7 {
8 array_variable[i] = 'i';
9 System.out.print(array_variable[i]
+ "");
10 }
11 }
12 }

1 2 3 4 5 6 7 8 9 10

0 1 2 3 4 5 6 7 8 9 10

ijklmnopqr

iiiiiiiiii

Q63. Which of these is necessary to specify at the time of array initialization?

Row

Column

Both Row and Column


None of the mentioned

Q64. What is the index variable for the element at the first row and first column in array a?

a[0][0]

a[1][1]

a[0][1]

a[1][0]

Q65. What will be the output of the following code?


1 class multidimention_array
2 {
3 public static void main(String
args[])
4 {
5 int arr[][] = new int[3][];
6 arr[0] = new int[1];
7 arr[1] = new int[2];
8 arr[2] = new int[3];
9 int sum = 0;
10 for (int i = 0; i < 3; ++i)
11 for (int j = 0; j < i + 1; ++j)
12 arr[i][j] = j + 1;
13 for (int i = 0; i < 3; ++i)
14 for (int j = 0; j < i + 1; ++j)
15 sum += arr[i][j];
16 System.out.print(sum);
17 }
18 }

11

10

13

14

Q66. Examine the following.


What is in values[2][1] ?
1 double[][] values =
2 { {1.2, 9.0, 3.2},
3 {9.2, 0.5, 1.5, -1.2},
4 {7.3, 7.9, 4.8} } ;

7.3

7.9
9.2

There is no such array element

Q67. The in a two-dimensional array specifies the element's row and column position.

Superscript

Subscript

Row number

Column number

Q68. What will be the output of the following Java code?


1 class array_output
2 {
3 public static void main(String args[])
4 {
5 int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}};
6 int sum = 0;
7 for (int i = 0; i < 3; ++i)
8 for (int j = 0; j < 3 ; ++j)
9 sum = sum + array_variable[i][j];
10 System.out.print(sum / 5);
11 }
12 }

10

11

Q69. Which of the following statements is correct?

char[][] charArray = {'a', 'b'};

char[2][2] charArray = {{'a', 'b'}, {'c', 'd'}}

char[2][] charArray = {{'a', 'b'}, {'c', 'd'}};

char[][] charArray = {{'a', 'b'}, {'c', 'd'}};

Q70. The subscripts are than the row and column in which the element is located
One number less

One number more

Two number less

Two number more

Q71. A resembles a table.

One-dimensional array

Two-dimensional array

Three-dimensional array

N-dimensional array

Q72. Assume int[][] x = {{1, 2}, {3, 4}, {5, 6}}, What are x.length are x[0].length?

1 and 2

2 and 3

3 and 2

3 and 3

Q73. Examine the following.


What is in values[3][0] ?
1 double[][] values =
2 { {1.2, 9.0, 3.2},
3 {9.2, 0.5, 1.5, -1.2},
4 {7.3, 7.9, 4.8} } ;

7.3

7.9

9.2

There is no such array element


Q74. Each element in a two-dimensional array is identified by a unique combination of

One subscript

Two subscripts

Three subscripts

Zero subscript

Q75. How many elements are array matrix (int[][] matrix = new int[5][5])?

20

25

30

35

Q76. Predict the output for the below code snippet.


1 StringBuilder sb=new StringBuilder("Hello World");
2 sb.delete(1,3);
3 System.out.println(sb);

Ho World

Hlo World

Hello World

Hello Wd

Q77. Which of these methods of class String is used to extract a single character from a String object?

CHARAT()

charat()

charAt()
ChatAt()

Q78. Predict the output for the below code snippet.


1 String s1 = "abc";
2 String s2 = "def";
3 System.out.println(s1.compareTo(s2));

false

true

-3

Q79. What is the string contained in s after the following lines of code?
1 StringBuffer s new StringBuffer(“Hello”);
2 s.deleteCharAt(0);

Hell

ello

Hel

llo

Q80. What is the value returned by function compareTo() if the invoking string is less than the string compared?

zero

value less than zero

value greater than zero

None of the listed options


Q81. Which of these classes is the superclass of String and StringBuffer class?

java.util

java.lang

ArrayList

None of the listed options

Q82. Predict the output for the below code snippet.


1 StringBuffer s1 = "one";
2 StringBuffer s2 = s1.append("two");

one

two

onetwo

twoone

Q83. Predict the output for the below code snippet.


1 System.out.println("13" + 5 + 3);

21

138

1353

Compile Time Error

Q84. Predict the output for the below code snippet.


1 String s1 = null;
2 System.out.print(s1+" ");
3 System.out.print(s1.toString());
null null

null NullPointerException

NullPointerException NullPointerException

None of the listed options

Q85. Which of these is an incorrect statement?

String objects are immutable, they cannot be changed

String object can point to some other reference of String variable

StringBuffer class is used to store string in a buffer for later use

None of the listed options

Q86. Which of these operators can be used to concatenate two or more String objects?

+=

&

||

Q87. Consider the following code


snippet, string s=new String();

It will create an instance of the string with

at least one character

a default character

no characters in it

number of characters in it
Q88. toString() method is defined in

java.lang.String

java.lang.Object

java.lang.util

None of the listed options

Q89. How many objects will be created for the following code?

1 public class Main{


2 public static void main(String args[])
3 {
4 String a=new String("java");
5 String b=new String("java");
6 String c="java";
7 String d="java";
8 }
9 }

None of the listed options

Q90. Find the output of the following code.


1 class String_demo
2 {
3 public static void main(String
args[])
4 {
5 int ascii[] = { 65, 66, 67, 68};
6 String s = new String(ascii, 1,
3);
7 System.out.println(s);
8 }
9 }

BCD

ABC

ACD

CBA

Q91. Find the output of the following code.

1 public class Main{


2 public static void main(String args[])
3 {
4 String s="a1b2c3";
5 String tokens[]=s.split("\\
d");
6 for(String s1:tokens)
7 {
8 System.out.print(s1);
9 }
10 }
11 }

123

abc

Compile Time Error

None of the listed options

Q92. The string method CompareTo() returns

true

false

an int value

Q93. Which constructor creates an empty string buffer with the specified capacity as length?

StringBuffer()

StringBuffer(String str)

StringBuffer(int capacity)

None of the listed options

Q94. Find the output for the following code.

1 public class Main{


2 public static void main(String args[])
3 {
4 String s1="SITHA";
5 String s2="RAMA";
6 System.out.println(s1.charAt(0)>s2.charAt(0));
7 }
8 }

false

true
0

Q95. Which of these methods of class String is used to obtain the length of a string object?

get()

Sizeof()

lengthof()

length()

Q96. Find the output of the following code.

1 public class Main{


2 public static void main(String args[])
3 {
4 String str="hello";
5 System.out.println(str.indexOf('t'));
6 }
7 }

true

false

-1

Q97. Find the output of the following code.

1 class Main{
2 public static void main(String args[])
3 {
4 String s1="java";
5 String s2="java";
6 System.out.println(s1.equals(s
2));
7 System.out.println(s1==s2);
8 }
9 }

true true

true false

false true
false false

Q98. Find the output of the following code.

1 public class Main{


2 public static void main(String args[])
3 {
4 String s1=new String("OKAY");
5 String s2=new String(s1);
6 System.out.println(s1==s2);
7 }
8 }

true

false

Q99. String s1="kolkata".replace('k','a');

In the above statement, the effect on string kolkata is,

The first occurrence of k is replaced by a.

All characters k are replaced by a.

All characters a are replaced by k.

Displays error message

Q100. Which of the following is not a class of java.util.regex?

Pattern class

matcher class

PatternSyntaxException

Regex class

Q101. What does public int end(int group) return?

offset from the last character of the subsequent group


offset from the first character of the subsequent group

offset from the last character matched

offset from the first character matched

Q102. What is the significance of the Matcher class for a regular expression in Java?

interpretes pattern in the string

Performs match in the string

interpreted both patterns and performs match operations in the string

None of the listed options

Q103. Which capturing group can represent the entire expression?

group *

group 0

group * or group 0

None of the listed options

Q104. Java provides the package for pattern matching with regular expressions.

regex

java.regex

util.regex

java.util.regex

Q105. Which method returns the start index of the previous match?

public int start(int group)

public int end()

public int end(int group)


public int start()

Q106. Which of the following statements about the regex API are true?

Instances of the Pattern class are used to match character


sequences against a given pattern

The package java.util.regex includes an exception called


PatternSyntaxException

Instances of Matcher class are used to represent regular expressions


in the form of String type

None of the listed options

Q107. What does public int start() return?

returns start index of the input string

returns start index of the current match

returns start index of the previous match

None of the listed options

Q108. What does public String replaceAll(string replace) do?

Replace all characters that match pattern with a replacement string

Replace the first subsequence that matches the pattern with a


replacement string

Replace all other than the first subsequence of that matches pattern
with a replacement string

Replace every subsequence of the input sequence that matches


pattern with a replacement string

Q109. The method that performs the search-and-replace operation to strings for pattern matching is

searchandreplace()

add()

edit()
replace()

Q110. Subexpression S is used for?

Matches the word characters

Matches the whitespace

Matches the non-whitespace

Matches the nondigits

Q111. Which class of objects is utilized to compile regular expressions?

Matcher class

Pattern class

String class

None of the listed options

Section 2 - Coding

Section Summary
● No. of Questions: 34
● Duration: 100 min

Additional Instructions:
None

Q1. Problem Statement:

Write a program to print the character value.

Input Format

The input consists of a character value

Output Format

The output displays the character value.

Constraints

The input value must be a character only.

Sample Input Sample Output

d d
Time Limit: - ms Memory Limit: - kb Code Size: - kb

Q2. Problem Statement:

Play with Typecasting

Write a code by declaring three variables where two variables are of integer type and one variable is double.
Multiply the two integer variables and store the result in the remaining variable (double).

Question Instructions:

1. Create a driver class named Main.


2. The solution code should be written inside the main method() of the Main class

Input Format

The input consists of two integer values n1 and n2 separated by a single space.

Output Format

The output displays the result in double as shown in the sample output.

Constraints

The input values n1 and n2 must be integers only.

Sample Input Sample Output

67 801 53667.0

Sample Input Sample Output

45 5 225.0

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q3. Problem Statement:

Write a Java program to get the integer values and print the same integer values.

Input Format

Input consists of a integer value

Output Format

Output consists of a integer value

Sample Input Sample Output

26 26

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q4. Problem Statement:

If the marks of Alice in 3 subjects are mark1, mark2, mark3. Write a program to calculate the total and average.

Input Format

The input consists of the three subject marks separated by space.


Output Format

The output prints the sum and average in separate lines.

Note: The average must be rounded off to two

decimal places. Constraints

The input values must be integers only.

Sample Input Sample Output

96 98 94 Total : 288
Average : 96.00

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q5. Problem Statement:

Write a program to find the square, cube, and square root of the given number.

Input Format

The input consists of an integer.

Output Format

The output displays the square, cube, and square root of the number.

Constraints

The input value must be integer only.

Sample Input Sample Output

5 Square of 5 is: 25.0


Cube of 5 is: 125.0
Square Root of 5 is: 2.23606797749979

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q6. Problem Statement:

Find the total customers


Jeevan is running a sports club. He already had N number of customers and due to the offer that he declared
yesterday a lot of new members have joined the club today. Your task here is to write a program to find the total
number of customers that he has.

Question Instructions:

1. Create a driver class named Main.


2. The solution code should be written inside the main method() of the Main class

Input Format

The input consists of two integers separated by space representing N1 as the customers that he already has and N2 denotes
the customer added today.

Output Format

The output prints the total number of customers that Jeevan has as shown in the sample output.

Constraints

The input values must be non-negative integers only.

Sample Input Sample Output

10 11 21
Sample Input Sample Output

4 5 9

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q7. Problem Statement:

Write a program to print the power of m raised to n.

Input Format

The input consists of the m and n values separated by space.

Output Format

The output prints the result of the power of m raised to m.

Note: Round off the output to two decimal

places. Constraints

The input values must be integers only.

Sample Input Sample Output

2 3 8.00

Sample Input Sample Output

3 4 81.00

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q8. Problem Statement:

Salary Computation

Danny has recently got his job offer as an Event Concept Creator at Sparsh Event Services. The Company has sent
him a detailed salary structure with details of his basic salary, HRA and DA. The Company has promised to pay him
as under:

If his basic salary is less than Rs. 15000, then HRA = 15% of basic salary and DA = 90% of basic salary.
If his basic salary is either equal to or above Rs. 15000, then HRA = Rs. 5000 and DA = 98% of
basic salary. If Danny’s salary is given as input, write a program to find his gross salary.

Note : Gross Salary = Basic Salary + HRA + DA

Input Format

The input is an integer that corresponds to the basic salary of Danny.

Output Format

The output displays the double value that refers to the gross salary of Danny. Display the output correct to 2 decimal places.

Constraints

The input value must be a non-negative integer.

Sample Input Sample Output

12000 24600.00
Sample Input Sample Output

30000 64400.00

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q9. Problem Statement:

Grade Calculation
Write a program to display the grade of a student. If the mark is > 85 then display 'A', if the mark is > 75 then
display 'B', mark is > 65 then display 'C' for others display 'D'.

Note: Use if else statement.

Input Format

The input consists of an integer representing the value of N.

Output Format

The output displays the respective grade for the student based on the mark provided as shown in the sample output. It
should print as "Invalid" if N<0.

Constraints

N (Integer type) >= 0.

Sample Input Sample Output

67 C

Sample Input Sample Output

-50 Invalid

Time Limit: 100 ms Memory Limit: 256 kb Code Size:

1024 kb Q10. Problem Statement:

Given an integer as an input, it represents the temperature in centigrade. Determine the weather conditions
based on the following temperatures.
●Temperature < 0 then print "Freezing weather".
●Temperature 0 - 10 then print "Very cold weather".
●Temperature 10 - 20 then print "Cold weather".
●Temperature 20 - 30 then print "Normal in temperature".
●Temperature 30 - 40 then print "Its hot".
●Temperature >= 40 then print "Its very hot".

Input Format

The input consists of an integer representing the temperature.

Output Format

The output prints the weather conditions based on the conditions mentioned in the problem statement.

Constraints

The input value must be integer only.

Sample Input Sample Output

-2 Freezing weather
Sample Input Sample Output

0 Very cold weather

Sample Input Sample Output

4 Very cold weather

Sample Input Sample Output

10 Cold weather

Sample Input Sample Output

20 Normal in temperature

Sample Input Sample Output

30 Its hot

Sample Input Sample Output

40 Its very hot

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q11. Problem Statement:-

Write a program to generate the first 'n' terms of the following series 1, 4, 9, 16, 25,...

Input Format

The input consists of an integer 'n' which denotes the number of terms to be printed in the series.

Output Format

The output consists of the series.


Refer to the sample output for formatting.

Sample Input Sample Output

5 1 4 9 16 25

Sample Input Sample Output

4 1 4 9 16

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q12. Problem Statement :

Lucas Sequence
a = 0, b=0, c=1 are the 1st three terms. All other terms in the Lucas sequence are generated by the sum of their 3 most
recent predecessors. Write a program to generate the first n terms of a Lucas Sequence.

Input Format

The input contains an integer 'n' which denotes the given number.

Output Format

Print the 'n' terms of the Lucas Sequence, separated by a single space. There are no leading or trailing spaces in the output.

Constraints

The input value must be integer only.

Sample Input Sample Output

5 0 0 1 1 2

Sample Input Sample Output

4 0 0 1 1

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q13. Problem Statement :

SPECIAL NUMBER
Write a program to find all special numbers between the given range m and n (both inclusive). Assume that m and n
are 2-digit numbers.

A 2-digit number is said to be a special number if the sum of its digits and the products of its digits is equal to the

number itself. For example, 19 is a special number.

The digits in 19 are 1 and 9. The sum of the digits is 10 and the product of the
digits is 9. So, the answer is 10+9 = 19.

Input Format

The input consists of 2 integers m and n on separate lines denoting the range.

Output Format

Print the special numbers in separate lines as shown in the sample output.

Constraints

The input values must be integers only.

Sample Input Sample Output

11 19
30 29

Sample Input Sample Output

28 29
60 39
49

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q14. Problem Statement :

Trendy Numbers
Write a program to check whether the given number is a trendy number or not. A number is said to be a trendy
number if and only if it has 3 digits and the middle digit is divisible by 3.

Input Format

The input contains an integer 'n' which denotes the given number.

Output Format

If the given number is trendy, then print "Trendy Number". Otherwise, print "Not a Trendy Number".

Constraints

The input must be an integer only.

Sample Input Sample Output

164 164 is trendy number

Sample Input Sample Output

123 123 is not a trendy number

Sample Input Sample Output

4 4 is not a trendy number

Sample Input Sample Output

2345 2345 is not a trendy number

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q15. Problem Statement:

Write a program to find the largest and smallest number in an array.

Input Format

Input consists of n+1 integer inputs.


The first line of the input describes the array size 'n',
The second line of input consists of the array of elements separated by space.

Output Format

The output displays the smallest and largest number in the array in separate lines.

Constraints

The input values should be integers only.

Sample Input Sample Output

5 smallest value: 2
12 4 2 5 22 largest value: 22

Sample Input Sample Output

6 smallest value: 4
20 30 50 4 71 100 largest value: 100
Time Limit: - ms Memory Limit: - kb Code Size: - kb

Q16. Problem Statement:

Given an array A consists of N number of elements. If the sum of the elements is "even" print the sum of the
element. If the sum of the elements is "odd" print the product of the element.

Input Format

The first line of input contains the number of elements N.


The second line of input represents the space-separated elements A1, A2, A3 AN.

Output Format

The output prints the desired result.

Constraints

The input values must be integers only.

Sample Input Sample Output

5 16
1 2 3 4 6

Sample Input Sample Output

4 530400
10 20 52 51

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q17. Problem Statement:

Given an array of elements, write a program to find two elements in the array such that their sum is equal to the
given element K.

Input Format

The first line of the input consists of the value


of n. The next line of input consists of array of
elements. The last line of input consists of the
sum.

Output Format

The output prints whether the array has a pair of elements with the given sum as shown in the sample output.

Constraints

The input must be integers only.

Sample Input Sample Output

6 Array has two elements with given sum 16


1 4 45 6 10 -8
16

Sample Input Sample Output

6 Array doesn't have two elements with given sum 60


1 4 45 6 10 -8
60

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q18. Problem Statement:

Weighing machines in Sunrise Logistics are not working. Raju, the manager of the division wants to calculate the total
weight of received goods. Weight is printed on the goods label. Write a suitable code to help Raju.
Input Format

The first line of input consists of the number of received goods.


The second line of input consists of the weight of goods separated by space.

Output Format

The output prints the total weight.

Constraints

The input values must be integers only.

Sample Input Sample Output

10 54
1 9 2 8 3 7 4 6 8 6

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q19. Problem Statement:


Write a program to obtain a matrix and find the sum of its diagonal elements.

Note: Only square matrix.

Input Format

The input consists of the number of rows and columns separated by a


space. The next n lines of input is the matrix.

Output Format

The output prints the sum of diagonal elements.

Constraints

The input values must be integers only.

Sample Input Sample Output

3 3 15
1 2 3
4 5 6

Sample Input Sample Output

4 4 191
12 23 45 56
78 89 98 87

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q20. Problem Statement:


Write a program to implement matrix multiplication.

Note: Only Square matrices.

Input Format

The first line of the input is the value of n - order of


matrices. The next line of input is the array elements.

Output Format

The output prints the product of two matrices as shown in the sample output.

Constraints

The input values must be integers only.


Sample Input Sample Output

3 33 28 35
1 2 3 75 70 83
4 5 6 47 42 51

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q21. Problem Statement:

Snake Pattern
Write a program to print the matrix in a zig-zag form.

Input Format

Input to get the integer R and C (i.e., rows and columns) in the first line separated by a single space, then get the values for
the R*C matrix in the following lines.

Output Format

The output displays the matrix in a zig-zag pattern.

Constraints

The input values must be integers only.

Sample Input Sample Output

4 3 1 2 3 6 5 4 7 8 9 12 11 10
1 2 3
4 5 6

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q22. Problem Statement:

Write a program to obtain a matrix and find the sum of each row and each column.

Input Format

The first line of the input consists of the value of the number of rows and the number of columns
separated by space. The next lines of input is the matrix.

Output Format

The output prints the sum of each row and each column in separate lines as shown in the sample output.

Constraints

The input values must be integers only.

Sample Input Sample Output

4 4 Sum of the row 0 =


1 2 3 4 10 Sum of the row 1
5 6 7 8 = 26 Sum of the row
2 = 42
Sample Input Sample Output

3 3 Sum of the row 0 =


98 87 65 250 Sum of the row 1
54 32 21 = 107 Sum of the row
2 = 159
Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q23. Problem Statement:


Write a program to count the vowels in the string.

Input Format
The input consists of a string S.

Output Format

The output displays the number of vowels present in the given string.

Constraints

S>0

Sample Input Sample Output

string 1

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q24. Problem Statement:


Write a program to check whether the string is lexicographically equal to another string.

Input Format

Input to get two strings in separate lines.

Output Format

Display the output as shown in the sample output.


Note :
●if (string1 > string2) it returns a positive value(difference between the characters)
●if both the strings are equal lexicographically i.e.(string1 == string2) it returns 0.
●if (string1 < string2) it returns a negative value((difference between the characters)

Sample Input Sample Output

harry 0
harry

Sample Input Sample Output

john -3
mini

Sample Input Sample Output

tiger 8
lion

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q25. Problem Statement:


Write a program to remove all the spaces in the given sentence.

Input Format

Input to get a sentence from the user.

Output Format

The output displays the sentence after removing the spaces.

Sample Input Sample Output

kite in the cloud kiteinthecloud

Sample Input Sample Output


networking networking

Time Limit: 100 ms Memory Limit: 256 kb Code Size:

1024 kb Q26. Problem Statement:


Write a program to check whether the given string is a palindrome.

Input Format

Input to get a string S.

Output Format

Output prints whether the string is palindrome or not.

Refer to the sample output for formatting specifications.

Sample Input Sample Output

mom mom :palindrome

Sample Input Sample Output

norms norms :not a palindrome

Sample Input Sample Output

butitwaslateonthatday butitwaslateonthatday :not a palindrome

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q27. Problem Statement:


Write a program to convert a string to an int.

Note: If the string contains a character then print 0.


Eg. a1b2 here the input contains character, so conversion is not possible.

Input Format

The input is a number as a string.

Output Format

The output displays the integer converted from a string.

Sample Input Sample Output

234 234

Sample Input Sample Output

a12 0

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q28. Problem Statement:


Alternating Code
It is IPL Season and the first league match of Dhilip’s favorite team, "Chennai Super Kings". The CSK team is
playing at the IPL after 2 years and like all Dhoni lovers, Dhilip is also eagerly awaiting to see Dhoni back in action.
After waiting in long queues, Dhilip succeeded in getting the tickets for the big match. On the ticket, there is a
letter code that can be represented as a string of upper-case Latin letters.
Dhilip believes that the CSK Team will win the match in case exactly two different letters in the code alternate.
Otherwise, he believes that the team might lose. Please see the note section for the formal definition of alternating
code.

You are given a ticket code. Please determine, whether CSK Team will win the match or not based on
Dhilip’sconviction. Print "YES" or "NO" (without quotes) corresponding to the situation.

Note:
Two letters x, y where x != y are said to be alternating in a code, if the code is of form "xyxyxy...".

Input Format

The first and only line of the input contains a string S denoting the letter code on the ticket.

Output Format

Output a single line containing "Yes" (without quotes) based on the conditions given and "No" otherwise.

Sample Input Sample Output

ABABAB Yes

Sample Input Sample Output

ABC No

Sample Input Sample Output

XYXYX Yes

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q29. Problem Statement:

Adjacent characters
Given a string, write a program to compute a new string where identical chars that are adjacent in the original string
are separated from each other by a "*".

Input Format

The input consists of a string.

Output Format

The output prints the newly formed string.

Sample Input Sample Output

hello hel*lo

Sample Input Sample Output

aaabbb a*a*ab*b*b

Time Limit: - ms Memory Limit: - kb Code Size: - kb


Q30. Problem Statement:

Mobile number validation

Let's implement the logic for mobile number validation using StringBuilder and embed it in our program. Mobile
number should precede with "+91", followed by 10 digits. The indexOf() method returns an index of a given
character value or substring. If it is not found, it returns -1. Write a program to validate the mobile number given as
input. Use indexOf() to check whether "+91" is present or not.

Create a driver class called Main. In the Main method, obtain the inputs from the console, validate the mobile
number and prompt the user as given in sample I/O.

Input Format

Input consists of a mobile number.

Output Format

Output prints whether the mobile number is valid or not.

Sample Input Sample Output

+919874653210 Mobile number valid

Sample Input Sample Output

9874653210 Mobile number invalid

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q31. Problem statement:


Write a program to check for a number at the end of a given string.

Input Format

The input consists of the string.

Output Format

The output prints the input text and results with whether the match is found or not found.

Constraints

The input must be a string only.

Sample Input Sample Output

abcdef abcdef
Not matched!

Sample Input Sample Output

abcdef3459 abcdef3459
Found a
match!

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q32. Problem statement:


Write a program finding data type of user input using Regular Expression.

Input Format

The input consists of different inputs of integer, string, double, and date with different formats.
Date formats :
1. dd/mm/yyyy : eg: 01/12/2022
2. mm/dd/yyyy : eg: 12/24/2022
3. dd-mon-yy : eg: 01-apr-22
4. dd-mon-yyyy : eg: 01-apr-2022
5. dd-month-yy: eg:01-april-22
6. dd-month-yyyy: eg: 01-april-2022

Output Format

The output prints the value with the appropriate datatype.

Refer to the sample output for the formatting specifications.

Sample Input Sample Output

100 The datatype of 100 is: java.lang.Integer

Sample Input Sample Output

52.87 The datatype of 52.87 is: java.lang.Double

Sample Input Sample Output

21-apr-1994 The datatype of 21-apr-1994 is: java.util.Date

Sample Input Sample Output

Born to win The datatype of Born to win is: java.lang.String

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q33. Problem statement :


Write a program to check whether a string contains only a certain set of characters (in this case a-z, A-Z, and 0-9)

Input Format

The input consists of a string.

Output Format

The output prints the input and returns the match whether it is true or false.

Sample Input Sample Output

ABCDEFabcdef123456 ABCDEFabcdef123456
true

Sample Input Sample Output

W3.com W3.com
false

Time Limit: - ms Memory Limit: - kb Code Size:

- kb Q34. Problem statement:


Write a regular expression to represent all valid identifiers in Java language.

Rules:
The allowed characters are:
1. a to z, A to Z, 0 to 9, -,#
2. The 1st character should be an alphabet symbol only.
3. The length of the identifier should be at least 2.

Input Format

The input consists of a string.

Output Format

The output prints if the given string is Valid Identifier or not.

Refer to the sample output for formatting specifications.

Sample Input Sample Output

ashok ashok:Valid Identifier

Sample Input Sample Output

?ashok ?ashok:Invalid Identifier

Time Limit: - ms Memory Limit: - kb Code Size: - kb


Answer Key & Solution
Section 1 -
MCQ
Q1
java.io.*

Solution

No Solution

Q2
BufferedInputStream

Solution

No Solution

Q3
All of the listed options

Solution

No Solution

Q4
Signed

Solution

No Solution

Q5
It can be run on any computer with a compatible JVM.

Solution

No Solution

Q6
I and II

Solution

No Solution

Q7
void

Solution

No Solution

Q8
Streams

Solution

No Solution
Q9
java.lang package

Solution

No Solution

Q10
volatile

Solution

No Solution

Q11
java.lang

Solution

No Solution

Q12
It prevents Java bytecode from being easily decoded/decompiled.

Solution

No Solution

Q13
.class

Solution

No Solution

Q14
True

Fals

True

Fals

Solution

No Solution

Q15
1, 2, 3 & 4

Solution

Operator ++ increases the value of the variable by 1. x = x + 1 can also be written in shorthand form as x += 1. Also, x =+

1 will set the value of x to 1.


Q16
1
Solution

No Solution

Q17
1

Solution

No Solution

Q18
12

Solution

No Solution

Q19

x+=10;

Solution

No Solution

Q20

The ^ operator can be applied to int type.

Solution

No Solution

Q21
1

Solution

No Solution

Q22

Compilation fails

Solution

No Solution

Q23

Car wins!

Solution

No Solution
Q24
14

Solution
No Solution

Q25

Both Numeric & Characters

Solution

No Solution

Q26

Both Integers and floating–point numbers.

Solution

No Solution

Q27

The code does not compile due to p1.

Solution

No Solution

Q28

None of the above

Solution

No Solution

Q29

The code doesn't compile.

Solution

No Solution

Q30
2

Solution

No Solution

Q31

A case statement must be terminated with a break statement.

Solution

No Solution
Q32

long

Solution

No Solution
Q33
The code doesn't compile.

Solution

No Solution

Q34

switch

Solution

No Solution

Q35

any number of, at most one

Solution

No Solution

Q36
15

Solution

No Solution

Q37

The code doesn't compile.

Solution

No Solution

Q38
2

Solution

No Solution

Q39

if()

Solution

No Solution

Q40

1 0, 2 0, 2 1,

Solution
No Solution

Q41
Three
Solution

No Solution

Q42

int k=0; do { } while(k++ < 7)

Solution

No Solution

Q43

Compile Time Error

Solution

No Solution

Q44
10

Solution

No Solution

Q45

Two

Solution

No Solution

Q46
0

Solution

No Solution

Q47

for-each

Solution

No Solution

Q48

Code will print abc

Solution
No Solution

Q49
2

Solution
No Solution

Q50

continue

Solution

No Solution

Q51

None of the listed options

Solution

No Solution

Q52

It is necessary to use new operator to initialize an array

Solution

No Solution

Q53
0

Solution

No Solution

Q54

Methods and Fields

Solution

No Solution

Q55

Object

Solution

No Solution

Q56

10 20 30 40 50

Solution
No Solution

Q57
Class name@ hashcode in hexadecimal form
Solution

No Solution

Q58

int arr[] = int[5] new

Solution

No Solution

Q59
1

Solution

No Solution

Q60
1

Solution

No Solution

Q61

int x[ ]= int[10];

Solution

No Solution

Q62

iiiiiiiiii

Solution

No Solution

Q63

Row

Solution

No Solution

Q64

a[0][0]

Solution
No Solution

Q65
10

Solution
No Solution

Q66

7.9

Solution

No Solution

Q67

Subscript

Solution

No Solution

Q68
9

Solution

No Solution

Q69

char[][] charArray = {{'a', 'b'}, {'c', 'd'}};

Solution

No Solution

Q70

One number less

Solution

No Solution

Q71

Two-dimensional array

Solution

No Solution

Q72

3 and 2

Solution

No Solution
Q73

There is no such array element

Solution

No Solution
Q74
Two subscripts

Solution

No Solution

Q75
25

Solution

No Solution

Q76

Hlo World

Solution

No Solution

Q77

charAt()

Solution

No Solution

Q78
-3

Solution

No Solution

Q79

ello

Solution

No Solution

Q80

value less than zero

Solution

No Solution

Q81

java.lang

Solution
No Solution

Q82
onetwo
Solution

No Solution

Q83

1353

Solution

No Solution

Q84

null NullPointerException

Solution

No Solution

Q85

StringBuffer class is used to store string in a buffer for later use

Solution

No Solution

Q86
+

Solution

No Solution

Q87

no characters in it

Solution

No Solution

Q88

java.lang.Object

Solution

No Solution

Q89
3

Solution
No Solution

Q90

BCD

Solution
No Solution

Q91

abc

Solution

No Solution

Q92

an int value

Solution

No Solution

Q93

StringBuffer(int capacity)

Solution

No Solution

Q94

true

Solution

No Solution

Q95

length()

Solution

No Solution

Q96
-1

Solution

No Solution

Q97

true true

Solution

No Solution
Q98

false

Solution

No Solution
Q99
All characters k are replaced by a.

Solution

No Solution

Q10
0

Regex class

Solution

No Solution

Q10
1

offset from the last character of the subsequent group

Solution

No Solution

Q10
2

interpreted both patterns and performs match operations in the string

Solution

No Solution

Q10
3

group 0

Solution

No Solution

Q10
4

java.util.regex

Solution

No Solution

Q10
5

public int start()

Solution

No Solution

Q10
6
The package java.util.regex includes an exception called PatternSyntaxException

Solution

No Solution

Q107
returns start index of the previous match
Solution

No Solution

Q10
8

Replace every subsequence of the input sequence that matches pattern with a replacement string

Solution

No Solution

Q10
9

replace()

Solution

No Solution

Q11
0

Matches the non-whitespace

Solution

No Solution

Q11
1

Pattern class

Solution

No Solution

Section 2 - Coding
Q1
Test Case

Input Output

e e

Weightage - 25

Input Output

a a

Weightage - 10

Input Output
g g
Weightage - 25

Input Output

z z

Weightage - 40

Sample Input Sample Output

d d

Solution

import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner s=new
Scanner(System.in); char
a=s.next().charAt(0);
System.out.print(a);
}
}

Q2
Test Case

Input Output

30 111 3330.0

Weightage - 30

Input Output

-2 -3 6.0

Weightage - 5

Input Output

22 55 1210.0

Weightage - 5

Input Output
90 1800.0
20

Weightage - 10

Input Output

29 232.0
8

Weightage - 5

Input Output

709 -67 -47503.0

Weightage - 30

Input Output

222 222 49284.0

Weightage - 10

Input Output

-89 56 -4984.0

Weightage - 5

Sample Input Sample Output

67 801 53667.0

Sample Input Sample Output

45 5 225.0

Solution

import java.util.Scanner;
class Main{
public static void main(String args[])
{ int x,y;
double z;
Scanner sc=new Scanner(System.in);
x=sc.nextInt();
y=sc.nextInt();
z=x*y;
System.out.println(z);
}
}

Q3
Test Case

Input Output

15 15

Weightage - 100

Sample Input Sample Output

26 26

Solution

import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner s=new
Scanner(System.in); int
n=s.nextInt();
System.out.print(n);
}
}

Q4
Test Case

Input Output

84 85 81 Total : 250
Average : 83.00

Weightage - 10

Input Output

75 77 79 Total : 231
Average : 77.00

Weightage - 25

Input Output

62 63 65 Total : 190
Average : 63.00
Weightage - 25

Input Output

91 96 98 Total : 285
Average : 95.00

Weightage - 40

Sample Input Sample Output

96 98 94 Total : 288
Average : 96.00

Solution

import java.util.Scanner;
import java.text.*;
class Main {
public static void main(String []
args) { int mark1, mark2, mark3;
Scanner sc = new Scanner(System.in);
mark1 = sc.nextInt();
mark2 = sc.nextInt();
mark3 = sc.nextInt();
DecimalFormat d = new DecimalFormat("0.00");
System.out.println("Total : "+(mark1+mark2+mark3)+"\nAverage : "+d.format(((mark1+mark2+mark3)/3)));
}
}

Q5
Test Case

Input Output

6 Square of 6 is: 36.0


Cube of 6 is: 216.0
Square Root of 6 is: 2.449489742783178

Weightage - 10

Input Output

3 Square of 3 is: 9.0


Cube of 3 is: 27.0
Square Root of 3 is: 1.7320508075688772

Weightage - 10

Input Output

8 Square of 8 is: 64.0


Cube of 8 is: 512.0
Square Root of 8 is: 2.8284271247461903

Weightage - 20
Input Output

12 Square of 12 is: 144.0


Cube of 12 is: 1728.0
Square Root of 12 is: 3.4641016151377544

Weightage - 20

Input Output

10 Square of 10 is: 100.0


Cube of 10 is: 1000.0
Square Root of 10 is: 3.1622776601683795

Weightage - 40

Sample Input Sample Output

5 Square of 5 is: 25.0


Cube of 5 is: 125.0
Square Root of 5 is: 2.23606797749979

Solution

import java.util.*;
import java.util.Scanner;
class j5
{
public static void main(String
args[]){ Scanner sc=new
Scanner(System.in); int num;
num=sc.nextInt();
System.out.println("Square of "+ num + " is: "+ Math.pow(num, 2));
System.out.println("Cube of "+ num + " is: "+ Math.pow(num, 3));
System.out.println("Square Root of "+ num + " is: "+ Math.sqrt(num));
}
}

Q6
Test Case

Input Output

23 24 47

Weightage - 15

Input Output

2 3 5

Weightage - 5

Input Output
6 15
9

Weightage - 5

Input Output

4 12
8

Weightage - 5

Input Output

11 33
22

Weightage - 5

Input Output

20 50
30

Weightage - 15

Input Output

23 66
43

Weightage - 25

Input Output

40 90
50

Weightage - 25

Sample Input Sample Output

10 11 21

Sample Input Sample Output

4 5 9

Solution
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
int a,b,c;
Scanner sc=new Scanner(System.in);
a=sc.nextInt();
b=sc.nextInt();
c=a+b;
System.out.print(c);
}
}

Q7
Test Case

Input Output

1 1.00
2

Weightage - 10

Input Output

3 9.00
2

Weightage - 10

Input Output

2 32.00
5

Weightage - 20

Input Output

2 256.00
8

Weightage - 20

Input Output

3 729.00
6

Weightage - 40

Sample Input Sample Output


2 3 8.00

Sample Input Sample Output

3 4 81.00

Solution

import java.util.Scanner;
import java.lang.Math;
import java.text.*;
class Main {
public static void main(String []
args) { int base, power;
Scanner sc = new Scanner(System.in);
DecimalFormat d = new
DecimalFormat("0.00"); base =
sc.nextInt();
power = sc.nextInt();
System.out.println(d.format(Math.pow(base,power)));
}
}

Q8
Test Case

Input Output

8000 16400.00

Weightage - 5

Input Output

10000 20500.00

Weightage - 5

Input Output

7000 14350.00

Weightage - 5

Input Output

5000 10250.00
Weightage - 5

Input Output

15000 34700.00

Weightage - 15

Input Output

20000 44600.00

Weightage - 15

Input Output

45000 94100.00

Weightage - 25

Input Output

40000 84200.00

Weightage - 25

Sample Input Sample Output

12000 24600.00

Sample Input Sample Output

30000 64400.00

Solution

import java.io.*;
import java.util.*;
import java.text.*;
class Salarycomputation {
public static void main(String []
args) { int salary;
double amount,hra,da;
DecimalFormat d = new
DecimalFormat("0.00"); Scanner sc = new
Scanner(System.in);
salary = sc.nextInt();
if(salary < 15000) {
hra =
0.15*salary; da =
0.90*salary;
amount = salary+hra+da;
}
else {
da = 0.98*salary;
amount = salary+5000+da;
}
System.out.println(d.format(amount));
}
}

Q9
Test Case

Input Output

90 A

Weightage - 20

Input Output

79 B

Weightage - 20

Input Output

-44 Invalid

Weightage - 10

Input Output

71 C

Weightage - 10

Input Output

-1 Invalid

Weightage - 30

Input Output

80 B
Weightage - 10

Sample Input Sample Output

67 C

Sample Input Sample Output

-50 Invalid

Solution

import java.util.Scanner;
class Main{
public static void main(String args[])
{ int mark;
Scanner in =new Scanner(System.in);
mark=in.nextInt();
if(mark>=85){
System.out.print("A");
}
else if(mark>=75){
System.out.print("B");
}
else if(mark>=65){
System.out.print("C");
}
else if(mark>=0){
System.out.print("D");
}
else{
System.out.print("Invalid");
}
}
}

Q10
Test Case

Input Output

-4 Freezing weather

Weightage - 10

Input Output

6 Very cold weather

Weightage - 10
Input Output

44 Its very hot

Weightage - 15

Input Output

16 Cold weather

Weightage - 25

Input Output

28 Normal in temperature

Weightage - 25

Input Output

33 Its hot

Weightage - 15

Sample Input Sample Output

-2 Freezing weather

Sample Input Sample Output

0 Very cold weather

Sample Input Sample Output

4 Very cold weather

Sample Input Sample Output

10 Cold weather

Sample Input Sample Output

20 Normal in temperature
Sample Input Sample Output

30 Its hot

Sample Input Sample Output

40 Its very hot

Solution

import java.io.*;
import java.util.*;
class Main {
public static void main(String []
args) { int temp;
Scanner sc = new
Scanner(System.in); temp =
sc.nextInt();
if(temp<0)
System.out.println("Freezing
weather"); else if(temp>=0 && temp<10)
System.out.println("Very cold
weather"); else if(temp>=10 && temp<20)
System.out.println("Cold
weather"); else if(temp>=20 &&
temp<30)
System.out.println("Normal in
temperature"); else if(temp>=30 && temp<40)
System.out.println("Its hot");
else if(temp>=40)
System.out.println("Its very hot");
}
}

Q11
Test Case

Input Output

10 1 4 9 16 25 36 49 64 81 100

Weightage - 20

Input Output

15 1 4 9 16 25 36 49 64 81 100 121 144 169 196


225

Weightage - 20

Input Output

20 1 4 9 16 25 36 49 64 81 100 121 144 169 196


225
Weightage - 20

Input Output

8 1 4 9 16 25 36 49 64

Weightage - 20

Input Output

7 1 4 9 16 25 36 49

Weightage - 20

Sample Input Sample Output

5 1 4 9 16 25

Sample Input Sample Output

4 1 4 9 16

Solution

#include<stdio.h> #include<iostream> n = int(input())


using namespace std; for i in
int main() int main() range(1,n+1):
{ { print(i*i,end='
int a ; int a ; ')
scanf("%d", &a) ; cin>>a ;
for(int i=1 ; i<=a ; i+ for(int i=1;i<=a;i+
+) printf("%d +) cout<<i*i<<"
",i*i) ; " ;
return 0 ; return 0 ;
} }

import java.util.* ;

class Main
{
public static void main(String[] args)
{
Scanner sc = new
Scanner(System.in) ; int a =
sc.nextInt() ;
for(int i=1;i<=a;i++)
System.out.print(i*i+" ") ;
}
}
Q12
Test
Case

Output
Input

10 0 0 1 1 2 4 7 13 24 44

Weighta - 25
ge

Output
Input

12 0 0 1 1 2 4 7 13 24 44 81 149

Weighta - 30
ge

Output
Input

4 0 0 1 1

Weighta - 25
ge

Output
Input

8 0 0 1 1 2 4 7 13

Weighta - 10
ge

Output
Input

6 0 0 1 1 2 4

Weighta - 10
ge
Sample Input Sample Output

5 0 0 1 1 2

Sample Input Sample Output

4 0 0 1 1

Solution

import
java.util.Scanner; class
Main
{
public static void main(String[] args)
{
int num;
Scanner s= new Scanner(System.in);
num=s.nextInt();
Main l= new Main();
l.lucas(num);
}
private void lucas(int num)
{
int thirdLast =
0; int secondLast
= 0; int last =
1;
int current = 0;
System.out.print(thirdLast + " " + secondLast + " " + last + "
"); for (int i = 3; i < num; i++)
{
current = last + secondLast + thirdLast;
System.out.print(current + " ");
int tmp =
last; last =
current;
thirdLast = secondLast;
secondLast = tmp;
}
}
}

Q13
Test Case

Input Output

20 29
60 39
49

Weightage - 15

Input Output

30 39
50 49

Weightage - 15

Input Output

60 69
120 79
89

Weightage - 30

Input Output

100 109
180 119
129
Weightage - 40

Sample Input Sample Output

11 19
30 29

Sample Input Sample Output

28 29
60 39
49

Solution

import java.util.Scanner;
class Main
{
public static void main(String args[])
{
int n,m,sum=0,i,pro=1,sum1,j;
Scanner io=new Scanner(System.in);
m=io.nextInt();
n=io.nextInt();
for(i=m;i<=n;i++)
{
j=i;
int
first=i/10;
int last=i%10;
sum=first+last
;
pro=first*last
;
sum1=sum+pro;
if(sum1==j)
System.out.println(sum1);
}
}
}

Q14
Test
Case

Output
Input

467 467 is trendy number

Weighta - 25
ge

Output
Input

23 23 is not a trendy number

Weightage - 10
Inp
ut

Out
put
768 768 is trendy number

Weightage - 40

Input Output

333 333 is trendy number

Weightage - 25

Sample Input Sample Output

164 164 is trendy number

Sample Input Sample Output

123 123 is not a trendy number

Sample Input Sample Output

4 4 is not a trendy number

Sample Input Sample Output

2345 2345 is not a trendy number

Solution

import java.util.Scanner;
class Main
{
public static void main(String args[])
{
int a,b,count=0;
Scanner s = new Scanner(System.in);
a=s.nextInt();
int c=a;
System.out.print(a+" is ");
while (a != 0)
{
a /= 10;
++count;
}
if(count!=3)
{
System.out.println("not a trendy number");
}
else
{
b = (c / 10) % 10
; if(b%3==0)
{
System.out.println("trendy number");
}
els
e
{
System.out.println("not a trendy number");;

}
}

}
}

Q15
Test Case

Input Output

10 smallest value: 9
23 34 13 64 72 90 10 15 9 27 largest value: 90

Weightage - 30

Input Output

6 smallest value: 4
20 30 50 4 71 100 largest value: 100

Weightage - 15

Input Output

16 smallest value: 1
20 30 50 4 71 100 23 34 13 64 72 90 largest value: 100

Weightage - 20

Input Output

4 smallest value: 7
7 8 9 40 largest value: 40

Weightage - 15

Input Output

9 smallest value: 12
98 87 65 74 25 254 45 12 41 largest value: 254

Weightage - 20

Sample Input Sample Output


5 smallest value: 2
12 4 2 5 22 largest value: 22

Sample Input Sample Output

6 smallest value: 4
20 30 50 4 71 100 largest value: 100

Solution

import java.util.Scanner;
class Large_Small{
public static void main (String args[])
{
Scanner scan=new
Scanner(System.in); int min,max;
int n=scan.nextInt();//get input from user for array
length int arr[]=new int[n]; //declaring an array of n
elements
//for loop takes input from
user for(int i=0; i<n; i++){
arr[i]=scan.nextInt();//takes input from user for array
}
min=arr[0];//assume first element as smallest
value max=arr[0];//assume first element as
largest value for(int i=0; i<n; i++){
if(min>arr[i]){//loop for find minimum
elements min=arr[i];
}

if(max<arr[i]){
max=arr[i]; //loop for find maximum elements
}
}
System.out.print("smallest value: "+min);
System.out.print("\nlargest value: "+max);
}
}

Q16
Test Case

Input Output

3 100
44 52 4

Weightage - 10

Input Output

7 1095791004
77 99 11 33 22 3 6

Weightage - 10

Input Output
10 482
12 34 56 78 90 20 34 52 40 66

Weightage - 25

Input Output

12 266338304
44 32 101 122 432 566 766 844 942 10 12 98

Weightage - 25

Input Output

30 5150
12 34 55 7 8 9 0 3 2 9 56 43 78 23 91 39
321

Weightage - 30

Sample Input Sample Output

5 16
1 2 3 4 6

Sample Input Sample Output

4 530400
10 20 52 51

Solution

import java.util.Scanner;

class Main {
public static void main(String args[])
{ int n;
Scanner in = new
Scanner(System.in); n =
in.nextInt();
int i;
int[] array = new
int[n]; for (i = 0; i <
n; i++)
array[i] = in.nextInt();
int sum = 0, mul = 1;
for (int num : array) {
sum = sum + num;
mul = mul * num;
}
i = (su
sum % 2 == 0) ? sum : mul;
System.out.println(i);
}
}

Q17
Test Case
Input Output

8 Array has two elements with given sum 94


12 35 74 96 20 2 6 8
94

Weightage - 20

Input Output

8 Array doesn't have two elements with given


12 35 74 96 20 2 6 8 sum 99
99

Weightage - 25

Input Output

10 Array has two elements with given sum 81


12 23 45 56 78 89 14 25 36 85
81

Weightage - 25

Input Output

10 Array doesn't have two elements with given


12 23 45 56 78 89 14 25 36 85 sum 12
122

Weightage - 30

Sample Input Sample Output

6 Array has two elements with given sum 16


1 4 45 6 10 -8
16

Sample Input Sample Output

6 Array doesn't have two elements with given


1 4 45 6 10 -8 sum 60
60

Solution

import java.util.*;
class Test {
static boolean
hasArrayTwoCandidates( int
A[],
int arr_size, int sum)
{
int l, r;
Arrays.sort(A)
; l = 0;
r = arr_size - 1;
while (l < r) {
if (A[l] + A[r] == sum)
return true;
else if (A[l] + A[r] < sum)
l++;
else
r--;
r--;
}
return false;
}
public static void main(String args[])
{
int i,n,sum;
Scanner sc = new
Scanner(System.in); n =
sc.nextInt();
int [] arr = new
int[n]; for(i=0;i<n;i+
+) {
arr[i] = sc.nextInt();
}
sum = sc.nextInt();
if (hasArrayTwoCandidates(arr, n, sum))
System.out.println("Array has two "
+ "elements with given sum "+sum);
else
System.out.println("Array doesn't have "
+ "two elements with given sum "+sum);
}
}

Q18
Test Case

Input Output

10 30973
1222 1339 476 1215 6024 7188 1962 558 1970
9019

Weightage - 10

Input Output

25 140365
8323 2552 121 4006 1439 841 4714 5932 9347 7975
9

Weightag 10
e -

Input Output

60 28744
997 290 242 766 59 467 418 522 793 424 985 784 4

Weightage - 15

Input Output

41 201124
5713 8117 8687 3137 9417 3123 4279 2944 5757
5186

Weightage - 15
Input Output

200 972712
1407 2551 9381 7624 9985 7805 6038 2585 1390
4909

Weightage - 25

Input Output

200 755388
-1407 2551 -9381 7624 9985 7805 6038 2585
1390 49

Weightage - 25

Sample Input Sample Output

10 54
1 9 2 8 3 7 4 6 8 6

Solution

import java.util.Scanner;
class SumDemo{
public static void main(String args[])
{ int count;
int sum;
sum=0;
Scanner scan = new Scanner(System.in);
count = scan.nextInt();
int array[] = new int[count];
for (int i = 0; i < count; i++)
{
array[i] = scan.nextInt();
}
scan.close();
for( int num : array) {
sum = sum+num;
}
System.out.println(sum);
}
}

Q19
Test Case

Input Output

3 3 15
1 2 3
4 5 6

Weightage - 10

Input Output

4 4 191
12 23 45 56
78 89 98
87

Weightage -
25

Input Output

2 2 126
74 85
96 52

Weightage - 25

Input Output

1 1 25
25

Weightage - 40

Sample Input Sample Output

3 3 15
1 2 3
4 5 6

Sample Input Sample Output

4 4 191
12 23 56
45
78 89 87
98

Solution

import java.util.Scanner;
class disgonalSum
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

int
i,j,row,col,sum=0;
row = sc.nextInt();
col = sc.nextInt();

int[][] mat = new int[row]


[col]; for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
mat[i][j] = sc.nextInt();
}
}

for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==j)
{
sum = sum + mat[i][j];
}
}
}

System.out.println(sum) ;
}
}

Q20
Test Case

Input Output

3 3900 3140 2840


10 20 30 7887 6892 6802
42 51 53 13893 12010 11710

Weightage - 10

Input Output

4 3489 6745 7085 8873


12 23 45 56 2822 5682 6196 7490
14 25 36 35 5539 11776 13456 15784

Weightage - 25

Input Output

3 5424 8112 9267


12 45 78 7250 10345 11621
23 56 89 4469 5053 5141

Weightage - 25

Input Output

4 3740 4840 5940 7040


10 20 30 40 9020 11880 14740 17600
50 60 70 80 3740 4840 5940 7040

Weightage - 40

Sample Input Sample Output

3 33 28 35
1 2 3 75 70 83
4 5 6 47 42 51

Solution

import java.io.*;
import java.util.*;
class Main {
public static void main(String []
args) { int n,i,j,k;
Scanner sc = new
Scanner(System.in); n =
sc.nextInt();
int mat1[][] = new int[n][n];
int mat2[][] = new int[n][n];
int m[][] = new int[n][n];
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
mat1[i][j] = sc.nextInt();
}
}
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
mat2[i][j] = sc.nextInt();
}
}
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
m[i][j] =0;
for(k=0;k<n;k++) {
m[i][j] += mat1[i][k]*mat2[k][j];
}
}
}
for(i=0;i<n;i++) {
for(k=0;k<n;k++) {
System.out.print(m[i][k]+" ");
}
System.out.println();
}
}
}

Q21
Test Case

Input Output

5 3 3 4 5 9 8 7 23 45 67 0 99 88 12 15 16
3 4 5
7 8 9

Weightage - 30

Input Output

2 2 34 37 78 45
34 37
45 78

Weightage - 15

Input Output

3 2 5 6 87 89 45 67
5 6
89 87

Weightage - 15

Input Output

4 4 12 34 56 78 90 54 76 98 22 33 44 55 99 88
12 34 56 78 77 66
98 76 54 90
Weightage - 20

Input Output

2 3 678 908 234 778 245 102


678 908 234
102 245 778

Weightage - 20

Sample Input Sample Output

4 3 1 2 3 6 5 4 7 8 9 12 11 10
1 2 3
4 5 6

Solution

import java.util.Scanner;
class Main
{
static void printZigZag(int row, int col, int a[][])
{
int evenRow = 0;
int oddRow = 1;

while (evenRow < row)


{
for (int i = 0; i < col; i++)
{
System.out.print(a[evenRow][i] + " ");
}
evenRow = evenRow + 2;

if(oddRow < row)


{
for (int i = col - 1; i >= 0; i--)
i--)
{
System.out.print(a[oddRow][i] + " ");
}
}
oddRow = oddRow + 2;
}
}

public static void main(String[] args)


{

int r, c;
int i,j;
Scanner in=new Scanner(System.in);
r=in.nextInt();
c=in.nextInt();
int mat[][]=new int[10][10];
for(i=0;i<r;i++){
for(j=0;j<c;j++){
mat[i][j]=in.nextInt();
}
}
printZigZag(r , c , mat);
}
}

Q22
Test Case

Input Output

4 4 Sum of the row 0 = 10


1 2 3 4 Sum of the row 1 = 26
5 6 7 8 Sum of the row 2 =
42

Weightage - 25

Input Output

3 3 Sum of the row 0 = 250


98 87 65 Sum of the row 1 = 107
54 32 21 Sum of the row 2 =
159

Weightage - 25

Input Output

2 2 Sum of the row 0 = 30


20 10 Sum of the row 1 = 90
40 50 Sum of the column 0 =
60

Weightage - 10

Input Output

5 5 Sum of the row 0 = 1665


111 222 333 444 555 Sum of the row 1 = 4330
666 777 888 999 1000 Sum of the row 2 = 165

Weightage - 40

Sample Input Sample Output

4 4 Sum of the row 0 = 10


1 2 3 4 Sum of the row 1 = 26
5 6 7 8 Sum of the row 2 =
42

Sample Input Sample Output

3 3 Sum of the row 0 = 250


98 87 65 Sum of the row 1 = 107
54 32 21 Sum of the row 2 =
159

Solution

import
java.util.Scanner; class
matrix {
static void row_sum(int arr[][],int m, int n)
{

int i, j, sum = 0;
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
sum = sum + arr[i][j];
}
System.out.println("Sum of the
he row
ow " + i + " = "
+ sum);
sum = 0;
}
}
static void column_sum(int arr[][], int m,int n)
{

int i, j, sum = 0;
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j)
{ sum = sum + arr[j]
[i];
}
System.out.println("Sum of the column " + i
+ " = " + sum);
sum = 0;
}
}
public static void main(String[] args)
{
int i, j,m,n;
Scanner sc = new Scanner(System.in);
m = sc.nextInt();
n = sc.nextInt();
int[][] arr = new int[m][n];
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
arr[i][j] = sc.nextInt();
row_sum(arr,m,n);
column_sum(arr,m,n);
}
}

Q23
Test Case

Input Output

lemon 2

Weightage - 10

Input Output

follow 2

Weightage - 10

Input Output

member 2

Weightage - 20
Input Output

tree 2

Weightage - 20

Input Output

yellow 2

Weightage - 40

Sample Input Sample Output

string 1

Solution

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in); String input =
scanner.nextLine();
scanner.close();

int count = countVowels(input);


System.out.println(count);
}

public static int countVowels(String


str) { int count = 0;
str = st
tr.
.to
oLo
owe
erC
Cas
se()
(); // Convert the string to lowercase to simplify comparison

// Iterate through each character in the


string for (int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
// Check if the
he character is a vowel (a, e, i, o, u)
if (ch == 'a'
a' || ch == 'e'
e' || ch == 'i'
i' || ch == 'o' || ch == 'u')
{ count++;
}
}
return count;
}
}

Q24
Test Case

Input Output

nisha -2
priya
Weightage - 10

Input Output

richard -6
roy

Weightage - 10

Input Output

solo 6
minion

Weightage - 20

Input Output

alloy -12
metal

Weightage - 20

Input Output

unique 1
talent

Weightage - 40

Sample Input Sample Output

harry 0
harry

Sample Input Sample Output

john -3
mini

Sample Input Sample Output

tiger 8
lion

Solution

import java.util.Scanner;
class Main{
public static void main(String
args[]) { Scanner in=new
Scanner(System.in);
String str1,str2;
str1=in.nextLine();
str2=in.nextLine();
int result = str1.compareTo( str2 );
System.out.println(result);
}
}

Q25
Test Case

Input Output

rapid fire rapidfire

Weightage - 10

Input Output

skills are developed skillsaredeveloped

Weightage - 10

Input Output

ranging from the begining rangingfromthebegining

Weightage - 20

Input Output

computer computer

Weightage - 20

Input Output

allen harry allenharry

Weightage - 40

Sample Input Sample Output

kite in the cloud kiteinthecloud

Sample Input Sample Output

networking networking
Solution

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
// Create a Scanner object for user
input Scanner scanner = new
Scanner(System.in);

// Prompt the user to enter a


sentence String sentence =
scanner.nextLine();

// Remove all spaces from the sentence


String sentenceWithoutSpaces = sentence.replaceAll("\\s", "");

// Print the modified sentence without spaces


System.out.println(sentenceWithoutSpaces);

// Close the scanner


scanner.close();
}
}

Q26
Test Case

Input Output

noon noon :palindrome

Weightage - 20

Input Output

morn morn :not a palindrome

Weightage - 20

Input Output

butteritwasbutterlove butteritwasbutterlove :not a palindrome

Weightage - 30

Input Output

kik kik :palindrome

Weightage - 10
Input Output

late late :not a palindrome

Weightage - 10

Input Output

madam madam :palindrome

Weightage - 10

Sample Input Sample Output

mom mom :palindrome

Sample Input Sample Output

norms norms :not a palindrome

Sample Input Sample Output

butitwaslateonthatday butitwaslateonthatday :not a palindrome

Solution

import java.util.*;
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
String str, reverse = "";
Scanner in = new

Scanner(System.in); str =

in.nextLine();

int length = str.length();


for (int i = length - 1; i >= 0; i--)
reverse = reverse + str.charAt(i);
if (str.equals(reverse))
System.out.println(str+" :palindrome
"); else
System.out.println(str+" :not a palindrome");
}
}

Q27
Test Case
Input Output

786 786

Weightage - 10

Input Output

456 456

Weightage - 25

Input Output

4851 4851

Weightage - 25

Input Output

a1234b 0

Weightage - 40

Sample Input Sample Output

234 234

Sample Input Sample Output

a12 0

Solution

import
java.util.*;
import
java.lang.*;
import java.io.*;

class Q01Medium_StringAdv {

public static int convert(String str)


{ int val = 0;
try {
val = Integer.parseInt(str);
} catch (NumberFormatException e) {
// Invalid String
}
return val;
}

public static void main(String[] args) {


Scanner input = new Scanner(System. in);
String str = input.nextLine();

int val = convert(str);


System.out.println(val);
}
}

Q28
Test Case

Input Output

ABABAB Yes

Weightage - 10

Input Output

ABC No

Weightage - 10

Input Output

XYXYX Yes

Weightage - 10

Input Output

IJIJIJ Yes

Weightage - 15

Input Output

KLKLK Yes

Weightage - 15

Input Output

IJK No
Weightage - 10

Input Output

IJIJK No

Weightage - 20

Input Output

WXYZ No

Weightage - 10

Sample Input Sample Output

ABABAB Yes

Sample Input Sample Output

ABC No

Sample Input Sample Output

XYXYX Yes

Solution

import java.io.*;
import java.util.*;
class AlternatingCode {
public static void main(String []
args) { int i,s=0;
String a;
char x,y;
Scanner sc = new
Scanner(System.in); a =
sc.next();
x =
a.charAt(0); y
= a.charAt(1);
if(x==y) {
s=1;
}
else{
for(i=0;i<a.length();i++)
{
if(i%2==0) {
if(x!=a.charAt(i)) {
s=1;
break;
}
}
else if(y!= a.charAt(i)) {
s=1;
break;
}
}
}
if(s==1) {
System.out.println("No");
}
else {
System.out.println("Yes");
}

}
}

Q29
Test Case

Input Output

hello hel*lo

Weightage - 10

Input Output

aabb a*ab*b

Weightage - 10

Input Output

abba ab*ba

Weightage - 10

Input Output

abbaab ab*ba*ab

Weightage - 15

Input Output

aaabbb a*a*ab*b*b

Weightage - 15
Input Output

abbabaab ab*baba*ab

Weightage - 20

Input Output

aaaabbbb a*a*a*ab*b*b*b

Weightage - 20

Sample Input Sample Output

hello hel*lo

Sample Input Sample Output

aaabbb a*a*ab*b*b

Solution

import java.io.*;
import java.util.*;
class AdjacentCharacters {
public static void main(String [] args)
{ String s;
Scanner sc = new
Scanner(System.in); s =
sc.next();
pairStar(s,0);
System.out.println(s1);
for(int i=0; i<s.length();i++)
{ s1 = s1+s.charAt(i);
if(i == s.length()-1) {
return;
}
if(s.charAt(i) == s.charAt(i+1))
{ s1 = s1+'*';
}
}
}
static String s1="";
private static void pairStar(String s, int
i) { s1 = s1+s.charAt(i);
if(i == s.length()-1) {
return;
}
if(s.charAt(i) == s.charAt(i+1))
{ s1 = s1+'*';
}
pairStar(s,i+1);
}
}
Q30
Test Case

Input Output

+919874653210 Mobile number valid

Weightage - 15

Input Output

9874653210 Mobile number invalid

Weightage - 15

Input Output

+919633319072 Mobile number valid

Weightage - 20

Input Output

9633319072 Mobile number invalid

Weightage - 20

Input Output

+91874444883 Mobile number invalid

Weightage - 30

Sample Input Sample Output

+919874653210 Mobile number valid

Sample Input Sample Output

9874653210 Mobile number invalid

Solution
import java.io.*;
import java.util.*;
class Main {
public static void main(String []
args) { Scanner sc = new
Scanner(System.in); String mobile
= sc.nextLine();
if(mobile.substring(0, 3).compareTo("+91") == 0 && mobile.substring(3).length() == 10) {
System.out.println("Mobile number valid");
}
else {
System.out.println("Mobile number invalid");
}
}}

Q31
Test Case

Input Output

3abcdef 3abcdef
Not matched!

Weightage - 10

Input Output

abcdef9 abcdef9
Found a match!

Weightage - 25

Input Output

ghijklm ghijklm
Not matched!

Weightage - 25

Input Output

defg21494 defg21494
Found a
match!

Weightage - 40

Sample Input Sample Output

abcdef abcdef
Not matched!

Sample Input Sample Output

abcdef3459 abcdef3459
Found a
match!
Solution

import java.util.*;
import
java.util.regex.Matcher;
import
java.util.regex.Pattern;
class Main
{

public static void main(String[] args)


{

Scanner s = new Scanner(System.in);


String str;
str=s.nextLine();
System.out.println(str);
System.out.println(validate(str));
}

public static String validate(String text)


{
Pattern pattern = Pattern.compile(".*[0-
9]$"); Matcher m = pattern.matcher(text);

if (m.find())
return "Found a match!";
else
return "Not matched!";
}
}

Q32
Test Case

Input Output

21/04/1991 The datatype of 21/04/1991 is: java.util.Date

Weightage - 10

Input Output

04/21/1991 The datatype of 04/21/1991 is: java.util.Date

Weightage - 25

Input Output

21-apr-92 The datatype of 21-apr-92 is: java.util.Date

Weightage - 30

Input Output
21-apr-1994 The datatype of 21-apr-1994 is: java.util.Date

Weightage - 25

Input Output

Core The datatype of Core is: java.lang.String

Weightage - 10

Sample Input Sample Output

100 The datatype of 100 is: java.lang.Integer

Sample Input Sample Output

52.87 The datatype of 52.87 is: java.lang.Double

Sample Input Sample Output

21-apr-1994 The datatype of 21-apr-1994 is: java.util.Date

Sample Input Sample Output

Born to win The datatype of Born to win is:


java.lang.String

Solution

import java.util.*;
class Main
{
public static void main(String[] arg)
{

String input;
Scanner s = new Scanner(System.in);
input = s.nextLine();
String dataType = null;

// checking for Integer


if (input.matches("\\d+"))
{
dataType = "java.lang.Integer";
}

// checking for floating point


numbers else if (input.matches("\\
d*[.]\\d+"))
{
dataType = "java.lang.Double";
}

// checking for date format dd/mm/yyyy


else if (input.matches( "\\d{2}[/]\\d{2}[/]\\d{4}"))
{
dataType = "java.util.Date";
}

// checking for date format mm/dd/yyyy


else if (input.matches("\\d{2}[/]\\d{2}[/]\\d{4}"))
{
dataType = "java.util.Date";
}

// checking for date format dd-mon-yy


dd-mon-yy
else if (input.matches("\\d{2}[-]\\w{3}[-]\\d{2}"))
(input.matches("\\d{2}[-]\\w{3}[-]\\d{2}"))
{
dataType = "java.util.Date";
}

// checking for date format dd-mon-yyyy


dd-mon-yyyy
else if (input.matches( "\\d{2}[-]\\w{3}[-]\\d{4}"))
"\\d{2}[-]\\w{3}[-]\\d{4}"))
{
dataType = "java.util.Date";
}

// checking for date format dd-month-yy


dd-month-yy
else if (input.matches("\\d{2}[-]\\w+[-]\\d{2}"))
(input.matches("\\d{2}[-]\\w+[-]\\d{2}"))
{
dataType = "java.util.Date";
}

// checking for date format dd-month-yyyy


dd-month-yyyy
else if (input.matches("\\d{2}[-]\\w+[-]\\d{4}"))
(input.matches("\\d{2}[-]\\w+[-]\\d{4}"))
{
dataType = "java.util.Date";
}

// checking for date format yyyy-mm-dd


yyyy-mm-dd
else if (input.matches( "\\d{4}[-]\\d{2}[-]\\d{2}"))
"\\d{4}[-]\\d{2}[-]\\d{2}"))
{
dataType = "java.util.Date";
}

// checking for String


else
{
dataType = "java.lang.String";
}

System.out.println("The datatype of " + input + " is: " + dataType);


}
}

Q33
Test Case

Input Output

SQL SQL
true

Weightage - 10
Input Output

Java Java
true

Weightage - 25

Input Output

www3.com www3.com
false

Weightage - 25

Input Output

!@#$ !@#$
false

Weightage - 40

Sample Input Sample Output

ABCDEFabcdef123456 ABCDEFabcdef123456
true

Sample Input Sample Output

W3.com W3.com
false

Solution

import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner s = new
Scanner(System.in); String str;
str=s.nextLine();
System.out.println(str);
System.out.println(validate(str));
}
public static boolean validate(String text)
{
return text.matches("^[\\w]+$");
}
}

Q34
Test Case

Input Output
_number _number:Invalid Identifier

Weightage - 10

Input Output

NUMBER NUMBER:Valid Identifier

Weightage - 25

Input Output

NumBer NumBer:Valid Identifier

Weightage - 25

Input Output

123abc 123abc:Invalid Identifier

Weightage - 40

Sample Input Sample Output

ashok ashok:Valid Identifier

Sample Input Sample Output

?ashok ?ashok:Invalid Identifier

Solution

import java.util.regex.*;
import java.util.*;
class Main
{
public static void main(String[] args)
{
Pattern p=Pattern.compile("[a-zA-Z][a-zA-Z0-9-#]
+"); Scanner s = new Scanner(System.in);
String str;
str=s.nextLine();

Matcher m=p.matcher(str);

if(m.find()&&m.group().equals(str))
{
System.out.print(str+":"+"Valid Identifier");
}
else
{
System.out.print(str+":"+"Invalid Identifier");
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy