Professional Documents
Culture Documents
if( x == 10 ) {
System.out.print("Value of X is 10");
}else if( x == 20 ) {
System.out.print("Value of X is 20");
}else if( x == 30 ) {
System.out.print("Value of X is 30");
}else {
System.out.print("This is else statement");
}
}
}
Output window:
Value of X is 30
-2-
Q.2)Switch
(I)General
class Main {
public static void main(String[] args) {
int week = 4;
String day;
Output Window:
-3-
(ii)Loop
class Main {
public static void main(String[] args) {
int sum = 0;
int n = 1000;
// for loop
for (int i = 1; i <= n; ++i) {
// body inside for loop
sum += i; // sum = sum + i
}
Output Window
Sum=500500
-4-
(iii) Patterns
import java.util.Scanner;
System.out.println();
}
sc.close();
}
}
Output Window:
-5-
Q3)Function overloading
class Demo{
void disp(int a, double b){
System.out.println("Method A");
}
void disp(int a, double b, double c){
System.out.println("Method B");
}
public static void main(String args[]){
Demo obj = new Demo();
/* I am passing float value as a second argument but
* it got promoted to the type double, because there
* wasn't any method having arg list as (int, float)
*/
obj.disp(100, 20.67f);
}
}
Output Window:
Method A
-6-
Q4)Constructor (Parameterized)
class Example{
Example(int i, int j){
System.out.print("parameterized constructor");
}
Example(int i, int j, int k){
System.out.print("parameterized constructor");
}
public static void main(String args[]){
Example obj = new Example();
}
}
Output Window:
-7-
Q5)String
i)
class Main {
public static void main(String[] args) {
// create strings
String first = "java programming";
String second = "java programming";
String third = "python programming";
Output Window:
-8-
ii)
class Main {
public static void main(String[] args) {
Output window:
-9-
Q6) Array
i)
class Main {
public static void main(String[] args) {
// create an array
int[] age = {12, 4, 5, 2, 5};
Output Window:
-10-
ii)
Output Window:
-11-
iii)
-12-
-13-
Q7)Special Numbers
import java.util.*;
-14-
Output Window-145
-15-