Practice Coding Questions
Practice Coding Questions
Note: Most of the questions have been developed using the following source, you can
refer to this link to study more about these topics or any other topics:
https://www.geeksforgeeks.org/java/
Q1 Scan a word given as an input. Then, in the next line scan two numbers given to you
as input (space-separated, in a single line). Then print that word and the sum of the
numbers as the output in the same line. Make a class named Source and do the
required operations (scanning, addition and printing) inside the main method.
E.g: Input
Plant
61
Output
Plant7
Q2 You will be given 7 different inputs in different lines, each having a different data
type. The inputs will be in the order of: char, int, byte, short, double, string and float. You
have to store all these values in different variables of the associated data type and then
print them as the output in a certain order shown in the below example.
E.g: Input
R
47
4
56
88.355
UpGrad
6.0000145f
Output
UpGrad 6.0000145 56 R 47 88.355 4
Q3 You are given a piece of code (not the complete code) below. It has an ‘if-else if’
structure handling the String named value, which stores the name of a fruit or
vegetable, and the appropriate output is printed according to what is stored in value.
You are required to change this structure into a switch-case statement structure doing
the same thing (you have to write the full code). Scan the name of the fruit/vegetable
(given to you as an input) into ‘value’.
E.g: Input
apple
Output
Fruit apple
Tags: conditional and control statements
Q4 You will be given an integer as an input. You have to output yes or no depending
upon whether that number is prime or not. Note: you have to use while loop in your
solution.
E.g: Input
28
Output
no
Q4 You will be given an integer as an input. You have to print the following pattern
(shown below) for that many number of lines. Note: you have to use for loop(s) in your
solution.
E.g: Input
5
Output
-----$-
----$-$-
---$-$-$-
--$-$-$-$-
-$-$-$-$-$-
Tags: for loops