0% found this document useful (0 votes)
1 views15 pages

Java Programming Test 2 Q - A

Uploaded by

lomosek265
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)
1 views15 pages

Java Programming Test 2 Q - A

Uploaded by

lomosek265
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

Phoenix InfoTech

Java Programming Test


Marks: 70 marks

SECTION I (10 Marks)


Attempt any 10 Questions

Q1) The method Integer________ converts a string to an integer.

Answer: parseInt

Q2) A class in Java is defined using the keyword ________.

Answer: class

Q3) The [Link]() method converts a string to a ________.

Answer: double

Q4) The public access specifier provides the highest level of accessibility.
(True/False)

Answer: True

Q5) To compile a Java program, the command used is ________.

Answer: javac

Q6) The ________ operator is used for addition in Java.

Answer: +

Q7) Methods declared as private can be accessed outside the class. (True/False)

Answer: False

Q8) In Java, ________ represents the starting point of program execution.

Answer: main method

Q9) The parseInt() method converts a string to an integer. (True/False)

Answer: True
Phoenix InfoTech
Java Programming Test
Marks: 70 marks

Q10) Default access specifier allows access within the same package only
(True/False)

Answer: True

Q11) The default value of an uninitialized int variable is ________.

Answer: 0

Q12) Command-line arguments are stored in the ________ array.

Answer: args
Phoenix InfoTech
Java Programming Test
Marks: 70 marks

SECTION II (10 Marks)


Attempt any 5 Questions

Q1) What is the use of the main method in Java?

Answer:

The main method is the entry point for Java program execution.

Q2) Define a class in Java.

Answer:

A class in Java is a blueprint for creating objects, containing data members


and methods.

Q3) What does protected access specifier mean?

Answer:

It allows access to members within the same package and in subclasses.

Q4) How do you accept command-line arguments in Java?

Answer:

Command-line arguments are passed as strings in the args[] array of the


main method.

Q5) What is the difference between public and private?

Answer:

public allows access from any class, while private restricts access to within
the same class.
Phoenix InfoTech
Java Programming Test
Marks: 70 marks

Q6) What are access specifiers?

Answer:

They define the visibility or scope of class members like public, private,
protected, and default.

Q7) How do you calculate the average of three numbers? What is the use of
[Link]()?

Answer:

Add the numbers and divide by 3: (a + b + c) / 3.

It converts a string into a double.


Phoenix InfoTech
Java Programming Test
Marks: 70 marks

SECTION III (25 Marks)


Attempt any 5 Questions (Programs)

Q1) Explain the types of access specifiers in Java with examples.

Answer:

In Java, access specifiers (also known as visibility modifiers) define the scope of
classes, methods, and variables. There are four types of access specifiers:

1. Public

 The public access specifier allows access from any class, regardless
of the package.
 It achieves the highest level of accessibility.

2. Private

 The private access specifier restricts access to within the same class
only.
 It provides the lowest level of accessibility.
 Members declared as private cannot be accessed outside the class.

3. Protected

 The protected access specifier allows access within the same package
and to subclasses in different packages.

4. Default (No Modifier)

 When no access specifier is defined, it is considered as default access.


 Default access allows access within the same package only.
Phoenix InfoTech
Java Programming Test
Marks: 70 marks

Q2) What is the difference between class and object in Java?

Answer:

Class Object
1. A class is a blueprint or template 1. An object is an instance of a
for creating objects. class.

2. Defines properties (data 2. Represents an entity that has


members) and behaviours state and behaviour defined by
(methods). the class.

3. No memory is allocated until 3. Memory is allocated when an


objects are created. object is instantiated.

4. Defines common properties 4. Each object has its unique set of


shared by objects. property values.

5. Example: 5. Example:

class Car Car myCar = new Car();

int speed;

void drive()

}
Phoenix InfoTech
Java Programming Test
Marks: 70 marks

Q3) Program to Calculate Total and Average Marks of 3 Subjects.

Answer:

import [Link].*;

class Student {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter marks of Science, Maths, and English: ");

int sci = [Link](), maths = [Link](), eng = [Link]();

int total = sci + maths + eng;

double average = total / 3;

[Link]("Total: " + total + ", Average: " + average);

Q4) Explain the concept of command-line arguments in Java with an example.

Answer:

Command-line arguments are inputs passed to the program at runtime using the
terminal or command prompt. They are stored in the args[] array of the main()
method.

Example:

class CommandLineDemo {

public static void main(String args[]) {

[Link]("Argument 1: " + args[0]);


Phoenix InfoTech
Java Programming Test
Marks: 70 marks
[Link]("Argument 2: " + args[1]);

Q5) Write a program to accept two numbers from the user and print their
summation.

Answer:

import [Link];

class SumOfNumbers {

public static void main(String[] args) {

int num1, num2, sum;

Scanner sc = new Scanner([Link]);

[Link]("Enter First Number: ");

num1 = [Link]();

[Link]("Enter Second Number: ");

num2 = [Link]();

sum = num1 + num2;

[Link]("Summation is: " + sum);

}
Phoenix InfoTech
Java Programming Test
Marks: 70 marks
Q6) Create a class Student with roll number, name, and marks of three subjects.
Write a method to calculate the total and average of marks.

Answer:

class Student {

int rollNo, marks1, marks2, marks3;

void getDetails(int r, int m1, int m2, int m3)

rollNo = r;

marks1 = m1;

marks2 = m2;

marks3 = m3;

void calculate()

int total = marks1 + marks2 + marks3;

double average = total / 3.0;

[Link]("Roll No: " + rollNo);

[Link]("Total Marks: " + total);

[Link]("Average Marks: " + average);

}
Phoenix InfoTech
Java Programming Test
Marks: 70 marks
public static void main(String[] args)

Student s1 = new Student();

[Link](1, 85, 90, 95);

[Link]();

Q7) Find Errors in Code and Correct:

println("Welcome to Java");

Answer:

Error: [Link] missing

Corrected Code:

[Link]("Welcome to Java ");


Phoenix InfoTech
Java Programming Test
Marks: 70 marks

SECTION IV (25 Marks)


Attempt any 5 Questions

Q1) Write a program to accept a radius as a command-line argument and calculate


the area of the circle.

Answer:

class CircleArea

public static void main(String[] args)

double radius = [Link](args[0]);

double area = 3.14 * radius * radius;

[Link]("Area of Circle: " + area);

Q2) Write a program to demonstrate the use of [Link]() for converting


strings to integers.

Answer:

class ParseIntDemo {

public static void main(String[] args) {

int num1 = [Link](args[0]);

int num2 = [Link](args[1]);


Phoenix InfoTech
Java Programming Test
Marks: 70 marks
int sum = num1 + num2;

[Link]("Sum is: " + sum);

Q3) Write a program to calculate the volume of a 3D object using a class with data
members and methods.

Answer:

base = float(input("Enter base of the triangle: "))

height = float(input("Enter height of the triangle: "))

area = 0.5 * base * height

print("Area of the triangle:", area)

Q4) Write a program that uses a method to calculate and return the factorial of a
number.

Answer:

class Main

void calculateFactorial()

int fact, n;

fact = 1;

n = 5;

for (int i = 1; i <= n; i++)


Phoenix InfoTech
Java Programming Test
Marks: 70 marks
{

fact *= i;

[Link]("Factorial is: " + fact);

public static void main(String[] args)

Main obj = new Main();

[Link]();

Q5) Find Errors in Code and Correct:

tot sci + maths + eng;

[Link]("Total = "+nm);

Answer:

Error: = sign Missing

Corrected Code:

tot = sci + maths + eng;

[Link]("Total = " + tot);


Phoenix InfoTech
Java Programming Test
Marks: 70 marks

Q6) Find Errors in Code and Correct:

class areacir {

Double radius, area;

void cal {

radius = 10.0;

area = 3.14 * radius * radius;

Answer:

Error: No print Statement

Corrected Code:

class areacir {

double radius, area;

void cal() {

radius = 10.0;

area = 3.14 * radius * radius;

[Link]("Area is: " + area);

}
Phoenix InfoTech
Java Programming Test
Marks: 70 marks

Q7) Find Errors in Code and Correct:

[Link]("Summation is "+);

Answer:

Error: Variable Missing

Corrected Code:

[Link]("Summation is: " + sum);

You might also like