0% found this document useful (0 votes)
33 views8 pages

Questions Unit 1 Basic Syntactic Constructs in Java

Uploaded by

Harish Khojare
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
33 views8 pages

Questions Unit 1 Basic Syntactic Constructs in Java

Uploaded by

Harish Khojare
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

Unit 1 Basic Syntactic Constructs in Java

Question Bank

1. Enlist the logical operators in Java.


&& : Logical AND

|| : Logical OR

! : Logical NOT

2. Give the syntax and example for the following functions

i) min ( )

ii) Sqrt ( )

i) min()

Syntax: (Any one of the following)

static int min(int x, int y) Returns minimum of x and y

static long min(long x, long y) Returns minimum of x and y

static float min(float x, float y) Returns minimum of x and y

static double min(double x, int y) Returns minimum of x and y

Example:

int y= Math.min(64,45);

ii)Sqrt()

Syntax:

static double sqrt(double arg) Returns square root of arg.

Example:

double y= Math.sqrt(64);

3. Explain any four features of Java

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+" ");
}
}
}

5. Describe types of variables in Java with their scope. 4M

There are three types of variables in Java:

local variable

instance variable

static variable

1) Local Variable

A variable declared inside the body of the method is called local variable.

A local variable cannot be defined with "static" keyword.

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.

Example to understand the types of variables in java

class A{

int data=50;//instance variable

static int m=100;//static variable

void method(){

int n=90;//local variable

} }//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{

public static void main(String args[]){

Simple1 s=new Simple1();

System.out.println(s instanceof Simple1); //true

}}

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.

8. Enlist any four compile time errors.


1)Missing semicolon
2)Missing of brackets in classes and methods
3)Misspelling of variables and keywords.
4)Missing double quotes in Strings.
5)Use of undeclared variable.
6)Incompatible type of assignment/initialization.
7)Bad reference to object

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
}

10. List relational operators in Java.

Operator Description Example


== Is Equal To 3 == 5 returns false
!= Not Equal To 3 != 5 returns true
> Greater Than 3 > 5 returns false
< Less Than 3 < 5 returns true
>= Greater Than or Equal To 3 >= 5 returns false
<= Less Than or Equal To 3 <= 5 returns false

11. Give syntax to create an object of a class with suitable example.

Creating the objects of the class is also called as instantiating an object.


As stated earlier, a class creates new data type.
Objects are created using the ‘new’ operator. This ‘new’ operator creates an object of the specified
class and returns the reference of that object.
Syntax:
Classname objectname=new Classname();
Example :
Country India; // declaration
India = new Country(); // instantiation
12. Explain switch case and conditional operator in java with suitable example.

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.

13.Explain Typecasting in Java.


Type casting is when you assign a value of one primitive data type to another type.
In Java, there are two types of casting:

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
}
}

14.What is JDK,JRE and JVM


What is JDK?
JDK is a software development environment used for making applets and Java applications. The full
form of JDK is Java Development Kit. Java developers can use it on Windows, macOS, Solaris, and Linux.
JDK helps them to code and run Java programs. It is possible to install more than one JDK version on the
same computer.

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.

15.Explain Data types in Java

Note: Do basic programs on if..else, for loop, conditional operator, switch statement.

You might also like