Fundamentals of Programming(JAVA)
Fundamentals of Java Programming
Q) Distinguish between variables and keywords.
Variables
Definition:
Variables are named storage locations used to hold data (like numbers, text, etc.) that your program can manipulate.
Created by the programmer.
Can store different types of data (int, float, String, etc.).
The name must follow Java's naming rules (e.g., can't start with a number, no special characters except _ and $, can't be a keyword).
The value of a variable can change during program execution.
Ex: int var=25;
Char ch;
Keywords
Definition:
Keywords are reserved words in Java that have a specific meaning to the compiler and cannot be used as identifiers (like variable names or class names).
Predefined and fixed in the language.
Used to define the structure and syntax of Java programs.
Cannot be redefined or used for any other purpose.
Ex: static, class, if, public etc
Q) Distinguish between Logical and Relational operators
Relational Operators
Definition:
Relational operators are used to compare two values. They return a boolean result: true or false.
Typically used in conditions (like in if, while, etc.) to compare numbers or characters.
Logical Operators
Definition:
Logical operators are used to combine multiple boolean expressions or conditions.
Use Case:
Often used to create complex conditions by combining results of relational expressions.
Q) Write step-by-step procedure to write, compile and run a java program in blueJ
Open BlueJ
Launch the BlueJ application.
Create a New Project
Go to Project > New Project, name it, and click Create.
Create a Class
Click New Class, give it a name (e.g., HelloWorld), and click OK.
Write Code
Double-click the class icon and replace the default code with:
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello, world!");
}
}
Compile the Program
Click Compile. If there are no errors, it says: “Class compiled - no syntax errors.”
Run the Program
Right-click the class icon → select void main(String[] args) → click OK.
View Output
Output appears in the Terminal Window.
----------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
Control Structures
Q) Describe the use of Control Structures
Control statements manage the flow of execution in a Java program. They determine how, when, and whether blocks of code are executed. Java has three main categories of control statements:
1. Decision-Making Statements
Used to execute certain blocks of code based on specific conditions.
Example; if, if-else, else if ladder
2. Looping (Iteration) Statements
Used to repeat a block of code multiple times:
example: for, while
3. Branching Statements
Used to alter the normal flow of control.
example: break, continue, return.
Q)Why do you use the selection statements in programs.
Purpose of Selection statements in programs:
To Make Decisions
Selection statements allow a program to choose different actions based on different conditions.-
To Control the Flow of Execution
They help direct the flow of the program depending on whether certain conditions are true or false. -
To Handle Multiple Scenarios
Programs often need to behave differently in different situations (e.g., user input, values, or states). -
To Increase Program Flexibility
By using selection, the program becomes dynamic and can respond to real-time data or user input. -
To Avoid Unnecessary Execution
They prevent some parts of the code from running unless a specific condition is met. -
To Improve Readability and Maintainability
Selection logic makes it easier to understand how different outcomes are handled. -
To Implement Logic-Based Functionality
Features like login systems, game rules, calculators, etc., all rely heavily on conditional decisions.
Q) What is the difference between if and if-else?
ifStatement:-
Executes a block of code only if a specified condition is true.
-
If the condition is false, the program simply skips the
ifblock and continues. -
No alternative action is defined for the false condition.
-
-
if-elseStatement: -
Provides two possible paths: one if the condition is true, another if the condition is false.
-
Executes one block when the condition is true, and a different block when it is false.
-
Ensures that exactly one of the two code blocks runs.
| Feature | if | if-else |
|---|---|---|
| Condition True | Executes the code | Executes the if block |
| Condition False | Does nothing | Executes the else block |
Q)What is the use of If...Else if Ladder?
Multiple Choices Handling
-
When a program needs to choose one option out of several, based on different conditions.
Example: assigning grades, checking age categories, or deciding ticket prices.
Improves Readability & Structure
-
Instead of writing many separate
ifstatements, the ladder makes it clear that only one branch should execute.
Efficient Execution
-
Conditions are checked in order; once one is true, the rest are skipped. This avoids unnecessary checks.
Default Case Handling
-
The final
elsegives a "fall-back" action if none of the conditions are true.
Q) How are Iterative statements different from Selective Statements?
Comments
Post a Comment