0% found this document useful (0 votes)
18 views7 pages

Java Programming Assessment Key

This document contains two Java programming questions and answers as part of a Continuous Assessment Test. The first question involves writing a program to input numbers into an array, calculate the sum of the diagonal elements, and output even numbers from the array if the sum is even. The second question defines several classes - Student, Quiz1, and Quiz2 - to get and display student details like name, registration number, and quiz marks using getter and setter methods. The third part provides code samples with errors and asks test-takers to identify and rectify the errors and list the program outputs separately. Sample errors identified are incorrect data type declarations and removing the static keyword from a method parameter. Listed outputs are separated by
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

Java Programming Assessment Key

This document contains two Java programming questions and answers as part of a Continuous Assessment Test. The first question involves writing a program to input numbers into an array, calculate the sum of the diagonal elements, and output even numbers from the array if the sum is even. The second question defines several classes - Student, Quiz1, and Quiz2 - to get and display student details like name, registration number, and quiz marks using getter and setter methods. The third part provides code samples with errors and asks test-takers to identify and rectify the errors and list the program outputs separately. Sample errors identified are incorrect data type declarations and removing the static keyword from a method parameter. Listed outputs are separated by
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CSE1007 Java Programming – A1+TA1 Slot

Continuous Assessment Test (CAT) – I Answer Key

1. Write a Java program to get n numbers using Scanner class to store it in an array then find
the sum of diagonal numbers. If the sum of diagonal number value is even number then print
all even numbers from the array. [4+6 Marks] [CO1] [BL – L6]
Key:
import [Link].*;
Scanner class obj;
[Link]();
forming array
Sum=sum+array[i][i];
if(sum%2==0)
display all even number from array

Or
Write a Java program to get n numbers using command line arguments to store it in an array,
find and display the sum of each rows and each columns on the screen. [4+6 Marks] [CO1]
[BL – L6]
Command line arguments inputs, convert string to integer and forming array
Sum of rows
rsum=rsum+array[i][j];
show rsum
rsum=0 and repeat
Sum of columns
csum=csum+array[j][i];
show csum
csum=0 and repeat

2. Create a class Student with data members Name, Register_No and address. Create a class
Quiz1 which inherits from Student class and it contains Register_No and Quiz1_Mark. Create
a class QUIZ2 which inherits from Quiz1 class and it contains Register_No, Quiz2_Marks as
member and two member function called get() and put(). Write the appropriate code to
implement the above said code to get student details using get() and display the student
details using put() method through the Quiz2 class object. [2+3+5 Marks] [CO2] [BL – L6]
Answer:
class Student_Quiz
{
String Name,Register_No,address;
}
class Quiz1 extends Student_Quiz
{
String Register_No;
int Quiz1_Mark;
}
class QUIZ2 extends Quiz1
{
String Register_No;
int Quiz2_Mark;
void get(String n,String r,String a,int Q1,int Q2)
{
Name=n;
Register_No=r;
address=a;
Quiz1_Mark=Q1;
Quiz2_Mark=Q2;
}
void put()
{
[Link]("Student Name is "+ Name);
[Link]("Student Register_No "+Register_No);
[Link]("Student Address is "+address);
[Link]("Quiz1 Mark is "+Quiz1_Mark);
[Link]("Quiz2 Mark is "+Quiz2_Mark);
}
public static void main(String args[])
{
QUIZ2 obj=new QUIZ2();
[Link]("kumar","19BCE0001","SJT413 A21",8,9);
[Link]();
}
}
Or
Create a class Student with data members Name, Student_Code and email. Create a class
Internals which inherits from Student class and it contains one member CAT_Mark and one
member function show_internal_mark() is used to show internal mark of the student. Create a
class a External which inherits from Internal class and it contains one member as
External_Mark and one member function as show_marks(), which is used to display internal
and external marks of the student. Write the appropriate code to implement the above said
code to display Student Name, Student_Code, email and student marks only with help of
constructor of External class.[2+3+5 Marks] [CO2] [BL – L6]
Answer:
class Student_CAT1
{
String Name,Student_code,email;
}
class Internals extends Student_CAT1
{
int CAT_Mark;
void show_internal_mark()
{
[Link]("Internal CAT Mark is "+CAT_Mark);
}
}
class External extends Internals
{
int External_Mark;
External(String n,String c,String e,int internal,int external)
{
Name=n;
Student_code=c;
email=e;
CAT_Mark=internal;
External_Mark=external;
[Link]("Student Name is "+ Name);
[Link]("Student Code is "+Student_code);
[Link]("Student Email is "+email);
show_mark();
}
void show_mark()
{
show_internal_mark();
[Link]("External Mark is "+External_Mark);
}
public static void main(String args[])
{
External obj=new External("kumar","19BCE0001","kumar@[Link]",30,50);
}
}

3. If there are errors in the below Java program, rectify it then write down the outputs
separated by new line. [3+7 Marks] [CO2] [BL – L1]
Note: No need to rewrite the complete code again, just write only error rectified code and
outputs.

Two Errors:
```````````````
address="Vellore " + '632000'; address="Vellore" + "632000"
int room_no=[Link]+1; int room_no=no+1;

Output(Separated by new line):


``````````````````````````````````````
8
Vellore632000
Back Side
8
House no 8
Back Side
Room No 9
updated address is Vellore632000

Or
3 If there are errors in the below Java program, rectify it then write down the outputs
separated by new line. [2+8 Marks] [CO2] [BL – L1]
Note: No need to rewrite the complete code again, just write only error rectified code and
outputs.
Three Errors:
````````````````
int emp_id,int age;long emp_id; int age; //, and long
void employee(static int count) void employee(int count) //static
Output(Separated by new line):
``````````````````````````````````````
Four
Five
-5
One
Three
Two
1

Common questions

Powered by AI

Error management is demonstrated by identifying logical errors and syntax in Java code and providing the corrected version focusing on appropriate data types and right syntax construction. For instance, the concatenation issue in address was resolved by using double quotes for both strings. And confusion with data type declarations was corrected by separating them into appropriate statements, resolving runtime and compile-time issues .

In Java, static methods belong to the class rather than instances, critical for utility methods that should be accessible without an object. The document clarifies errors by removing 'static' from inappropriate contexts like method arguments, ensuring that the functions are only classified as static when applicable, e.g., global counters or other utilities shared across instances .

The polymorphic behavior is evident when displaying student marks through method overriding. The class 'External' extends 'Internals' and overrides the method to show marks. 'show_internal_mark()' is defined in 'Internals', and called within 'External' to show CAT_Mark, while 'show_marks()' in 'External' also presents External_Mark specifically. The use of the show_mark() method in the constructor demonstrates a dynamic method call that leverages inheritance .

In the described Java program, inheritance allows for hierarchical information structure. The Student class provides basic information shared by both Quiz1 and QUIZ2 subclasses. QUIZ1 adds specific information about the Quiz1_Mark, and QUIZ2 further extends it by adding Quiz2_Mark and relevant methods for data manipulation. This hierarchy efficiently manages shared and specific information across related classes .

Method overriding in the Java program enhances flexibility by allowing subclasses to offer specific implementations for methods defined in parent classes. In the context of displaying marks, 'External' overrides the method to show marks, enabling it to not just display internal marks but encapsulate broader functionality tailored to its needs without altering the parent class structure, promoting an extendable design .

Encapsulation is key to organizing student details within classes by managing data access through getter and setter methods. Fields like 'Register_No' and marks are kept private and controlled through methods like 'get()' and 'put()', which allow for controlled access and manipulation while safeguarding the integrity and security of the data .

Using command line inputs proves beneficial for scenarios needing batch processing, automation, or testing with various input sets without modifying the code. It allows passing different sets of numbers for computations like row and column sums, offering flexibility and easy replicability for different data analyses in efficient manners .

To address this, one would first use the Scanner class to input n numbers to form an array. Sum the diagonal numbers (i.e., array[i][i]) in a loop. After computing the sum, check if it is even; if true, display all even numbers from the array. This involves effectively using loops and conditional statements to handle array manipulation and logic flow .

Data member types and proper initialization critically influence class functionality including memory usage and output correctness. Typing issues such as mixing 'int' with 'long' can lead to errors if not managed properly. Initialization, like resetting sums before computations, prevents carrying over data leading to unexpected results, essential for predictable and correct program behavior .

Constructors are crucial in Java for initializing objects with relevant data right upon creation. In this context, constructors in the 'External' class initialize student data efficiently, ensuring all necessary information, including inherited attributes from 'Internals' such as CAT_Mark, are set at once, streamlining data handling while leveraging class hierarchy benefits .

You might also like