Java MCQ
Java MCQ
JVM: One of the main features of Java is Write Once Run Anywhere, i.e. it is
platform-independent. It can run on any OS irrespective of the environment because
of Java Virtual Machine.
Java MCQ
1.
Number of primitive data types in Java are?
6
7
8
9
Hide
Wrong Answer
There are 8 types of primitive data types- int, char, boolean, byte, long, float, short,
double.
2.
What is the size of float and double in java?
float 4 bytes
double 8 bytes
32 and 64
32 and 32
64 and 64
64 and 32
Hide
Wrong Answer
The size of float and double in java is 32 and 64.
3.
Automatic type conversion is possible in which of the possible cases?
Byte to int
Int to long
Long to int
Short to int
Hide
Wrong Answer
Automatic type conversion is possible in Int to long.
4.
Find the output of the following code.
int Integer = 24;
char String = ‘I’;
System.out.print(Integer);
System.out.print(String);
Compile error
Throws exception
I
24 I
Hide
Wrong Answer
24 I will be printed.
5.
Find the output of the following program.
public class Solution{
public static void main(String[] args){
short x = 10;
x = x * 5;
System.out.print(x);
}
}
50
10
Compile error
Exception
Hide
Wrong Answer
This will give compile error - “Lossy conversion from int to short”
6.
Find the output of the following program.
public class Solution{
public static void main(String[] args){
byte x = 127;
x++;
x++;
System.out.print(x);
}
}
-127
127
129
2
Hide
Wrong Answer
Range of byte data in java is -128 to 127. But the byte data type in java is cyclic in
nature.
7.
Select the valid statement.
char[] ch = new char(5)
char[] ch = new char[5]
char[] ch = new char()
char[] ch = new char[]
Hide
Wrong Answer
char[] ch = new char[5] is the correct syntax for declaring a character array.
8.
Find the output of the following program.
public class Solution{
public static void main(String[] args){
int[] x = {120, 200, 016};
for(int i = 0; i < x.length; i++){
System.out.print(x[i] + “ “);
}
}
}
120 200 016
120 200 14
120 200 16
None
Hide
Wrong Answer
016 is an octal number, its equivalent decimal number is 14. Hence answer is B.
9.
When an array is passed to a method, what does the method receive?
The reference of the array
A copy of the array
Length of the array
Copy of first element
Hide
Wrong Answer
When an array is passed to a method, a reference of the array is received by the
method.
10.
Select the valid statement to declare and initialize an array.
int[] A = {}
int[] A = {1, 2, 3}
int[] A = (1, 2, 3)
int[][] A = {1,2,3}
Hide
Wrong Answer
int[] A = {1, 2, 3} is the valid way of declaring arrays.
11.
Find the value of A[1] after execution of the following program.
int[] A = {0,2,4,1,3};
for(int i = 0; i < a.length; i++){
a[i] = a[(a[i] + 3) % a.length];
}
0
1
2
3
Hide
Wrong Answer
a.length = 5
A[0] = a[(0 + 3) % 5] = a[3] = 1
So, a[0] = a[3] = 1
A[1] = a[(2 + 3) % 5] = a[0] = 1
Therefore, a[1] = 1;
12.
Arrays in java are-
Object references
objects
Primitive data type
None
Hide
Wrong Answer
Arrays are objects in java. It is a container that holds a fixed number of items of a
single type.
13.
When is the object created with new keyword?
At run time
At compile time
Depends on the code
None
Hide
Wrong Answer
The object created with new keyword during run-time.
14.
Identify the corrected definition of a package.
A package is a collection of editing tools
A package is a collection of classes
A package is a collection of classes and interfaces
A package is a collection of interfaces
Hide
Wrong Answer
A package is a collection of classes and interfaces.
15.
Identify the correct restriction on static methods.
I and II
II and III
Only III
I, II and III
Hide
Wrong Answer
Static methods must only access static data and can call other static methods.
Moreover they cannot refer this or super.
16.
Identify the keyword among the following that makes a variable belong to a
class,rather than being defined for each instance of the class.
final
static
volatile
abstract
Hide
Wrong Answer
Static keyword makes a variable belong to a class,rather than being defined for each
instance of the class.
17.
Identify what can directly access and change the value of the variable res.
Package com.mypackage;
Public class Solution{
Private int res = 100;
}
Any class
Only Solution class
Any class that extends Solution
None
Hide
Wrong Answer
Only solution class can directly access and change the value of the variable res.
18.
In which of the following is toString() method defined?
java.lang.Object
java.lang.String
java.lang.util
None
Hide
Wrong Answer
toString() is defined in java.lang.Object.
19.
compareTo() returns
True
False
An int value
None
Hide
Wrong Answer
compareTo() returns an int value
20.
Identify the output of the following program.
String str = “abcde”;
System.out.println(str.substring(1, 3));
abc
bc
bcd
cd
Hide
Wrong Answer
str.substring(start, end) returns the string from s[start] till s[end - 1]
21.
Identify the output of the following program.
String str = “Hellow”;
System.out.println(str.indexOf(‘t));
0
1
true
-1
Hide
Wrong Answer
Since, t isn’t present in the string str, it returns -1.
22.
Identify the output of the following program.
Public class Test{
Public static void main(String argos[]){
String str1 = “one”;
String str2 = “two”;
System.out.println(str1.concat(str2));
}
}
one
two
onetwo
twoone
Hide
Wrong Answer
concat attached both the string. Hence answer is C.
23.
What does the following string do to given string str1.
String str1 = “Interviewbit”.replace(‘e’,’s’);
Replaces single occurrence of ‘e’ to ‘s’.
Replaces all occurrences of ‘e’ to ‘s’.
Replaces single occurrence of ‘s’ to ‘e’.
None.
Hide
Wrong Answer
replace() replaces all the occurrences of the oldcharacter by the newcharacter.
24.
To which of the following does the class string belong to.
java.lang
java.awt
java.applet
java.string
Hide
Wrong Answer
string class belongs to java.lang.
25.
What does the following string do to given string str1.
String a = new String(“Interviewbit”);
String b = new String(“Interviewbit”);
Strinc c = “Interviewbit”;
String d = “Interviewbit”;
2
3
4
None
Hide
Wrong Answer
Using the new keyword creates an object everytime. Hence, 2 objects are created for
first two statement. Next, a string is declared which creates another object. For the
fourth statement, since, a string ”Interviewbit” already exists, it doesn’t create an
additional object again. Hence, answer is 3.
26.
Total constructor string class have?
3
7
13
20
Hide
Wrong Answer
String class has 13 constructors.
27.
Find the output of the following code.
int ++a = 100;
System.out.println(++a);
101
Compile error as ++a is not valid identifier
100
None
Hide
Wrong Answer
It shows compile error as ++a is not valid identifier.
28.
Find the output of the following code.
if(1 + 1 + 1 + 1 + 1 == 5){
System.out.print(“TRUE”);
}
else{
System.out.print(“FALSE”);
}
TRUE
FALSE
Compile error
None
Hide
Wrong Answer
Since, LHS matches RHS, hence the output is TRUE.
29.
Find the output of the following code.
Public class Solution{
Public static void main(String… argos){
Int x = 5;
X * = 3 + 7;
System.out.println(x);
50
22
10
None
Hide
Wrong Answer
x* = 3 + 7 is equivalent to x * (3 + 7) = x * 10. Therefore, x = 50.
30.
Identify the return type of a method that does not return any value.
int
void
double
None
Hide
Wrong Answer
void does not return any value.
31.
Output of Math.floor(3.6)?
3
3.0
4
4.0
Hide
Wrong Answer
floor returns largest integer that is less than or equal to the given number.
32.
Where does the system stores parameters and local variables whenever a method is
invoked?
Heap
Stack
Array
Tree
Hide
Wrong Answer
The system stores parameters and local variables in a stack.
33.
Identify the modifier which cannot be used for constructor.
public
protected
private
static
Hide
Wrong Answer
Static cannot be used for constructor.
34.
What is the variables declared in a class for the use of all methods of the class called?
Object
Instance variables
Reference variable
None
Hide
Wrong Answer
It is know as instance variable.
35.
What is the implicit return type of constructor?
No return type
A class object in which it is defined
void
None
Hide
Wrong Answer
Implicit return type of constructor is the class object in which it is defined.
36.
When is the finalize() method called?
Before garbage collection
Before an object goes out of scope
Before a variable goes out of scope
None
Hide
Wrong Answer
finalize() method is called before garbage collection.
37.
Identify the prototype of the default constructor.
Public class Solution {}
Solution(void)
Solution()
public Solution(void)
public Solution()
Hide
Wrong Answer
public Solution() is the prototype of the default constructor.
38.
Identify the correct way of declaring constructor.
Public class Solution {}
Solution(){}
public Solution(){}
Solution(void){}
Both (A) and (B)
Hide
Wrong Answer
Both A and B are correct way of declaring constructor.
39.
Find the output of the following code.
Public class Solution{
Public static void main(String args[]){
Int i;
for(i = 1; i < 6; i++){
if(i > 3) continue;
}
System.out.println(i);
}
}
3
4
5
6
Hide
Wrong Answer
Since, the loop runs till 6, the value of i is 6.
40.
How many times will “Interviewbit” be printed.
Int count = 0;
do{
System.out.println(“Interviewbit”);
count++;
} while(count < 10);
8
9
10
11
Hide
Wrong Answer
Interviewbit will be printed 10 times, starting from count = 0.
41.
Identify the infinite loop.
for(; ;)
for(int i = 0; i < 1; i--)
for(int i = 0; ;i++)
All of the above
Hide
Wrong Answer
All of the above are infinite loop.
42.
What is Runnable?
Abstract class
Interface
Class
Method
Hide
Wrong Answer
Runnable is an interface.
43.
Excepted created by try block is caught in which block.
catch
throw
final
none
Hide
Wrong Answer
Excepted created by try block is caught in throw block.
44.
Which of the following exception is thrown when divided by zero statement is
executed?
NullPointerException
NumberFormatException
ArithmeticException
None
Hide
Wrong Answer
ArithmeticException is thrown when divided by zero statement is executed.
45.
Where is System class defined?
java.lang.package
java.util.package
java.io.package
None
Hide
Wrong Answer
System class is defined in java.lang.package.
46.
Identify the interface which is used to declare core methods in java?
Comparator
EventListener
Set
Collection
Hide
Wrong Answer
Collection is used to declare core methods in java.
47.
Which of the following statements are true about finalize() method?
It can be called Zero or one times
It can be called Zero or more times
It can be called Exactly once
It can be calledOne or more times
Hide
Wrong Answer
The finalize() method can be called Zero or one times.
48.
What does the operator >>>> do?
Right shift operator
Left shift operator
Zero fill left shift
Zero fill right shift
Hide
Wrong Answer
>>>> is Zero fill right shift.
49.
Identify the incorrect Java feature.
Object oriented
Use of pointers
Dynamic
Architectural neural
Hide
Wrong Answer
Java does have the concept of pointers.
50.
Which of the following is used to find and fix bugs in the program?
JDK
JRE
JVM
JDB
Hide
Wrong Answer
JDB is used to find and fix bugs in the program.
Java Section
1. What will be the output of the following program ?
class Parent
{
public void display ()
{
System.out.println ("Parent");
}
}
Compilation error
Hide Explanation
Display method in child class is overridden, so method resolution is based on the
object.Hence the answer is Parent Child Child.
class StringDemo
{
public static void main (String[]args)
{
String s1 = new String ("This is prepinsta Material");
String s2 = new String ("This is prepinsta Material");
System.out.println (s1 == s2);
String s3 = "This is prepinsta Material";
System.out.println (s1 == s3);
String s4 = "This is prepinsta Material";
System.out.println (s3 == s4);
String s5 = "This is ";
String s6 = s5 + "prepinsta Material";
System.out.println (s3 == s6);
final String s7 = "This is ";
String s8 = s7 + "prepinsta Material";
System.out.println (s3 == s8);
System.out.println (s5 == s7);
}
}
class Solution
{
public static void main (String[]args)
{
StringBuffer sb = new StringBuffer ("abcdcbd");
sb = sb.reverse ();
sb.setCharAt (0, 'a');
sb.setCharAt (6, 'd');
System.out.println (sb);
}
}
dbcdcba
abcdcba
abcdcbd
Prepinsta Prepinsta
Runtime error
class Sample
{
protected void display ()
{
System.out.println ("Parent class");
}
}
public class Main extends Sample
{
protected void display ()
{
System.out.println ("Child class");
}
Parent class
Child class
No output
Hide Explanation
In case of overriding the method resolution is based on object type. Here in the
above question, the object is of type Main therefore the display method present
inside main class will get executed.Hence the output is “Child class”.
true false
false true
true true
false false
Hide Explanation
Equals method is overridden inside the string class for content comparison whereas
== operator compares the reference of the string. Therefore option B is correct.
8. What are the major components of the JDBC?
null
Sudhir
102
Hide Explanation
If we remove the element from map it will return the value present at that key.Since
in our case value present at 102 is sudhir. Therefore Sudhir will return and print as an
output.
return result;
}
So, The given code takes a number ‘no’ and returns the sum of its digits.
Question-1
Convert the same result to the U.S. style of miles per gallon and display the result. If
the quantity or distance is zero or negative display ” is an Invalid Input”.
The result should be with two decimal place.To get two decimal place refer the
below-mentioned print statement :
float cost=670.23;
20
150
Sample Output 1:
Liters/100KM
13.33
Miles/gallons
17.64
Explanation:
Sample Input 2:
-5
Sample Output 2:
-5 is an Invalid Input
Sample Input 3:
Enter the no of liters to fill the tank
25
-21
Sample Output 3:
C
Java
Python
#include<stdio.h>
#include <stdlib.h>
int main()
{
printf("Enter the no of liters to fill the tank\n");
int ltt ;
scanf("%d",<t);
if(diss<1)
{
printf(" %d is an Invalid Input\n",diss);
exit(0);
}
return 0;
}
Question-2
Problem Statement – Vohra went to a movie with his friends in a Wave theatre and
during break time he bought pizzas, puffs and cool drinks. Consider the following
prices :
Rs.100/pizza
Rs.20/puffs
Rs.10/cooldrink
Sample Input 1:
Sample Output 1:
Bill Details
No of pizzas:10
No of puffs:12
No of cooldrinks:5
Total price=1290
C
Java
Python
#include<stdio.h>
#include <stdlib.h>
int main ()
{
int totalprice;
printf ("Enter the no of pizzas bought:\n");
int pizza;
scanf ("%d", &pizza);
printf ("Enter the no of puffs bought:\n");
int puffs;
scanf ("%d", &puffs);
return 0;
}
Question-3
Problem Statement – Ritik wants a magic board, which displays a character for a
corresponding number for his science project. Help him to develop such an
application.
For example when the digits 65,66,67,68 are entered, the alphabet ABCD are to be
displayed.
[Assume the number of inputs should be always 4 ]
Sample Input 1:
Sample Output 1:
65-A
66-B
67-C
68-D
Sample Input 2:
Sample Output 2:
115-s
116-t
101-e
112-p
C
Java
Pyhton
#include<stdio.h>
#include <stdlib.h>
int main()
{
printf("Enter the digits: \n");
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
char q=(char)a;
char w=(char)b;
char e=(char)c;
char r=(char)d;
printf("%d - %c\n",a,q);
printf("%d - %c\n",b,w);
printf("%d - %c\n",c,e);
printf("%d - %c\n",d,r);
return 0;
}
Question-4
Problem Statement – FOE college wants to recognize the department which has
succeeded in getting the maximum number of placements for this academic year.
The departments that have participated in the recruitment drive are CSE,ECE, MECH.
Help the college find the department getting maximum placements. Check for all the
possible output given in the sample snapshot
Note : If any input is negative, the output should be “Input is Invalid”. If all
department has equal number of placements, the output should be “None of the
department has got the highest placement”.
Sample Input 1:
Sample Output 1:
Highest placement
CSE
Sample Input 2:
Enter the no of students placed in CSE:55
Enter the no of students placed in ECE:85
Enter the no of students placed in MECH:85
Sample Output 2:
Highest placement
ECE
MECH
Sample Input 3:
Sample Output 3:
Sample Input 4:
Sample Output 4:
Input is Invalid
C
Java
Python
#include<stdio.h>
#include <stdlib.h>
int main()
{
int cse;
int ece;
int mech;
// Take user input
}
//System.out.println("Highest Placement:");
// First, check if any two values are equal and greater than the third
else if (cse == ece && cse > mech)
{
printf("Highest Placement:\n");
printf("CSE\n");
printf("ECE\n");
}
else if (cse == mech && cse > ece)
{
printf("Highest Placement:\n");
printf("CSE\n");
printf("MECH\n");
}
else if (ece == mech && ece > cse)
{
printf("Highest Placement:\n");
printf("ECE\n");
printf("MECH\n");
}
// Now, if we reached here, all values are distinct
// Check if one value is greater than both
else if (cse > ece && cse > mech)
{
printf("Highest Placement:\n");
printf("CSE\n");
}
else if (ece > mech)
{
printf("Highest Placement:\n");
printf("ECE\n");
}
else
{
printf("Highest Placement:\n");
printf("MECH\n");
}
}
return 0;
}
Question-5
Problem Statement – In a theater, there is a discount scheme announced where one
gets a 10% discount on the total cost of tickets when there is a bulk booking of more
than 20 tickets, and a discount of 2% on the total cost of tickets if a special coupon
card is submitted. Develop a program to find the total cost as per the scheme. The
cost of the k class ticket is Rs.75 and q class is Rs.150. Refreshments can also be
opted by paying an additional of Rs. 50 per member.
Hint: k and q and You have to book minimum of 5 tickets and maximum of 40 at a
time. If fails display “Minimum of 5 and Maximum of 40 Tickets”. If circle is given
a value other than ‘k’ or ‘q’ the output should be “Invalid Input”.
Sample Input 1:
Sample Output 1:
Ticket cost:4065.25
Sample Input 2:
Sample Output 2:
C
Java
Python
#include<stdio.h>
#include <stdlib.h>
int main ()
{
int noTicket;
double total = 0, cost;
char ref[2], co[2], circle[2];
if (circle[0] == 'k')
cost = 75 * noTicket;
else if (circle[0] == 'q')
cost = 150 * noTicket;
else
{
printf ("Invalid Input");
exit (0);
}
total = cost;
if (co[0] == 'y')
total = cost - ((0.02) * cost);
if (ref[0] == 'y')
total += (noTicket * 50);
return 0;
}
Question-6
Problem Statement – Rhea Pandey’s teacher has asked her to prepare well for the
lesson on seasons. When her teacher tells a month, she needs to say the season
corresponding to that month. Write a program to solve the above task.
Month should be in the range 1 to 12. If not the output should be “Invalid month”.
Sample Input 1:
Sample Output 1:
Season:Autumn
Sample Input 2:
Sample Output 2:
Invalid month
C
Java
Python
#include<stdio.h>
#include <stdlib.h>
int main()
{
printf("Enter the month:");
int entry ;
scanf("%d",&entry);
switch (entry)
{
case 12:
case 1:
case 2:
printf("Season:Winter");
break;
case 3:
case 4:
case 5:
printf("Season:Spring");
break;
case 6:
case 7:
case 8:
printf("Season:Summer");
break;
case 9:
case 10:
case 11:
printf("Season:Autumn");
break;
default:
printf("Invalid month");
}
return 0;
}
Question-7
Write a java program to print all prime numbers in the interval [a,b] (a and b, both
inclusive).
Note
Input 1 should be lesser than Input 2. Both the inputs should be positive.
Range must always be greater than zero.
If any of the condition mentioned above fails, then display “Provide valid
input”
Use a minimum of one for loop and one while loop
Sample Input 1:
15
Sample Output 1:
2 3 5 7 11 13
Sample Input 2:
Sample Output 2:
C
Java
Python
#include<stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
int flag;
if(a<=0 || b<=0 || a>=b)
printf("Provide valid input");
else
{
while(a<=b)
{
if(a==2)
printf("%d ",a);
else if(a==1)
{
a++;
continue;
}
else
{
flag=0;
for(int i=2;i<=a/2;i++)
{
if(a%i==0)
{
flag=1;
break ;
}
}
if(flag==0)
printf("%d ",a);
}
a++;
}
}
return 0;
}
Question-8
Problem Statement – Goutam and Tanul plays by telling numbers. Goutam says a
number to Tanul. Tanul should first reverse the number and check if it is same as
the original. If yes, Tanul should say “Palindrome”. If not, he should say “Not a
Palindrome”. If the number is negative, print “Invalid Input”. Help Tanul by writing a
program.
Sample Input 1 :
21212
Sample Output 1 :
Palindrome
Sample Input 2 :
6186
Sample Output 2 :
Not a Palindrome
C
Java
Python
#include<stdio.h>
#include <stdlib.h>
int main()
{
int n ;
scanf("%d",&n);
int sum = 0, r;
int temp = n;
if(n>-1)
{
while(n>0)
{
r = n % 10;
sum = (sum*10)+r;
n = n/10;
}
if(temp==sum)
printf("Palindrome");
else
printf("Not a Palindrome");
}
else{
printf("Invalid Input");
}
return 0;
}
Question-9
XYZ Technologies is in the process of increment the salary of the employees. This
increment is done based on their salary and their performance appraisal rating.
1. If the appraisal rating is between 1 and 3, the increment is 10% of the salary.
2. If the appraisal rating is between 3.1 and 4, the increment is 25% of the
salary.
3. If the appraisal rating is between 4.1 and 5, the increment is 30% of the
salary.
Help them to do this, by writing a program that displays the incremented salary.
Write a class “IncrementCalculation.java” and write the main method in it.
Note : If either the salary is 0 or negative (or) if the appraisal rating is not in the
range 1 to 5 (inclusive), then the output should be “Invalid Input”.
Sample Input 1 :
8000
Sample Output 1 :
8800
Sample Input 2 :
7500
4.3
Sample Output 2 :
9750
Sample Input 3 :
Enter the salary
-5000
Sample Output 3 :
Invalid Input
Java
C
import java.util.*;
class IncrementCalculation{
if(salary<1||rating<1.0||rating>5.0){
System.exit(0);
}
salary=salary+(int)(0.1*salary);
System.out.println(salary);
}
salary=salary+(int)(0.25*salary);
System.out.println(salary);
}
salary=salary+(int)(0.3*salary);
System.out.println(salary);
}
}
Question-10
Problem Statement – Chaman planned to choose a four digit lucky number for his
car. His lucky numbers are 3,5 and 7. Help him find the number, whose sum is
divisible by 3 or 5 or 7. Provide a valid car number, Fails to provide a valid input then
display that number is not a valid car number.
Note : The input other than 4 digit positive number[includes negative and 0] is
considered as invalid.
Sample Input 1:
Sample Output 1:
Lucky Number
Sample Input 2:
Sample Output 2:
Sample Input 3:
Enter the car no:14
Sample Output 3:
Java
C++
import java.util.*;
class LuckyNum{
}
else{
while(carNum!=0){
sum=sum+l;
carNum=carNum/10;
}
if(sum%3==0||sum%5==0||sum%7==0){
}
else{
}
}
}
Question-11
Problem Statement –
Sample Input 1:
Enter no of course:
Oracle
C++
Mysql
Dotnet
C++
Sample Output 1:
Enter no of course:
Java
Oracle
Dotnet
C++
Sample Output 2:
Enter no of course:
Sample Output 3:
Invalid Range
Java
Python
C++
import java.util.*;
class Course
{
n= sc.nextInt();
if(n<=0||n>20)
{
System.exit(0);
}
sc.nextLine();
{
course[i] = sc.nextLine();
}
courseSearch=sc.nextLine();
{
if(course[i].equals(courseSearch))
{
flag=1;
}
}
if(flag==1)
{
}
else
{
}
}
Question-12
Problem Statement – Mayuri buys “N” no of products from a shop. The shop offers a
different percentage of discount on each item. She wants to know the item that has
the minimum discount offer, so that she can avoid buying that and save money.
[Input Format: The first input refers to the no of items; the second input is the item
name, price and discount percentage separated by comma(,)]
Assume the minimum discount offer is in the form of Integer.
Note: There can be more than one product with a minimum discount.
Sample Input 1:
mobile,10000,20
shoe,5000,10
watch,6000,15
laptop,35000,5
Sample Output 1:
shoe
Explanation: The discount on the mobile is 2000, the discount on the shoe is 500, the
discount on the watch is 900 and the discount on the laptop is 1750. So the discount
on the shoe is the minimum.
Sample Input 2:
Mobile,5000,10
shoe,5000,10
WATCH,5000,10
Laptop,5000,10
Sample Output 2:
Mobile
shoe
WATCH
Laptop
Java
Python
C++
import java.util.*;
import java.io.*;
{
//System.out.println(itemName[i]);
itemPrice[i]=Integer.parseInt(s[1]);
// System.out.println(itemPrice[i]);
itemDis[i]=Integer.parseInt(s[2]);
// System.out.println(itemDis[i]);
//float x = itemDis[i]
dis[i]=(float)((itemDis[i]*itemPrice[i])/100);
// System.out.println(dis[i]);
}
if(dis[i]<=min)
{
min=dis[i];
idx[j++]=i;
//System.out.println(min);
}
}
System.out.println(itemName[idx[i]]);
//System.out.println(idx[i]);
}
}
}
Question-13
Problem Statement – Raj wants to know the maximum marks scored by him in each
semester. The mark should be between 0 to 100 ,if goes beyond the range display
“You have entered invalid mark.”
Sample Input 1:
Enter no of semester:
2
Marks obtained in semester 1:
50
60
70
90
98
76
67
89
76
Sample Output 1:
Sample Input 2:
Enter no of semester:
4
Enter no of subjects in 3 semester:
55
67
98
67
-98
Sample Output 2:
Java
Python
C++
import java.util.*;
import java.lang.*;
import java.io.*;
class HighestMarkPerSem
{
arr[i]=sc.nextInt();
}
{
if(max<0||max>100)
{
System.exit(0);
}
{
if(marks<0||marks>100)
{
System.exit(0);
}
if(max<marks)
max=marks;
}
}
{
}
}
Question-14
Problem Statement – Bela teaches her daughter to find the factors of a given
number. When she provides a number to her daughter, she should tell the factors of
that number. Help her to do this, by writing a program. Write a class FindFactor.java
and write the main method in it.
Note :
If the input provided is negative, ignore the sign and provide the output. If the
input is zero
If the input is zero the output should be “No Factors”.
Sample Input 1 :
54
Sample Output 1 :
1, 2, 3, 6, 9, 18, 27, 54
Sample Input 2 :
-1869
Sample Output 2 :
Java
Python
C++
import java.util.*;
int i;
if(num==0){
}
for(i=1;i<num;i++){
if(num%i==0){
}
}
System.out.println(num);
}
else{
num=num*-1;
for(i=1;i<num;i++){
if(num%i==0){
}
}
System.out.println(num);
}
}