0% found this document useful (0 votes)
24 views14 pages

Computer Question Paper 2023 Solutions

The document discusses code snippets in Java for various programming concepts like classes, methods, arrays, loops, conditionals, etc. It includes code to: 1) Define a Student class with methods to accept details, allocate stream based on marks, and print details. 2) Implement bubble sort on a character array. 3) Check if a number is a "lead number" by summing its even and odd digits. 4) Find frequency of digits, letters, and special characters in a string. 5) Perform linear search on a double array. 6) Calculate sum of 1-digit and 2-digit numbers in an integer array.

Uploaded by

arun
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)
24 views14 pages

Computer Question Paper 2023 Solutions

The document discusses code snippets in Java for various programming concepts like classes, methods, arrays, loops, conditionals, etc. It includes code to: 1) Define a Student class with methods to accept details, allocate stream based on marks, and print details. 2) Implement bubble sort on a character array. 3) Check if a number is a "lead number" by summing its even and odd digits. 4) Find frequency of digits, letters, and special characters in a string. 5) Perform linear search on a double array. 6) Calculate sum of 1-digit and 2-digit numbers in an integer array.

Uploaded by

arun
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/ 14

Am

pl
ify
Le
ar
ni
ng
Am
pl
ify
Le
ar
ni
ng
Am
pl
ify
Le
ar
ni
ng
Am
pl
ify
Le
ar
ni
ng
Am
pl
ify
Le
ar
ni
ng
Am
pl
ify
Le
ar
ni
ng
Am
pl
ify
Le
ar
ni
ng
import java.util.Scanner;

class Student
{
String name;
int age;
int mks;
String stream;

void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
name = sc.nextLine();

g
System.out.println("Enter your age");
age = sc.nextInt();

n
System.out.println("Enter your marks");
mks = sc.nextInt();
}

ni
void allocation()
{
if(mks>=300)
stream = "Science and Computer";
else if(mks>=200 && mks<300)
ar
Le
stream = "Commerce and Computer";
else if (mks>=75 && mks<200)
stream = "Arts and Animation";
else if(mks<75)
stream = "Try Again";
}
ify

void print()
{
System.out.println("Name : "+ name);
pl

System.out.println("Age : "+ age);


System.out.println("Marks : "+ mks);
System.out.println("Stream : "+ stream);
Am

public static void main(String[] args)


{
Student obj = new Student();
obj.accept();
obj.allocation();
obj.print();
}
}
import java.util.Scanner;

class BubbleSort
{
public static void main(String[] args)
{
//Character Array lena tha
char a[] = new char[10];

g
Scanner sc = new Scanner(System.in);

n
System.out.println("Enter 10 characters");

ni
for(int i=0; i<a.length; i++)
a[i] = sc.nextLine().charAt(0); //char input

//Print the Original Array


System.out.println("ORIGINAL ARRAY");
for(int i=0; i<a.length; i++)
System.out.print(a[i] + ", ");
ar
Le
int n=a.length;
//Bubble Sort
for(int i=0; i<n-1; i++)
{
ify

for(int j=0; j<n-i-1; j++)


{
if(a[j]>a[j+1])
{
pl

char t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
Am

}
}

//Print the Sorted Array


System.out.println("\nSORTED ARRAY");
for(int i=0; i<a.length; i++)
System.out.print(a[i] + ", ");
}
}
Am
pl
ify
Le
ar
ni
ng
class Overload
{
void print()
{
for(int i=1; i<=5; i++)
{
for (int j=1; j<=4; j++)
{
System.out.print(i + " ");
}
System.out.println();
}

g
}

n
void print(int n)
{
int sum_even = 0, sum_odd = 0;

ni
int temp = n; //to save the original no.

while(temp!=0)
{
int d = temp%10; ar
Le
if(d%2 == 0) //even condition
sum_even += d;
else // for odd digits
sum_odd += d;

temp = temp/10;
ify

if(sum_even == sum_odd)
System.out.println(n+" is a lead number");
pl

else
System.out.println(n+" is not a lead number");
}
Am

public static void main(String[] args)


{
Overload obj = new Overload();
obj.print();
obj.print(3669);
}
}
g
import java.util.Scanner;

n
class Frequency
{
public static void main(String[] args)

ni
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String");
String s = sc.nextLine();

int d=0, a=0, sp=0; //counters


ar
Le
for(int i=0; i<s.length(); i++)
{
char c = s.charAt(i);
if(Character.isDigit(c))
d++;
ify

else if(Character.isLetter(c))
a++;
else
sp++;
pl

System.out.println("Number of digits - "+d);


Am

System.out.println("Number of alphabets - "+a);


System.out.println("Number of Special characters - "+sp);
}
}
import java.util.Scanner;

class LinearSearch

g
{
public static void main(String[] args)

n
{
double a[] = new double[20];

ni
Scanner sc = new Scanner(System.in);

System.out.println("Enter 20 values in Array");


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

}
a[i] = sc.nextDouble(); ar
Le
System.out.println("Enter the value to be searched?");
double x = sc.nextDouble();

//Linear Search
int pos = -1; //position of found element
ify

for(int i=0; i<a.length; i++)


{
if(a[i] == x)
{
pl

pos = i;
break;
}
Am

if(pos != -1)
System.out.println("Element is found at "+pos);
else
System.out.println("Element is not found.");
}
}
g
import java.util.Scanner;

n
class Sum_1_2_Digit
{
public static void main(String[] args)

ni
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter 10 values into the Array");
int a[] = new int[10];
for(int i=0; i<10; i++)
a[i] = sc.nextInt();
ar
Le
int sum1 = 0, sum2 = 0; //sum variables

for(int i=0; i<a.length; i++)


{
//I assumed all no. to be positive
ify

if(a[i]>=0 && a[i]<=9)


sum1 += a[i];
else if(a[i]>=10 && a[i]<=99)
sum2 += a[i];
pl

System.out.println("Sum of one digit numbers: "+sum1);


Am

System.out.println("Sum of two digit numbers: "+sum2);


}
}

You might also like