0% found this document useful (0 votes)
2 views9 pages

Notebook Work - Part I - Fundamentals of Java Programming

The document provides an overview of fundamental concepts in Java programming, including definitions of computer programs, bytecode, JVM, IDEs, and the structure of Java code. It explains the importance of variables, methods, control flow structures like if-else and switch statements, and the use of comments and packages. Additionally, it covers primitive data types, variable declaration rules, and examples of Java syntax and control flow logic.

Uploaded by

ashajit7
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)
2 views9 pages

Notebook Work - Part I - Fundamentals of Java Programming

The document provides an overview of fundamental concepts in Java programming, including definitions of computer programs, bytecode, JVM, IDEs, and the structure of Java code. It explains the importance of variables, methods, control flow structures like if-else and switch statements, and the use of comments and packages. Additionally, it covers primitive data types, variable declaration rules, and examples of Java syntax and control flow logic.

Uploaded by

ashajit7
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
You are on page 1/ 9

Fundamentals of Java Programming

Q1. What is a computer program ?


Ans. A computer program is a sequence of instructions given to the computer in order
to accomplish a specific task.

Q2. What is a byte code ? (Board Exam Term II Q8 – 1 mark)


Ans. A Java compiler instead of translating Java code to machine language code, translates it
into Java Bytecode - a highly optimized set of instructions also called a Java class file.

Q3. What is JVM? Or What makes Java programs platform independent and portable ?
(Board Exam Term II Q9 – 1 mark + Sample Question paper 2022-23 Q4.iv)
Ans. When the bytecode (also called a Java class file) is to be run on a computer, a Java
interpreter, called the Java Virtual Machine (JVM), translates the bytecode into machine code
and then executes it.
The advantage of such an approach is that once a programmer has compiled a Java program into
bytecode, it can be run on any platform (say Windows, Linux, or Mac) as long as it has a JVM
running on it.
This makes Java programs platform independent and highly portable.

(Note: a computer only understands its own language called “machine language”)

Q4. What is Java IDE ?


Ans. To write a Java program, you will need a Text Editor (for writing the code)and a Java
compiler (for compiling the code into bytecode).
There are a wide variety of Java Integrated Development Environments (IDEs) available in
the market that come equipped with a text editor and a Java compiler thus simplifying writing,
compiling and executing Java programs.
Q. What do you mean by Projects in Java ?
Ans. A project is a group of source files and the settings with which you build, run, and debug those source
files. In the IDE, all Java development has to take place within a project.

Q. What are Comments in Java ? Specify the two ways to write comments in Java.
Ans. All the grey parts are comments. Comments are used in code by programmers to document their programs
– to provide explanatory notes to other people who read the code. This is especially useful in the real world,
where large programs are written by one programmer and maintained by other programmers.
You can write comments in a Java program in the following two ways:
- Beginning a comment line with two consecutive forward slashes (//)
- Writing the comment between the symbols /* and */
The former method is used for single line comments while the latter is generally preferred for multiple line
comments

Q.What do you mean by package in Java ?


Ans. A package in java is a group of related classes. All classes in a package can share their data and code

Q.What do you mean by methods in Java ?


Ans. A method is a group of statements written to perform a specific task. The method body is enclosed within
a pair of curly braces and contains the statements that the method will execute.

Q.What is the use of Main Method in Java ?


Ans. Main is a special method that every Java application must have. When you run a program, the statements
in the main method are the first to be executed. The main() method is the entry point of each and every Java
program

Q.What Is System.out.println in Java?


Ans.To define System.out.println() in Java, it is a simple statement that prints any argument you pass and adds a
new line after it.

 println() is responsible for printing the argument and printing a new line
 System.out refers to the standard output stream

Q. How do the following two codes differ :


a. System.out.println(“Hello world”);
b. System.out.print(“Hello world”); (Term II – Board Exam – 2 marks)
Ans. The System.out.println(“HelloWorld”); statement prints the line “Hello World” on the screen followed by
a newline causing the subsequent line to be printed on the next line.
System.out.print(“Hello World”);
In This time a newline is not printed after the “Hello World” causing the subsequent line to be printed on the
same line.

(**All Java statements must end with a semicolon)


Variables
Q.What is a Variable ?
Ans. To store the program data we will use variables. A variable is a placeholder for data that
can change its value during program execution. Technically, a variable is the name for a storage
location in the computer's internal memory. The value of the variable is the contents at that
location.

Q.How do we declare a Variabla in Java ?


Ans.All data variables in Java have to be declared and initialized before they are used.
When declaring variables, we have to specify the data type of information that the member will
hold – integer, fractional, alphanumeric, and so on. The type of a variable tells the compiler, how
much memory to reserve when storing a variable of that type

Q. Variable Tips – Variable Names – Things to be taken care of while declaring variable
names – Variable Rules
1. Variable names can begin with either an alphabetic character, an underscore (_), or a dollar
sign ($). However, convention is to begin a variable name with a letter. They can consist of only
alphabets, digits, and underscore.
2.Variable names must be one word. Spaces are not allowed in variable names. Underscores are
allowed. “total_marks” is fine but “total marks” is not.
3. There are some reserved words in Java that cannot be used as variable names, for example -
int.
4. Java is a case-sensitive language. Variable names written in capital letters differ from
variable names with the same spelling but written in small letters. For example, the variable
name “percentage” differs from the variable name “PERCENTAGE” or “Percentage”.
5. It is good practice to make variable names meaningful. The name should indicate the use of
that variable.
6. You can define multiple variables of the same type in one statement by separating each
with a comma. E.g. int num1, num2, num3;

Primitive Data Types


In all, Java supports eight primitive data types as in Table 1
May 22 – Board Exam Questions

Q1. How many Primitive data types does Java support in all ? (1 mark)

Q2.What is the size of a Boolean variable in Java ? (1 mark)


Control Flow

Control Flow

Selection Structure Repetition Structure

IF - Else Switch While Loop Do-While Loop For Loop

Java executes the instructions in sequential order, that is, one after the other. However, sometimes you might
want to execute an instruction only if a condition holds true. Or you may want to execute a set of instructions
repeatedly until a condition is met. Java provides selection structures for the former and repetition structures for
the latter.

Selection Structures
In real life, you often select your actions based on whether a condition is true or false. For example, if it is
raining outside, you carry an umbrella, otherwise not.

Similarly in a program, you may want to execute a part of the program based on the value of an expression.
Java provides two statements – the if else statement and the switch statement for executing a block of code
conditionally

Q.What is a Program
Ans. A Program is a a sequence of instructions.
Q. What is a Block of Code –
Ans. A block of code is a sequence of statements enclosed in curly braces.

The if Else Statement

The if statement in Java lets us execute a block of code depending upon whether an expression evaluates to true
or false. The structure of the Java if statement is as below:
if (expression)
{
statements
}

The expression given in the round brackets after the if keyword, is a condition – an expression constructed
using relational and logical operators. If the condition evaluates to true, the block of code following if is
executed, otherwise not. If there is only one statement in the block after the if, the curly braces are
optional.
 The structure of the Java if else statement is as shown below:
if(expression)
{
statements
}
else
{
statements
}
the block of code following if is executed if the conditional expressionevaluates to true. The block of code
following the else is executed if the conditional expression evaluates to false.

 Sometimes, you may want to execute a block of code based on the evaluation of two or more conditional
expressions. For this, you can combine expressions using the logical && or || operators.

if ((percentage >= 40)&& (percentage < 60))


{
System.out.println("PASSED with II Division");
}
if (percentage >= 60)
{
System.out.println("PASSED with I Division ");
}
if (percentage < 40)
{
System.out.println("FAILED");
}

 if-else statements can also be nested – you can have another if-else statement within an outer if or else
statement. The example below is a nested if.

if (percentage >= 40)


{
System.out.println("PASSED");

if (percentage>= 75)
{
System.out.println("With Distinction!");
}
}
else
{
System.out.println("FAILED");
}

Q. If percentage greater than 40 - passed


 if the percentage is above 90, assign grade A
 if the percentage is above 75, assign grade B
Nested
 if the percentage is above 65, assign grade C
if
else print failed

Ans. –

Board Exam Questions :


Term II – 2022 – 2 marks
What will be the value of n after the execution of the following selection statement :
int n=0;
if (6>7 || 5>4)
{
n=5;
}
else
n=7;
Ans. –

2021 – 1 mark
Predict the Output
int x=9, k=0;
int y=10;
y=++x;
x+=y;
k=x;
What will be the value of k?

2022-1 mark
Identify the type of operator used
If (A >=B)
(i) Arithmetic
(ii) Relational
(iii) Logical
(iv) Assignment

Q. Differentiate between = and == operator in JAVA - 2marks


Answer = is an assignment operator
== is a comparison operator
For example int a=10;
Will assign a value 10 to variable a
Whereas a==10 will check whether the value of a is 10

2021 – 1 Mark
If a=20 and b=30 What will be the value of a%=b?
Ans.
The Switch Statement
The switch statement is used to execute a block of code matching one value out of many possible values. The
structure of the Java switch statement is as follows:
switch (expression)
{
case constant_1 : statements;
break;
case constant_2 : statements;
break;


default : statements;
break;
}

Within the switch statement, as you can see, there can be many case statements. The expression is compared
with the constants in each case group and the matching case group is executed. If the expression evaluates to
some constant = constant_1, the statements in the case group constant_1 are executed. Similarly, if the
expression evaluates to constant_2, the statements in the case group constant_2 are executed.

Q.What is the use of break in the switch case ?


Ans. The break statement after each case group terminates the switch and causes execution to continue to the
statements after the switch.

Q.What is the use of default in the switch case ?


Ans. If there is no match for the expression with any case group, the statements in the default part are executed.

(Q. datatypes allowed in expression of switch)


Ans. The expression in the switch statement must evaluate to byte, short, int, or char.

The program code below demonstrates usage of the switch statement.

public class SwitchDemo


{
public static void main (String[ ] args)
{
int today = 5;
String day = “”;

switch (today)
{
case 1: day = “Monday”
break;
case 2: day = “Tuesday”;
break;
case 3: day = “Wednesday”;
break;
case 4: day = “Thursday”;
break;
case 5: day = “Friday”;
break;
case 6: day = “Saturday”;
break;
case 7: day = “Sunday”;
break;
default: day = “Incorrect Day!
break;
}

System.out.println (day);

The break after the assignment causes the switch to terminate.

Board Exam Questions 2022-23


Q. What will be the value of rem after the execution of the following code snippet? why? – 2 marks
code =2;
switch(code)
{case 1: rem= 8;
case 2: rem= 9;
case 3: rem= 10; break;
case 4: rem= 11; break;}
Ans.

You might also like