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

Practice Coding Questions

This document provides 4 questions to test coding skills in Java. Question 1 involves taking user input of a word and numbers, summing the numbers, and outputting the word and sum. Question 2 takes different data types as input and outputs them in a specified order. Question 3 changes an if-else statement to a switch case to output fruit/vegetable names. Question 4 checks if a number is prime using a while loop.

Uploaded by

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

Practice Coding Questions

This document provides 4 questions to test coding skills in Java. Question 1 involves taking user input of a word and numbers, summing the numbers, and outputting the word and sum. Question 2 takes different data types as input and outputs them in a specified order. Question 3 changes an if-else statement to a switch case to output fruit/vegetable names. Question 4 checks if a number is prime using a while loop.

Uploaded by

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

Week 1 Questions

Introduction to coding (Basics)

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

Tags​: class, main method, input-output handling

Links for help:


1. https://www.geeksforgeeks.org/scanner-class-in-java/
2. https://www.javatpoint.com/java-bufferedreader-class

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

Tags​: data types and variables

Links for help:


1. https://learn.upgrad.com/v/course/119/session/8817/segment/42790
2. https://www.geeksforgeeks.org/variables-in-java/
3. https://www.geeksforgeeks.org/data-types-in-java/
4. https://learn.upgrad.com/v/course/119/session/8817/segment/42791

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’.

String value = sc.nextLine();


if ("apple".equals(value )) {
System.out.println("Fruit "+value);
}
else if ("tomato".equals(value )) {
System.out.println("Fruit/Vegetable "+value);
}
else if ("mango".equals(value )) {
System.out.println("Fruit "+value);
}
else if ("potato".equals(value )) {
System.out.println("Vegetable "+value);
}

E.g: Input
apple

Output
Fruit apple
Tags​: conditional and control statements

Links for help:


1. https://stackoverflow.com/questions/10240538/use-string-in-switch-case-in-java
2. https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continu
e-jump/
3. https://learn.upgrad.com/v/course/119/session/8817/segment/42793
4. https://www.geeksforgeeks.org/switch-statement-in-java/

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

Tags​: while loops

Links for help:


1. https://learn.upgrad.com/v/course/119/session/8817/segment/42794
2. https://www.geeksforgeeks.org/loops-in-java/
3. https://www.programiz.com/java-programming/examples/prime-number

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

Links for help:


1. https://learn.upgrad.com/v/course/119/session/8817/segment/42794
2. https://www.geeksforgeeks.org/loops-in-java/
3. https://stackoverflow.com/questions/21373738/simple-java-program-of-pyramid

Links for further reading:


1. https://www.geeksforgeeks.org/java-identifiers/
2. https://www.javatpoint.com/java-tutorial
3. https://www.geeksforgeeks.org/enum-in-java/
4. https://www.geeksforgeeks.org/for-each-loop-in-java/
5. https://www.journaldev.com/13134/file-handling-in-java
6. https://www.tutorialspoint.com/java/java_exceptions.htm
7. https://www.geeksforgeeks.org/variable-scope-in-java/

You might also like