0% found this document useful (0 votes)
13 views4 pages

Java Programming Exam Solutions 2019

The document contains solutions to a Java programming exam for BCA Semester V, covering topics such as command-line arguments, branching statements, class structures, and exception handling. It includes programming examples, explanations of Java concepts like inheritance and polymorphism, and details on layout managers. The document is divided into three parts with varying question formats and marks allocation.

Uploaded by

kevinsaju313
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)
13 views4 pages

Java Programming Exam Solutions 2019

The document contains solutions to a Java programming exam for BCA Semester V, covering topics such as command-line arguments, branching statements, class structures, and exception handling. It includes programming examples, explanations of Java concepts like inheritance and polymorphism, and details on layout managers. The document is divided into three parts with varying question formats and marks allocation.

Uploaded by

kevinsaju313
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

BCA Semester V - Java Programming Using Linux (2019 Question Paper Solutions)

PART A - (Answer any ten questions, each 2 marks)

1. Command-line arguments:
Values passed to main when a Java program starts: public static void main(String[] args).

2. Branching statements:
if, if-else, if-else if ladder, switch.

3. Class:
Blueprint/template for objects; defines fields and methods.

4. Accessing members:
Create object; use dot operator ([Link]()).

5. Protected keyword:
Accessible within same package and subclasses.

6. Array:
Fixed-size indexed collection of same type elements.

7. Newborn thread:
Thread object created but not started yet.

8. Exception handling:
Catch runtime errors to avoid program termination.

9. Swing vs AWT:
Swing = lightweight, platform-independent; AWT = heavyweight, dependent.

10. JPanel:
Swing container used to group components.

11. Web page sections:


Header, navigation, content, sidebar, footer.
12. JDBC ResultSet:
Holds query results; allows cursor-based access to rows.

------------------------------------------------------------

PART B - (Answer any six, each 5 marks)

13. Java Data Types:


Primitive (byte, short, int, long, float, double, char, boolean).
Non-primitive (String, Arrays, Classes).

14. Program: Prime Numbers between two limits


public class Prime {
static boolean isPrime(int n){
if(n<=1)return false;
for(int i=2;i<=n/2;i++) if(n%i==0) return false;
return true;
}
public static void main(String[] a){
int low=10, high=50;
for(int i=low;i<=high;i++)
if(isPrime(i)) [Link](i);
}
}

15. Multilevel Inheritance:


class A{ void showA(){[Link]("A");}}
class B extends A{ void showB(){[Link]("B");}}
class C extends B{ void showC(){[Link]("C");}}

16. Dynamic Method Dispatch:


Runtime polymorphism-method call resolved by object type, not reference type.

17. String vs StringBuffer:


String: immutable; StringBuffer: mutable and synchronized.

18. Built-in Exceptions:


Checked: IOException, SQLException.
Unchecked: ArithmeticException, NullPointerException.

19. Swing Architecture:


MVC model - Model (data), View (UI), Controller (event handling).

20. Applet for Sum of Two Numbers:


import [Link].*;
import [Link].*;
import [Link].*;
public class SumApplet extends Applet implements ActionListener{
TextField t1,t2; Label l; Button b;
public void init(){
t1=new TextField(5); t2=new TextField(5);
b=new Button("Add"); l=new Label("Result:");
[Link](this);
add(t1);add(t2);add(b);add(l);
}
public void actionPerformed(ActionEvent e){
int x=[Link]([Link]());
int y=[Link]([Link]());
[Link]("Result: "+(x+y));
}
}

------------------------------------------------------------

PART C - (Answer any two, each 15 marks)

22. OOP vs POP and Concepts:


POP - focuses on functions, data is global.
OOP - focuses on objects, encapsulation of data and methods.
Concepts: Class, Object, Inheritance, Encapsulation, Abstraction, Polymorphism.

23. Constructor Overloading:


Multiple constructors with different parameter lists.
Example:
class Student{
Student(){}
Student(String n){}
Student(String n,int a){}
}

24. User-defined Packages:


Steps:
1. Declare with 'package name;'
2. Save in folder matching package.
3. Compile and import.
Example:
package mypack;
public class Add{public int sum(int a,int b){return a+b;}}
import [Link];
class Test{public static void main(String[]a){[Link](new Add().sum(5,3));}}

25. Layout Managers:


- FlowLayout: left-to-right.
- BorderLayout: N,S,E,W,Center regions.
- GridLayout: equal cell grid.
- CardLayout: one panel at a time.
- BoxLayout: vertical/horizontal stacking.

Common questions

Powered by AI

Multilevel inheritance in Java occurs when a class is derived from a class that is already derived from another class, forming a hierarchy. This allows a child class to inherit properties and behaviors from multiple classes up the hierarchy, thus promoting code reuse and a clear logical structure. Use cases include building layers of functionality where each level adds specificity or behavior, such as in UI frameworks or domain models .

Layout managers in Java are responsible for arranging components within a container. Examples include FlowLayout, which arranges components left-to-right across the container, BorderLayout, which divides the container into five regions (North, South, East, West, Center), and GridLayout, which arranges components in a rectangular grid. BoxLayout stacks components vertically or horizontally, while CardLayout allows only one component to be visible at a time, which is useful for switching views. These differing approaches give developers flexibility in designing complex layouts .

In Java, Strings are immutable, meaning once created, they cannot be changed. This immutability ensures thread safety as the objects cannot be modified after creation, which prevents concurrency issues. However, this can be inefficient when dealing with a large number of modifications, as new objects must be created. StringBuffer, on the other hand, is mutable and synchronized, meaning it can be modified directly, thus offering better performance for string manipulations involving multiple changes. Its synchronization also makes it thread-safe, suitable for multi-threaded environments .

A JDBC ResultSet represents a database query result set and provides methods for navigating through data returned by a query. It allows the cursor-based navigation of rows and retrieval of column values. As a result, programmers can traverse the results sequentially, making updates, or retrieving specific pieces of data, which simplifies interaction with the data source and is pivotal in managing database operations in Java applications .

The protected keyword in Java restricts access such that members are accessible only within the same package and to subclasses. This means that a member with protected access cannot be accessed by classes in different packages unless they inherit from the declaring class, which facilitates controlled inheritance while maintaining encapsulation. This promotes the use of inheritance while still protecting class internals to some extent .

Dynamic method dispatch is a mechanism by which a call to an overridden method is resolved at runtime rather than at compile-time. This is accomplished by the JVM by using the actual object at runtime to determine which method to call, rather than the reference type. This allows for runtime polymorphism, enabling developers to invoke methods on objects without needing to know their specific classes, thereby supporting flexible and scalable code architectures .

Java handles command-line arguments by allowing them to be passed to the main method as an array of strings. This feature is useful for passing configuration parameters and input values directly when starting a Java application, enabling dynamic behavior without altering the source code. It facilitates customizing program runs through command inputs .

Java's exception handling mechanism enables developers to manage runtime errors gracefully, preventing abrupt program termination and maintaining application stability. The key components include try-catch blocks, which allow for capturing exceptions and handling them appropriately, and finally blocks, which execute code regardless of exception occurrences. This structure encourages proactive error management, logging, and necessary cleanup operations, thus enhancing application robustness .

Swing components are platform-independent and lightweight, meaning they do not rely directly on the native components, allowing for a consistent look across platforms. In contrast, AWT components are heavyweight and dependent on the platform's GUI components, which can lead to inconsistencies across different operating systems. Furthermore, Swing provides more sophisticated features and a richer set of GUI components when compared to AWT .

Applets are special Java programs that are run in the context of a web browser, offering interactive features through GUI components. Thanks to their integration with HTML pages and sandboxing, they provide a seamless way to enhance web pages with dynamic content. However, applets are limited by security restrictions, dependence on browser and plugin versions, and reduced performance compared to standalone applications. The rise of modern web technologies has further diminished their utility in comparison to more capable standalone Java applications or HTML5-based solutions .

You might also like