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

23CY201-JavaProgramming-CIA1-Answerky.docx

The document outlines the Continuous Internal Assessment I retest for the JAVA PROGRAMMING course, detailing course outcomes and a series of programming questions. It includes both theoretical questions about Java concepts and practical coding tasks that require students to write Java programs. The assessment is structured into two parts, with Part A focusing on short answer questions and Part B consisting of more complex programming tasks.

Uploaded by

Kishore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

23CY201-JavaProgramming-CIA1-Answerky.docx

The document outlines the Continuous Internal Assessment I retest for the JAVA PROGRAMMING course, detailing course outcomes and a series of programming questions. It includes both theoretical questions about Java concepts and practical coding tasks that require students to write Java programs. The assessment is structured into two parts, with Part A focusing on short answer questions and Part B consisting of more complex programming tasks.

Uploaded by

Kishore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

CONTINUOUS INTERNAL ASSESSMENT I RETEST

Regulation – 2022

Programme Semester Course Code Course Title

COMMON TO B.E. CSD


II 23CY201 JAVA PROGRAMMING
& CSE (CYBERSECURITY)
​ ​ ​ ​ ​ ​
COURSE OUTCOMES
C201.1 Infer the basic concepts of java programming.
C201.2 Illustrate the usage of different aspects of Controls statements in real world scenarios.
C201.3 Apply Array and strings in real time environment.
C201.4 Analyze and Interpret StringBuffer and StringBuilder Classes
C201.5 Utilize the functionalities of streams and java console class.

ANSWER ALL QUESTIONS


PART A (10 x 2 Marks = 20 Marks) BT CO MARKS
1.​ Illustrate the usage of access modifiers.

Access modifiers in Java are keywords that determine the


accessibility or visibility of classes, attributes, and methods.
There are five access modifiers in Java: public, protected,
default, private, and final.
U C201.1 2
●​ public: allows access from any class.
●​ protected: allows access from the same package and
subclasses.
●​ default (no keyword): allows access from the same
package only.
●​ private: allows access only within the same class.
Here is an example demonstrating the usage of access
modifiers with a class and its methods:

public class AccessModifiers {


public void publicMethod() {
System.out.println("Public Method");
}

protected void protectedMethod() {


System.out.println("Protected Method");
}

void defaultMethod() { // default (no keyword)


System.out.println("Default Method");
}

private void privateMethod() {


System.out.println("Private Method");
}
}
2.​ Compare keywords and identifiers.

Keywords are reserved words in Java that have a predefined


meaning and cannot be used as identifiers. Identifiers are
user-defined names given to classes, methods, variables, and
U C201.1 2
interfaces. Here is an example of keywords and identifiers:

Keywords: public, class, void, int, if, else (total 53 keywords)


Identifiers: studentName, calculateAge, myVariable, myClass
(user-defined)
3.​ Describe the function of the unary NOT (-) operator in java U
with a concise code snippet.

The unary NOT (~) operator in Java is a bitwise operator that


flips all the bits of a single binary number (integer). It can be
used to change the sign of a number by flipping all the bits
and adding 1 to it. The unary NOT operator has a higher U C201.1 2
precedence than any arithmetic or relational operators. Here is
an example:

int num = 5; // binary representation: 00000101 (decimal 5)


int result = ~num; // binary representation: 11111010
(decimal -6)
System.out.println(result); // prints -6 (flipped all bits and
added 1)
4.​ Illustrate an example demonstrating the usage of the AP
conditional (ternary) operator in Java.

The conditional (ternary) operator in Java is a concise way to


write an if-else statement using a single line of code. It has AP C201.1 2
three operands separated by a question mark (?) and a colon
(:). Here is an example: java int num = 5; String result = num >
0 ? "Positive" : "Non-Positive"; System.out.println(result); //
prints "Positive" less
5.​ Describe the functionality of the default keyword in a
switch-case statement with a concise example.

In Java, the default keyword in a switch-case statement is used


to specify a block of code to be executed when none of the
cases match the given value. It is optional, but recommended U C201.2 2
to include for readability purposes. Here is an example: java
int num = 7; switch (num) { case 5:
System.out.println("Number is 5"); break; case 6:
System.out.println("Number is 6"); break; default:
System.out.println("Number is not 5 or 6"); break; }
6.​ Build a java code demonstrating the usage of nested if-else
statements in Java.

java int num = 5; if (num > 0) { if (num % 2 == 0) { AP C201.2 2


System.out.println("Number is positive and even"); } else {
System.out.println("Number is positive and odd"); } } else {
System.out.println("Number is non-positive"); }
7.​ Showcase the application of the do-while loop in Java with a
code demonstration.
U C201.2 2
The do-while loop in Java is used when we want to execute a
block of code at least once before checking the condition for
termination
8.​ Compare and contrast for loop and the while loop in Java,
presenting distinct examples for each.

●​ For Loop Example: U C201.2 2


●​ for (int i = 0; i < 5; i++) {
●​ System.out.println("Iteration " + i);
}
●​ While Loop Example:
●​ int i = 0;
●​ while (i < 5) {
●​ System.out.println("Iteration " + i);
●​ i++;
}
9.​ Write the syntax for declaring and initializing an array in java.

In Java, you can declare and initialize an array of integers with


the values {1, 2, 3, 4, 5} using the following code snippet:
U C201.3 2
int[] arr = {1, 2, 3, 4, 5};

This statement declares an array of integers named arr and


initializes it with the values {1, 2, 3, 4, 5}
10.​ Build a java code to reverse the array elements.
1.​ public class ReverseArray {
2.​ public static void main(String[] args) {
3.​ //Initialize array
4.​ int [] arr = new int [] {1, 2, 3, 4, 5};
5.​ System.out.println("Original array: ");
6.​ for (int i = 0; i < arr.length; i++) {
7.​ System.out.print(arr[i] + " ");
8.​ } AP C201.3 2
9.​ System.out.println();
10.​ System.out.println("Array in reverse order: ");
11.​ //Loop through the array in reverse order
12.​ for (int i = arr.length-1; i >= 0; i--) {
13.​ System.out.print(arr[i] + " ");
14.​ }
15.​ }
16.​}

ANSWER ALL QUESTIONS


PART B (5 x 16 Marks = 80 Marks) BT CO MARK
S
11.​ i) Ramu and Somu are going on a picnic. Ramu packs m
apples, n oranges. Somu packs m1 more apples than Ramu
and n1 more oranges than Ramu.
If Somu eats x of his apples and Ramu eats y of Somu's
oranges, how many apples and oranges are left in total?
Input Format
●​ Input consists of 6 integers m,n,m1,n1,x,y.
1.​ m - corresponds to the apples brought by Ramu.
2.​ n - corresponds to the oranges brought by Ramu.
3.​ m1 – Apples brought by Somu.
4.​ n1 – Oranges brought by Somu.
5.​ x - apples ate by Somu.
6.​ y – oranges ate by Ramu.
Output Format
Print two integers representing remaining apples and
oranges.
Sample Input
432832
Sample Output
7 12

// You are using Java


AP C201.1 8
import java.io.*;
import java.util.*;
import java.lang.*;
class main
{
public static void main(String[] args)
{
int m,n,m1,n1,x,y;
Scanner sc=new Scanner(System.in);
m=sc.nextInt();
n=sc.nextInt();
m1=sc.nextInt();
n1=sc.nextInt();
x=sc.nextInt();
y=sc.nextInt();
int a,o;
a=(m+n);
o=(n1+m);
System.out.print(a+" ");
System.out.print(o);
}
}
ii) If the marks of Alice in 3 subjects are mark1, mark2, mark3.
Calculate the total and average.
Sample Input
96 98 94
Sample Output
Total : 288
Average : 96.00
// You are using Java
import java.io.*;
import java.util.*;
import java.lang.*;
class main
AP C201.1 8
{
public static void main(String[] args)
{
int m,n,o;
Scanner sc=new Scanner(System.in);
m=sc.nextInt();
n=sc.nextInt();
o=sc.nextInt();
System.out.println("Total : "+(m+n+o));
System.out.printf("Average : "+((m+n+o)/3),"%.2f");
}
}
OR
12.​ i) Write a java program to convert Celsius to Fahrenheit. The
formula is as follows, F = 1.8 C + 32.

Question Instructions:

1.​ Create a driver class named Main.


2.​ The solution code should be written inside the main
method() of the Main class
Sample Input
51 AP C201.1 8
Sample Output
123.8

// You are using Java


import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
double f,c;
Scanner sc=new Scanner(System.in);
c=sc.nextDouble();
f=1.8*c+32;
System.out.println(f);
​ }
}
ii) Write a program to find the square, cube, and square root of a
number.
Sample Input
5
Sample Output
Square of 5 is: 25.0
Cube of 5 is: 125.0
Square Root of 5 is: 2.23606797749979

// You are using Java


import java.io.*;
import java.util.*;
import java.lang.*;
class main AP C201.1 8
{
public static void main(String[] args)
{
int a;
Scanner sc=new Scanner(System.in);
a=sc.nextInt();
System.out.println("Square of " +a+" is: "+ a*a+".0");
System.out.println("Cube of " +a+" is: "+a*a*a+".0");
System.out.println("Square Root of " +a+" is:
"+Math.sqrt(a));
}
}

13.​ i) 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: AP C201.2 16
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.
Note : Gross Salary = Basic Salary+HRA+DA
Sample Input
12000
Sample Output
24600.00

// You are using Java


import java.util.*;
class main
{
public static void main(String args[])
{
double s,h,d,g;
Scanner sc=new Scanner(System.in);
s=sc.nextDouble();
if(s<15000)
{
h=(0.15*s);
d=(0.9*s);
g=(s+h+d);
System.out.printf("%.2f",g);
}
else
{
h=5000;
d=(0.98*s);
g=(s+h+d);
System.out.printf("%.2f",g);
}
}
}
OR
14.​ i) Write a java program to input the electricity unit consumed
and calculate the total electricity bill according to the given
conditions:
●​ For the first 50 units Rs. 3.50/unit
●​ For the next 100 units Rs. 4.50/unit
●​ For the next 100 units Rs. 5.20/unit
AP C201.2 16
●​ For units above 250 Rs. 6.75/unit
●​ An additional surcharge of 20% is added to the bill.
Sample Input
50
Sample Output
210.00
// You are using Java
import java.util.*;
class main
{
public static void main(String args[])
{
int u;
Scanner sc=new Scanner(System.in);
u=sc.nextInt();
double b=0;
if(u<=50)
{
b=u*3.5;
}
else if(u<=150)
{
b=((u-50)*4.5)+175;
}
else if(u<=250)
{
b=((u-150)*5.2)+625;
}
else if(u>=250)
{
b=((u-250)*6.75)+1145;
}
System.out.printf("%.2f",(b+(b*0.2)));
}
}

15.​ i) Write a java program to generate the first 'n' terms of the
following series 1, 2, 3, 6, 9, 18, 27,...
Sample Input
6
Sample Output
1 2 3 6 9 18
AP C201.3 16
import java.util.*;
class main
{
public static void main(String args[])
{
Scanner t=new Scanner(System.in);
int n=t.nextInt();
int a=1,b=2,sum;
System.out.print(a+" "+b+" ");
for(int i=3;i<=n;i++)
{
if(i%2!=0)
{
sum=a+b;
System.out.print(sum+" ");
}
else
{
sum=b*2;
System.out.print(sum+" ");
}
a=b;
b=sum;
}
}
}

OR
16.​ i) The factorial concept was explained in Seetha's math class.
Ankit discovered a connection between the sum of the
factorial of digits in a number and the number itself when
experimenting with numbers after understanding the idea of
factorial. He began looking for additional numbers like this
since he was so excited. Write a suitable Java program to
check the supplied numbers to aid Ankit.
Explanation:
Input:
145
Output:
145 Strong Number AP C201.3 16
In this case, the program reads the input integer 145 and
calculates the factorial of each digit of the number (1! + 4! +
5!). It then checks if the sum of the factorials is equal to the
original number, which is true for 145, so the program prints
"145 Strong Number" on the console as the output.
Sample Input
145
Sample Output
145 Strong Number

// You are using Java


import java.util.*;
class main
{
public static void main(String args[])
{
Scanner t=new Scanner(System.in);
int n=t.nextInt();
int m=n;
int sum=0;
while(n>0)
{
int c=n%10;
int pro=1;
for(int i=1;i<=c;i++)
{
pro*=i;
}
n=n/10;
sum+=pro;
}
if(sum==m)
{
System.out.println(m+" Strong Number");
}
else
{
System.out.println(m+" Not Strong Number");
}
}
}

17.​ i) Write a program to print the following pattern.


Sample Input
5
Sample Output
1
01
101
AP C201.3 16
0101
10101
// You are using Java
import java.util.*;
class main
{
public static void main(String args[])
{
Scanner t=new Scanner(System.in);
int n=t.nextInt(),a=0;
for(int i=0;i<n;i++)
{
if(i%2==0)
{
a=1;
}
else
{
a=0;
}
for(int j=0;j<=i;j++)
{
System.out.print(a+" ");
if(a==0)
{
a=1;
}
else
{
a=0;
}
}
System.out.print("\n");
}
}
}
OR
18.​ i) Hari is a civil engineer who is designing a fountain in square
shape with water sprinklers in the edges with n number of
steps. He needs to draw a sketch of the fountain in top view
with the step number at the edges of the square.

Write a program to help him in printing the pattern with n


AP C201.3 16
number of steps.

Sample Input
5
Sample Output
1 1
2 2
3 3
44
5
4 4
3 3
2 2
1​ 1

import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int rows=sc.nextInt();
int i,j,col=0;
col=(rows*2)-1;
for(i=1;i<=rows;i++)
{
for(j=1;j<=col;j++)
{
if(j==i)
{
System.out.print(i);
}
else if(j==col)
{
System.out.print(i);
}
else { System.out.print(" ");}
if (j<=col){
System.out.print(" ");}
}
col--;
System.out.println();
}
col+=2;
for(i=rows-1;i>=1;i--)
{
for(j=1;j<=col;j++)
{
if(j==i)
{
System.out.print(i);
}
else if(j==col)
{
System.out.print(i);
}
else { System.out.print(" ");}
if (j<=col){
System.out.print(" ");}
}
col++;
System.out.println();
}
sc.close();
}
}

19.​ i) A common problem in statistics is that of generating


frequency distribution of the given data. Assuming that the
data consists of n positive integers in the range 1 to 25, write
a java program that prints the number of times each integer
occurs in the data.
Sample Input
8
10 20 20 10 10 20 5 20
Sample Output
10 3
20 4
51

import java.util.Scanner; AP C201.3 16

public class FrequencyDistribution {


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

int[] frequency = new int[26];

int n = scanner.nextInt();

for (int i = 0; i < n; i++) {


int num = scanner.nextInt();
frequency[num]++;
}
for (int i = 1; i <= 25; i++) {
if (frequency[i] > 0) {
System.out.println(i + " " + frequency[i]);
}
}

scanner.close();
}
}

OR
20.​ i) Johnsy wants to create a matrix in which the elements are
formed differently. The elements are formed by adding the
values of their index positions. Write a program that obtains
the order of the matrices and creates a matrix by adding the
values of their index positions.
Sample Input
33
Sample Output
0​ 1​ 2​
1​ 2​ 3​
2​ 3​ 4​

// You are using Java


import java.util.*;
class main{
AP C201.3 16
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
int n=s.nextInt();
int m=s.nextInt();
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(i+j);
System.out.print("\t");
}
System.out.print("\n");
}

}
}

You might also like