Questions Unit 1 Basic Syntactic Constructs in Java
Questions Unit 1 Basic Syntactic Constructs in Java
Question Bank
|| : Logical OR
! : Logical NOT
i) min ( )
ii) Sqrt ( )
i) min()
Example:
int y= Math.min(64,45);
ii)Sqrt()
Syntax:
Example:
double y= Math.sqrt(64);
1.Object Oriented: In Java, everything is an Object. Java can be easily extended since it is based on the
Object model.
2.Platform Independent: Unlike many other programming languages including C and C++, when Java is
compiled, it is not compiled into platform specific machine, rather into platform independent byte code.
This byte code is distributed over the web and interpreted by the Virtual Machine (JVM) on whichever
platform it is being run on.
3.Simple: Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would
be easy to master.
4.Secure: With Java's secure feature it enables to develop virus-free, tamperfree systems.
Authentication techniques are based on public-key encryption.
5.Architecture-neutral: Java compiler generates an architecture-neutral object file format, which makes
the compiled code executable on many processors, with the presence of Java runtime system.
6.Multithreaded: With Java's multithreaded feature it is possible to write programs that can perform
many tasks simultaneously. This design feature allows the developers to construct interactive
applications that can run smoothly.
7.Interpreted: Java byte code is translated on the fly to native machine instructions and is not stored
anywhere. The development process is more rapid and analytical since the linking is an incremental and
light-weight process.
4. Write a Java Program to find out the even numbers from 1 to 100 using for loop.
class test
{
public static void main(String args[])
{
System.out.println("Even numbers from 1 to 100 :");
for(int i=1;i<=100; i++)
{
if(i%2==0)
System.out.print(i+" ");
}
}
}
local variable
instance variable
static variable
1) Local Variable
A variable declared inside the body of the method is called local variable.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is called instance variable. It is
not declared as static.
It is called instance variable because its value is instance specific and is not shared among instances.
3) Static variable
A variable which is declared as static is called static variable. It cannot be local. You can create a single
copy of static variable and share among all the instances of the class. Memory allocation for static
variable happens only once when the class is loaded in the memory.
class A{
void method(){
} }//end of class
6. Describe instance Of and dot (.) operators in Java with suitable example. 4M
Instance of operator:
The java instance of operator is used to test whether the object is an instance of the specified type
(class or subclass or interface). The instance of in java is also known as type comparison operator
because it compares the instance with type. It returns either true or false. If we apply the instance of
operator with any variable that has null value, it returns false.
Example
class Simple1{
}}
dot (.) operator: The dot operator, also known as separator or period used to separate a variable or
method from a reference variable. Only static variables or methods can be accessed using class name.
Code that is outside the object's class must use an object reference or expression, followed by the dot (.)
operator, followed by a simple field name.
Example
this.name=”john”; where name is a instance variable referenced by ‘this’ keyword
c.getdata(); where getdata() is a method invoked on object ‘c’.
7. Explain the concept of platform independence and portability with respect to Java language.
Java is a platform independent language. This is possible because when a java program is compiled, an
intermediate code called the byte code is obtained rather than the machine code. Byte code is a
highly optimized set of instructions designed to be executed by the JVM which is the interpreter for the
byte code. Byte code is not a machine specific code. Byte code is a universal code and can be moved
anywhere to any platform. Therefore java is portable, as it can be carried to any platform. JVM is a
virtual machine which exists inside the computer memory and is a simulated computer within a
computer which does all the functions of a computer. Only the JVM needs to be implemented for each
platform. Although the details of the JVM will defer from platform to platform, all interpret the same
byte code.
9. Write a program to divide any positive even integer by 2 using bitwise shift operator
class Main {
public static void main(String[] args) {
int number1 = 8;
System.out.println(number1 >> 1); // prints 4
}
switch…case statement:
The switch…case statement allows us to execute a block of code among many alternatives.
Syntax :
switch (expression)
{
case value1:
// code
break;
case value2:
// code
break;
...
...
default:
// default statements
}
The expression is evaluated once and compared with the values of each case.
If expression matches with value1, the code of case value1 are executed. Similarly, the code of case
value2 is executed if expression matches with value2.
break is a required statement, which is used to take break from switch block, if any case is true.
Otherwise even after executing a case, if break is not given, it will go for the next case.
If there is no match, the code of the default case is executed.
Example :
// Java Program to print day of week
// using the switch...case statement
class test1{
public static void main(String[] args) {
int number = 1;
String day;
switch (number)
{
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= "Invalid day";
} System.out.println(day);
}
}
Note : any other relevant example can be considered.
Conditional Operator:
The Conditional Operator is used to select one of two expressions for
evaluation, which is based on the value of the first operands. It is used
to handling simple situations in a line.
Syntax:
expression1 ? expression2:expression3;
The above syntax means that if the value given in Expression1 is true,
then Expression2 will be evaluated; otherwise, expression3 will be
evaluated.
Example
class test
{
public static void main(String[] args) {
String result;
int a = 6, b = 12;
result = (a==b ? "equal":"Not equal");
System.out.println("Both are "+result);
}
}
Note : any other relevant example can be considered.
Widening Casting
Widening casting is done automatically when passing a smaller size type to a larger size type:
Example
public class Main {
public static void main(String[] args) {
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double
System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0
}
}
Narrowing Casting
Narrowing casting must be done manually by placing the type in parentheses in front of the value:
Example
public class Main {
public static void main(String[] args) {
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int
System.out.println(myDouble); // Outputs 9.78
System.out.println(myInt); // Outputs 9
}
}
What is JRE?
JRE is a piece of a software which is designed to run other software. It contains the class libraries,
loader class, and JVM. In simple terms, if you want to run Java program you need JRE. If you are not a
programmer, you don’t need to install JDK, but just JRE to run Java programs. Though, all JDK versions
comes bundled with Java Runtime Environment, so you do not need to download and install the JRE
separately in your PC. The full form of JRE is Java Runtime Environment.
What is JVM?
JVM is an engine that provides a runtime environment to drive the Java Code or applications. It
converts Java bytecode into machine language. JVM is a part of Java Run Environment (JRE). It cannot be
separately downloaded and installed. To install JVM, you need to install JRE. The full form of JVM is Java
Virtual Machine.
In many other programming languages, the compiler produces machine code for a specific system.
However, Java compiler produces code for a virtual machine which is called as JVM.
Note: Do basic programs on if..else, for loop, conditional operator, switch statement.