COM 211 (Programming Language Using Java II)
COM 211 (Programming Language Using Java II)
COM 211 (Programming Language Using Java II)
Write a Java program that would print "How are you doing"
So, this is how the Number 1 question would look like. They will ask us to read the
program/code and write out the output. The Output is How are you doing
1b. List and explain three modes in which Java operates form.
i. Development Mode: This mode is used during the development of Java applications. In this
mode, Java source code is written using a text editor or an integrated development environment
(IDE) such as Eclipse, IntelliJ IDEA, or NetBeans.
ii. Deployment Mode: This mode is used when Java applications are ready for deployment to
production environments. In this mode, the Java bytecode generated during development is
executed on a JVM installed on a production server.
iii. Applet Mode: Applet mode is a legacy mode of Java that is rarely used today. In this mode,
Java applets, which are small Java applications embedded in web pages, are executed on a
client-side Java Virtual Machine (JVM) within a web browser.
2a. List and explain ten(10) features of the Java programming language
ii. Platform-independent: Java code is compiled into platform-independent bytecode that can be
executed on any Java Virtual Machine (JVM), making Java a platform-independent language.
iii. Garbage collection: Java has built-in automatic memory management through garbage
collection, which frees developers from manual memory management.
iv. Exception handling: Java has a robust exception-handling mechanism that allows developers
to handle runtime errors gracefully.
v. Multi-threading: Java supports multi-threading, allowing for the concurrent execution of tasks
within a single application.
vi. Rich standard library: Java comes with a vast standard library that provides a wide range of
pre-built classes and APIs for common tasks such as I/O, networking, collections, GUI
(Graphical User Interface), and more.
vii. Strong typing: Java is a statically typed language, which means that data types of variables
must be explicitly defined at compile-time.
viii. Security: Java has built-in security features that make it a preferred choice for building
secure applications.
ix. Portability: Java's platform independence makes it highly portable. Java code can be written
once and run on multiple platforms without modification, making it an ideal choice for building
cross-platform applications.
x. Community support: Java has a large and active community of developers, users, and
contributors who provide continuous support, regular updates, and a rich ecosystem of libraries,
frameworks, and tools.
2b. List four levels of scope that is required to explicitly type a variable
Ans= The four levels of scope in Java that are required to explicitly type a variable are:
i. Class-level scope: In Java, variables declared at the class level are called instance variables.
ii. Method-level scope: Variables declared within a method or a constructor are called local
variables.
iii. Block-level scope: Variables declared within a block of code, such as a loop or an if
statement, are called block-scoped variables.
iv. Package-level scope: Variables declared at the package level, outside of any class or
method, are called package-level variables.
i. Classes and Objects: In Java, a class is a blueprint for creating objects. It defines the
attributes (data members) and behaviors (methods) of objects that can be created from it. An
object is an instance of a class, and it represents a specific occurrence of a class with its own
state and behavior.
ii. Encapsulation: Encapsulation is the process of hiding the internal details of an object and
exposing only what is necessary.
iii. Inheritance: Inheritance is a mechanism by which one class can inherit properties and
methods from another class.
iv. Polymorphism: Polymorphism allows objects of different classes to be treated as if they are of
the same type.
Ans= Developing applications using Java typically involves the following three primary steps:
i. Writing Java Code: The first step is to write the Java code for your application using a text
editor or an integrated development environment (IDE) such as Eclipse, IntelliJ IDEA, or
NetBeans. Java code is written in plain text files with the ".java" extension. You need to define
classes, methods, variables, and other elements according to the logic and requirements of your
application.
ii. Compiling Java Code: Once you have written the Java code, the next step is to compile it into
bytecode using the Java Compiler. The Java Compiler translates the human-readable Java
source code into machine-readable bytecode that can be executed by the Java Virtual Machine
(JVM).
iii. Running Java Code: After compiling the Java code into bytecode, the next step is to run the
Java application using the Java Virtual Machine (JVM).
4a. Write an application that generates random addition problems which will also provide some
kind of feedback and scoring system in Java.
4b. List four rules used in naming variables
Ans = i. Start with a letter or underscore: Variable names must begin with a letter (a-z or A-Z) or
an underscore (_) character. They cannot start with a digit (0-9) or any other special character.
ii. Use only letters, digits, or underscores: Variable names can only contain letters (a-z or A-Z),
digits (0-9), or underscores (_). They cannot contain spaces, punctuation marks, or other
special characters.
iii. No reserved words: Variable names cannot be the same as Java reserved words, such as int,
boolean, if, while, etc. Java has a set of reserved words that have special meanings in the
language and cannot be used as variable names.
iv. Follow camelCase convention: It is a common convention in Java to use camelCase for
naming variables. This means that variable names should start with a lowercase letter and each
subsequent word should start with an uppercase letter, without spaces or underscores. For
example, myVariable, count, firstName, etc.
5a. With the aid of if/then/else type statements and the corresponding select case statement,
write a branching statement that will categorize the following people Age 5 = five year old, Age
13 to 19 = Teenager, Age 20 to 35, 50, 60-65 = Special Adult, Age above 65 = Senior citizen,
Others = Everyone else.
Ans = The five possible categories and the values they may return are:
6a. With the aid of a Do/While loops and a corresponding Do/Until loops write a Java program
that will list all numbers between 1 and 1000.
Ans = List of various operators in Java, along with their level of precedence (from highest to
lowest):
7a. Write a Java program that will ask the user to input a password. if it is correct, a message
box should appears to validate the user. If it is incorrect, access should be denied.
7b. List four Java data type and their corresponding suffix
i. int: Represents integer values, which are whole numbers without decimal points. No suffix is
required.
ii. double: Represents double-precision floating-point values, which are numbers with decimal
points and a higher range and precision compared to float. No suffix is required.
iii. boolean: Represents boolean values, which can have two states: true or false. No suffix is
required.
iv. long: Represents long integer values, which are larger integer values compared to int. It
requires a suffix of L or l to indicate that the value is a long type.
Ans = Inheritance is a key feature of object-oriented programming (OOP) in Java, which allows
a class to inherit properties and methods from another class. In other words, inheritance
enables the creation of a new class (called a subclass or derived class) that inherits the
properties and methods of an existing class (called a superclass or base class).
i. Code reusability: Inheritance allows the subclass to inherit the members of the superclass,
reducing code duplication and promoting code reuse.
ii. Method overriding: Subclass can override the methods of the superclass to provide its own
implementation.
iii. Polymorphism: Subclass objects can be treated as superclass objects, allowing for
polymorphic behavior.
iv. Modularity: Inheritance promotes code organization and modularity, as related classes can be
grouped into a class hierarchy based on their inheritance relationships.
v. Code extensibility: New classes can be easily added to the class hierarchy by inheriting from
existing classes.
Here's an example of how inheritance works in Java:
Ans = Event handling in Java is the process of responding to events, such as user actions or
system events, that occur during the execution of a Java program. Events can be triggered by
various actions, such as button clicks, mouse movements, keyboard input, timer expiration, and
window events, among others.
i. Define an event listener class: Create a class that implements a specific event listener
interface, such as ActionListener for handling button clicks, MouseListener for handling
mouse events, or KeyListener for handling keyboard events. This class will contain the code
that responds to the events.
ii. Register the event listener: Attach an instance of the event listener class to the corresponding
event source using the appropriate registration method, such as addActionListener() for
buttons, addMouseListener() for components that listen for mouse events, or
addKeyListener() for components that listen for keyboard events.
iii. Implement the event handling code: Override the appropriate methods in the event listener
class to define the behavior that should occur when the events are triggered. For example, in
the actionPerformed() method of an ActionListener, you can specify what should happen when
a button is clicked.