1. a) Write a java program to calculate sum of all the numbers divisible by 3 from 0 to n and Print the sum?
[.1, BTL2, Understanding (L2),-- 05M]
import java.util.*;
public class Main
{
public
static void main(String[] args) {
int
sum=0;
Scanner
sc=new Scanner(System.in);
int
n=sc.nextInt();
System.out.println(n);
for
(int i=0;i<=n;i++){
if(i%3==0){
sum=sum+i;
}
}
System.out.println("Sum
: "+ sum);
}
}
b) Write
a java program to find the sum of first 'n' odd integer numbers?
[.1, BTL2, Understanding (L2),--
05M]
import java.util.*;
public class Main
{
public
static void main(String[] args) {
int
sum=0;
Scanner
sc=new Scanner(System.in);
int
n=sc.nextInt();
System.out.println(n);
for
(int i=0;i<=n;i++){
if(i%2!=0){
sum=sum+i;
}
}
System.out.println("Sum
of first n odd integers: "+ sum);
}
}
2.
Explain any 10 Java features?
[.1, BTL2, Understanding (L2), -- 10M]
Object Oriented
In Java, everything is an Object. Java can be easily extended since it is based on the Object model.
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.
Simple
Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.
Secure
With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption.
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.
Portable
Being architecture-neutral and having no implementation dependent aspects of the specification makes Java portable. The compiler in Java is written in ANSI C with a clean portability boundary, which is a POSIX subset.
Robust
Java makes an effort to eliminate error-prone situations by emphasizing mainly on compile time error checking and runtime checking.
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.
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.
High Performance
With the use of Just-In-Time compilers, Java enables high performance.
Distributed
Java is designed for the distributed environment of the internet.
Dynamic
Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment. Java programs can carry an extensive amount of run-time information that can be used to verify and resolve accesses to objects at run-time
3.
How to implement precedence
rules and associativity in java language? Give an example program?
[.1,
BTL2, Understanding (L2), -- 10M]]
operator Precedence Table
The table below lists the precedence of operators in Java; higher it appears in the table, the higher its precedence.
Operators | Precedence |
---|---|
postfix increment and decrement | ++ -- |
prefix increment and decrement, and unary | ++ -- + - ~ ! |
multiplicative | * / % |
additive | + - |
shift | << >> >>> |
relational | < > <= >= instanceof |
equality | == != |
bitwise AND | & |
bitwise exclusive OR | ^ |
bitwise inclusive OR | | |
logical AND | && |
logical OR | || |
ternary | ? : |
assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= |
Example: Operator Precedence
class Precedence {
public static void main(String[] args) {
int a = 10, b = 5, c = 1, result;
result = a-++c-++b;
System.out.println(result);
}
}
Output:
2
1.
Write a java program to find
the Fibonacci
Series without using recursion
[.1,
BTL2, Understanding (L2), -- 10M]
import
java.util.*;
public
class Main
{
public static void main (String[]args)
{
int sum = 0;
Scanner sc = new Scanner (System.in);
int n = sc.nextInt ();
int f1 = 0;
int f2 = 1;
System.out.println (f1);
System.out.println (f2);
int f3 = f1 + f2;
while (f3 < n)
{
System.out.println(f3);
f1=f2;
f2=f3;
f3=f1+f2;
}
}
}
2.
Write a java program to Check
the given number is prime or not.
[.1,
BTL2, Understanding (L2), -- 10M]
import java.util.*;
public class Main
{
public
static void main(String[] args) {
int
c=0;
Scanner
sc=new Scanner(System.in);
int
n=sc.nextInt();
System.out.println(n);
for
(int i=2;i<=n-1;i++){
if(n%i==0){
c=c+i;
}
}
if(c>0){
System.out.println(n+
" is not a Prime Number ");
}
else{
System.out.println(n+ " is a Prime Number ");
}
}
}
3.
How to perform type casting in
java? Differentiate it from primitive type conversion with an example program.
[.1, BTL2, Understanding (L2), -- 10M]
Type conversion :
In type conversion, a data type is automatically converted into another data type by a compiler at the compiler time. In type conversion, the destination data type cannot be smaller than the source data type, that’s why it is also called widening conversion. One more important thing is that it can only be applied to compatible data types.
Type Conversion example –
int x=30; float y; y=x; // y==30.000000.
1. a) Write a java program to find the sum of first 'n' even integer
numbers?
[.1,
BTL2, Understanding (L2), -- 05M]
import java.util.*;
public class Main
{
public static
void main(String[] args) {
int
sum=0;
Scanner
sc=new Scanner(System.in);
int
n=sc.nextInt();
System.out.println(n);
for (int
i=0;i<=n;i++){
if(i%2==0){
sum=sum+i;
}
}
System.out.println("Sum
of first n even numbers: "+ sum);
}
}
b) Write a java
program to perform addition of two numbers without using operators + and/or -,
and using ++ and/or –?
[.1,
BTL2, Understanding (L2), -- 05M]
2. Write a java Program to Find Largest of Three Numbers?
[.1,
BTL2, Understanding (L2), -- 10M]
import
java.util.*;
public class Main
{
public static void main(String[] args) {
int sum=0;
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
if(a>b && a>c){
System.out.println(a +
" is largest ");
}
else if(b>c && b >a){
System.out.println(b +
" is largest ");
}
else{
System.out.println(c +
" is largest ");
}
}
}
3. Write the problems associated with procedure languages. Elaborate
how objectoriented languages overcomes the problems of procedural languages?
[.1, BTL2,
Understanding (L2), -- 10M]
1. Write a java program to read minutes and find number of years and
days?
[.1,
BTL2, Understanding (L2), -- 10M]
import java.util.*;
public class Main
{
public static
void main (String[]args)
{
int sum = 0;
Scanner sc =
new Scanner (System.in);
System.out.println("Enter minutes ");
int min =
sc.nextInt ();
int
hrs=min/60;
int
days=hrs/24;
System.out.println("Number
of Days :"+ days);
int
yrs=days/365;
System.out.println("Number of Yrs :"+ yrs);
}
}
2. Write a Java Program to demonstrate the use of break statement ?
[.1,
BTL2, Understanding (L2), -- 10M]
import java.util.*;
public class Main
{
public static
void main (String[]args)
{
int sum = 0;
Scanner sc =
new Scanner (System.in);
System.out.println("Enter any number between 1 to 7 ");
int n =
sc.nextInt ();
switch(n){
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
}
}
3. Discuss various principles of object oriented programming.
[.1,
BTL2, Understanding (L2), -- 10M]