Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

11 17 2018 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 167

Q1

package example;
public class Example {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}

///////////////////////////////////////////////////////////////////////
Q2
package example;
public class Example {
public static void main(String[] args) {
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
}
}

//////////////////////////////////////////////////////////////////////
Q3
package example;
public class Example {
public static void main(String[] args) {
System.out.println("A");
System.out.println("B");
System.out.println("C");
System.out.println("D");
System.out.println("E");
System.out.println("F");
}
}

//////////////////////////////////////////////////////////////////////
Q4 Exercise
===========
//package example;
public class Example {
public static void main(String[] args) {
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
}
}

//////////////////////////////////////////////////////////////////////
Q5 Compile Error
================
package example;
public class example {
public static void main(String[] args) {
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
}
}

//////////////////////////////////////////////////////////////////////
Q6 No Compile Error (Runtime error)
===================================
package example;
public class Example {
static void main(String[] args) {
System.out.println("Hello Java");
}
}

//////////////////////////////////////////////////////////////////////
Q7 No Compile Error (Runtime error)
===================================
package example;
public class Example {
public static void main(String args) {
System.out.println("Hello Java");
}
}
//////////////////////////////////////////////////////////////////////
Q8 No Compile Error (Runtime error)
===================================
package example;
public class Example {
public void main(String[] args) {
System.out.println("Hello Java");
}
}

//////////////////////////////////////////////////////////////////////
Q9
package example;
public class Example {
public static void Main(String[] args) {
System.out.println("Hello Java");
}
}

//////////////////////////////////////////////////////////////////////
Q1

//////////////////////////////////////////////////////////////////////
Q2
package example;
public class Example {
public static void main() {
System.out.println("Hello Java");
}
}

//////////////////////////////////////////////////////////////////////
Q11 Compile, run ok
===================
package example;
public class Example {
public static void main(String args[]) {
System.out.println("Hello Java");
}
}

//////////////////////////////////////////////////////////////////////
Q12 Compile, run ok
===================
package example;
public class Example {
public static void main(String danapala[]) {
System.out.println("Hello Java");
}
}

//////////////////////////////////////////////////////////////////////
Q13 Compile, run ok
===================
package example;
public class Example {
static public void main(String args[]) {
System.out.println("Hello Java");
}
}

//////////////////////////////////////////////////////////////////////
Q14 Compile, run ok
===================
package example;
public class Example {
public static void main(String ... args) {
System.out.println("Hello Java");
}
}

//////////////////////////////////////////////////////////////////////
Q15 Compile, run ok
===================
package example;
public interface Example {
public static void main(String ... args) {
System.out.println("Hello Java");
}
}

//////////////////////////////////////////////////////////////////////
Q16 Error
========
package example;
public class Example {
public static void main(String []args) {
system.out.println("Hello Java");
}
}

//////////////////////////////////////////////////////////////////////
Q17 Error
=========
package example;
public class Example {
public static void main(String []args) {
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java")
System.out.println("Hello Java");
System.out.println("Hello Java");
}
}

//////////////////////////////////////////////////////////////////////
System.out.println(data) vs System.out.print(data);
//////////////////////////////////////////////////////////////////////
Q18
package example;
public class Example {
public static void main(String []args) {
System.out.print("A");
System.out.print("B");
System.out.print("C");
System.out.print("D");
System.out.print("E");
}
}

//////////////////////////////////////////////////////////////////////
Q19
package example;
public class Example {
public static void main(String []args) {
System.out.println("A");
System.out.print("B");
System.out.print("C");
System.out.println("D");
System.out.print("E");
System.out.print("F");
System.out.println("G");

}
}

//////////////////////////////////////////////////////////////////////
Java Comments
//////////////////////////////////////////////////////////////////////
Q20 Line Comments
=================
package example;
public class Example {
public static void main(String []args) {
System.out.println("A");
//System.out.println("B");
System.out.println("C");
//System.out.println("D");
System.out.println("E");
System.out.println("F");
//System.out.println("G");

}
}

//////////////////////////////////////////////////////////////////////
Q21 Block Comments
==================
package example;
public class Example {
public static void main(String []args) {
System.out.println("A");
System.out.println("B");
/*System.out.println("C");
System.out.println("D");
System.out.println("E");
System.out.println("F");*/
System.out.println("G");

}
}
//////////////////////////////////////////////////////////////////////
Q22 Exercise
============
package example;
public class Example {
public static void main(String []args) {
//----------Start main method-----------
System.out.println("A"); //prints A
System.out.println("B"); //prints B
System.out.println("C"); //prints C
//----------End main method-------------
}
}

//////////////////////////////////////////////////////////////////////
Q23 Exercise
============
package example;
public class Example {
public static void main(String []args) {
///////////////////////////////////
}
}

//////////////////////////////////////////////////////////////////////
Q24 Exercise
============
package example;
public class Example {
public static void main(String []args) {
///////////////////////////////////
///////////////////////////////////
///////////////////////////////////
///////////////////////////////////
}
}
//////////////////////////////////////////////////////////////////////
Q25 Exercise
============
package example;
public class Example {
public static void main(String []args) {
/********************************/
///////////////////////////////////
/********************************/
/********************************/
}
}

//////////////////////////////////////////////////////////////////////
Q26 Exercise
============
package example;
public class Example {
public static void main(String []args) {
/***************//*****************/
}
}
//////////////////////////////////////////////////////////////////////
Q27 Exercise
============
package example;
public class Example {
public static void main(String []args) {
/***************//*****************/
//***************//*****************/
}
}
//////////////////////////////////////////////////////////////////////
Q28 Exercise
============
package example;
public class Example {
public static void main(String []args) {
/***************
sadasfasdfa dfd d
dfbgddddddddddddddddd dfhdfh sdfh
***************/
/*****************/
}
}

//////////////////////////////////////////////////////////////////////
Q29 Exercise
============
package example;
public class Example {
public static void main(String []args) {
/***************
//Hello Java
*/***************/
//*****************
*//Hello
}
}

//////////////////////////////////////////////////////////////////////
Java Literals (Simple Data in Java)
Integer
Floating-point
Characters
Boolean
String

//////////////////////////////////////////////////////////////////////
Q30 Integer Literals
====================
package example;

public class Example {

public static void main(String[] args) {


System.out.println(100); //prints 100
System.out.println(12345);
System.out.println(-12345);
}
}

//////////////////////////////////////////////////////////////////////
Q31
package example;

public class Example {

public static void main(String[] args) {


System.out.println(100); //Decimal Literals
System.out.println(0B1100100); //Binary Literals
System.out.println(0144); //Octal Literals
System.out.println(0X64); //Hexadecimal
}
}

//////////////////////////////////////////////////////////////////////
Q32 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


System.out.println(100); //Decimal
System.out.println(0B1100100); //Binary
System.out.println(0B112); //Illegal ?
}
}

//////////////////////////////////////////////////////////////////////
Q33 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


System.out.println(100); //Decimal
System.out.println(0144); //Ocatal
System.out.println(0147); //
System.out.println(0148); //Illegal ?
}
}

//////////////////////////////////////////////////////////////////////
Q34 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


System.out.println(100); //Decimal
System.out.println(0x64); //Hexadecimal '0x' or '0X'
System.out.println(0X64);
System.out.println(0Xabcd);
System.out.println(0XABCD);
}
}

//////////////////////////////////////////////////////////////////////
Q35 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


System.out.println(0XABCDEF);
System.out.println(0XABCDG);

}
}

//////////////////////////////////////////////////////////////////////
Q36 Floating-Point Literals
===========================
package example;

public class Example {

public static void main(String[] args) {


System.out.println(12.2323);
System.out.println(-12.2323);

}
}

//////////////////////////////////////////////////////////////////////
Q37
package example;

public class Example {

public static void main(String[] args) {


System.out.println(1000000000.0);
System.out.println(1E9);
System.out.println(1E3);
System.out.println(0.00012);
System.out.println(1.2E-4);
System.out.println(-1.2E-4);
}
}

//////////////////////////////////////////////////////////////////////
Q38 Charactera Literals
=======================
package example;

public class Example {

public static void main(String[] args) {


System.out.println('A');
System.out.println('7'); //character 7
System.out.println('*');
}
}

//////////////////////////////////////////////////////////////////////
Q39
package example;

public class Example {

public static void main(String[] args) {


System.out.println('AB'); //Illegal
}
}

//////////////////////////////////////////////////////////////////////
Q40 Escape characters
=====================
package example;

public class Example {

public static void main(String[] args) {


System.out.println('\b'); //backspace character
System.out.println('\n'); //newline character
System.out.println('\t'); //backspace character
System.out.println('\f'); //form feed
System.out.println('\r'); //carriage return character
System.out.println('\''); //single quote
System.out.println('\"'); //double quoto
System.out.println('\\'); //backslash character
}
}

//////////////////////////////////////////////////////////////////////
Q41 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


System.out.println('\g');
}
}

//////////////////////////////////////////////////////////////////////
Q42 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


System.out.println("AB\tCD");
System.out.println("AB\bCD");
System.out.println("AB\nCD");
}
}

//////////////////////////////////////////////////////////////////////
Q43 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


System.out.println("AB\rCD");
System.out.println("AB\fCD");
System.out.println("AB\'CD");
}
}

//////////////////////////////////////////////////////////////////////
Q44 Exercise
===========
package example;

public class Example {

public static void main(String[] args) {


System.out.println("AB\\CD");
System.out.println("AB\'CD");
System.out.println("AB\"CD");
}
}

//////////////////////////////////////////////////////////////////////
Q45 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


System.out.println("/\/\/\"); //output-->/\/\/\
}
}

//////////////////////////////////////////////////////////////////////
Q46 From Q45
============
package example;

public class Example {

public static void main(String[] args) {


System.out.println("/\\/\\/\\"); //output-->/\/\/\
}
}

//////////////////////////////////////////////////////////////////////
Q47 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


System.out.println(); //output-->C:\\Windows\"Calc.exe"
}
}

//////////////////////////////////////////////////////////////////////
Q48 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


System.out.println("C:\\\\Windows\\\"Calc.exe\""); //output-->C:\\Windows\"Calc.exe"
}
}

//////////////////////////////////////////////////////////////////////
Q49 Exercise
===========
package example;

public class Example {

public static void main(String[] args) {


System.out.println("\y"); //Illegal
}
}

//////////////////////////////////////////////////////////////////////
Q50 Boolean Literals
====================
package example;

public class Example {

public static void main(String[] args) {


System.out.println(true); //reserved words
System.out.println(false); //reserved words
//System.out.println(False);
}
}

//////////////////////////////////////////////////////////////////////
Q51 String Literals
===================
package example;

public class Example {

public static void main(String[] args) {


System.out.println("Niroth"); //String literals
System.out.println("A");
System.out.println("7");
}
}
//////////////////////////////////////////////////////////////////////
Q52 Exercise=
============
package example;

public class Example {

public static void main(String[] args) {


System.out.println(7); //Integer
System.out.println(7.0); //float
System.out.println('7'); //character
System.out.println("7"); //String
}
}

//////////////////////////////////////////////////////////////////////
Computer Variables
//////////////////////////////////////////////////////////////////////
Q53
package example;

public class Example {

public static void main(String[] args) {


int x; //Create a variable 'x' --->Variable declaration
x=100; //assign 100 to x --->Variable initialization
System.out.println(x); //print value of x
}
}

//////////////////////////////////////////////////////////////////////
Q54 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


int x;
x=100;
System.out.println(x); //variable
System.out.println('x'); //character x
System.out.println("x"); //String x
}
}

//////////////////////////////////////////////////////////////////////
Q55 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


int x;
//x=100;
System.out.println(x); //Illegal ---> Variable initializaion
}
}

//////////////////////////////////////////////////////////////////////
Q56 Exercise
============
package example;

public class Example {


public static void main(String[] args) {
int x;
x=100;
x=200;
System.out.println(x); //prints 200
}
}

//////////////////////////////////////////////////////////////////////
Q57 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


int x;
x=100;
System.out.println(x); //prints 100
x=200;
System.out.println(x); //prints 200
}
}

//////////////////////////////////////////////////////////////////////
Q58 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


int x;
System.out.println(x); //prints 100
x=100;
}
}

//////////////////////////////////////////////////////////////////////
Q59 Exercise
============
package example;
public class Example {

public static void main(String[] args) {


int x;
x=100;
int y;
y=200;
System.out.println(x); //prints 100
System.out.println(y); //prints 200
}
}

//////////////////////////////////////////////////////////////////////
Q60 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


int x,y;
x=100;
y=200;
System.out.println(x); //prints 100
System.out.println(y); //prints 200
}
}

//////////////////////////////////////////////////////////////////////
Q61 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


int x=100;
int y=200;
System.out.println(x); //prints 100
System.out.println(y); //prints 200
}
}
//////////////////////////////////////////////////////////////////////
Q62 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


int x=100,y,z=300;
y=200;
System.out.println(x); //prints 100
System.out.println(y); //prints 200
System.out.println(z); //prints 300
}
}

//////////////////////////////////////////////////////////////////////
Q63 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


int x,y,z;
x=y=z=100;
System.out.println(x); //prints 100
System.out.println(y); //prints 100
System.out.println(z); //prints 100
}
}

//////////////////////////////////////////////////////////////////////
Q64 Exercise
============
package example;

public class Example {


public static void main(String[] args) {
int x,y,z;
x=y=z=100;
System.out.println(x); //prints 100
System.out.println(y); //prints 100
System.out.println(z); //prints 100
int x=500;
System.out.println(x); //?
}
}

//////////////////////////////////////////////////////////////////////
Q65 Exercise
============
package example;

public class Example {

public static void main(String[] args) {


int x,y,z;
x=y=z=100;
System.out.println(x); //prints 100
System.out.println(y); //prints 100
System.out.println(z); //prints 100
int X=500; //Legal
System.out.println(X); //?
}
}

//////////////////////////////////////////////////////////////////////
Q66 Exercise
============
class Example{
public static void main(String args[]){
int x=y=z=200;
System.out.println(x);
System.out.println(y);
System.out.println(z);
}
}

//////////////////////////////////////////////////////////////////////
Q67 Exercise
============
class Example{
public static void main(String args[]){
int x,y,z;
x=600;
y=200;
z=x+y;
System.out.println(z);
System.out.println(x+y);

z=x-y;
System.out.println(z);
System.out.println(x-y);

z=x*y;
System.out.println(z);
System.out.println(x*y);

z=x/y;
System.out.println(z);
System.out.println(x/y);
}
}

//////////////////////////////////////////////////////////////////////
Q68 Exercise
============
class Example{
public static void main(String args[]){
int x,y;
x=600;
y=200;
System.out.println(x);
System.out.println(y);

x=y;
System.out.println(x);
System.out.println(y);
}
}

//////////////////////////////////////////////////////////////////////
Q69 Execise
===========
class Example{
public static void main(String args[]){
int x=10,y=20,z=30;
System.out.println(x);
System.out.println(y);
System.out.println(z);

x=y=z;
System.out.println(x);
System.out.println(y);
System.out.println(z);

z=y=x=100;
System.out.println(x);
System.out.println(y);
System.out.println(z);

}
}

//////////////////////////////////////////////////////////////////////
String Concatenatin vs Arithmetic Addition
//////////////////////////////////////////////////////////////////////
Q70
package example;

class Example {

public static void main(String args[]) {


System.out.println(10+20); //Arithmetic Addition
System.out.println("10"+"20"); //String contenation
System.out.println("10"+20); //String contenation
System.out.println(10+"20"); //String contenation

}
}

//////////////////////////////////////////////////////////////////////
Q71 Exercise
============
package example;

class Example {

public static void main(String args[]) {


int x=10,y=20;
System.out.println("x+y"); //Line 1
System.out.println("x"+"y");//Line 2
System.out.println(x+"y"); //Line 3
System.out.println("x"+y); //Line 4
System.out.println(x+y); //Line 5
}
}

//////////////////////////////////////////////////////////////////////
Q72 Exercise
============
package example;

class Example {

public static void main(String args[]) {


int x=10,y=20,z=30;
System.out.println("x+y+z"); //Line 1
System.out.println("x+y"+"z"); //Line 2
System.out.println("x"+"y+z"); //Line 3
System.out.println("x"+"y"+"z");//Line 4
System.out.println(x+"y"+"z"); //Line 5
System.out.println("x"+y+"z"); //Line 6
System.out.println("x"+"y"+z); //Line 7
System.out.println("x"+y+z); //Line 8
System.out.println(x+y+"z"); //Line 9
System.out.println(x+y+z); //Line 10
}
}

//////////////////////////////////////////////////////////////////////
Q73 Exercise
============
package example;

class Example {

public static void main(String args[]) {


int age=12;
System.out.println(); //Your age is : 12
}
}

//////////////////////////////////////////////////////////////////////
Q74 From Q73
============
package example;

class Example {

public static void main(String args[]) {


int age=12;
System.out.println("your age is :"+age); //Your age is : 12
}
}

//////////////////////////////////////////////////////////////////////
Q75 Exercise
============
package example;

class Example {

public static void main(String args[]) {


int x=100,y=200;
System.out.println(); //100 200
}
}

//////////////////////////////////////////////////////////////////////
Q76 From Q75
============
package example;

class Example {

public static void main(String args[]) {


int x=100,y=200;
System.out.println(x+" "+y); //100 200
}
}

//////////////////////////////////////////////////////////////////////
Q77 Exercise
============
package example;
class Example {

public static void main(String args[]) {


int year=2018;
int month=10;
int day=6;
System.out.println(); //2018-10-6
}
}

//////////////////////////////////////////////////////////////////////
Q78 From Q77
============
package example;

class Example {

public static void main(String args[]) {


int year=2018;
int month=10;
int day=6;
System.out.println(year+"-"+month+"-"+day); //2018-10-6
}
}

//////////////////////////////////////////////////////////////////////
Q79 Exerxcise
=============
package example;

class Example {

public static void main(String args[]) {


int x=100;
int y=200;
int z;
z=x+y;
System.out.println(/*Insert Code Here*/);//100 + 200 = 300
}
}

//////////////////////////////////////////////////////////////////////
Q80 From Q79
============
package example;

class Example {

public static void main(String args[]) {


int x=100;
int y=200;
int z;
z=x+y;
System.out.println(x+" + "+y+" = "+z);//100 + 200 = 300
System.out.println(x+" + "+y+" = "+(x+y));//100 + 200 = 300
}
}

//////////////////////////////////////////////////////////////////////
Keyboard input
//////////////////////////////////////////////////////////////////////
Q81
package example;

import java.util.Scanner;

class Example {

public static void main(String args[]) {


Scanner input=new Scanner(System.in);
int x=input.nextInt();
int y=input.nextInt();
int z;
z=x+y;
System.out.println(x+" + "+y+" = "+z);//100 + 200 = 300
}
}

//////////////////////////////////////////////////////////////////////
Q82 From Q81
============
package example;

import java.util.Scanner;
class Example {

public static void main(String args[]) {


Scanner input=new Scanner(System.in);
System.out.print("Input number 1 : ");
int x=input.nextInt();
System.out.print("Input number 2 : ");
int y=input.nextInt();
int z;
z=x+y;
System.out.println(x+" + "+y+" = "+z);//100 + 200 = 300
}
}

//////////////////////////////////////////////////////////////////////
Q83 Exercise
============
package example;

import java.util.Scanner;

class Example {

public static void main(String args[]) {


Scanner input=new Scanner(System.in);
System.out.print("Input your age : ");
int age=input.nextInt();

//find age after 10years;


int x=age+10;

//Result
System.out.println("your age after 10 years : "+x);
}
}

//////////////////////////////////////////////////////////////////////
Q84
package example;

import java.util.Scanner;

class Example {
public static void main(String args[]) {
Scanner input=new Scanner(System.in);
System.out.print("Input your age : ");
int age=input.nextInt();

//find age after 10years;


age=age+10;

//Result
System.out.println("your age after 10 years : "+age);
}
}

//////////////////////////////////////////////////////////////////////
Q85 Exercise
============
package example;

import java.util.Scanner;

class Example {

public static void main(String args[]) {


Scanner input=new Scanner(System.in);
System.out.print("Input number 1 : ");
int x=input.nextInt();
System.out.print("Input number 2 : ");
int y=input.nextInt();
System.out.println(x+" "+y);
/*
Insert code
*/
System.out.println(x+" "+y);
}
}

//////////////////////////////////////////////////////////////////////
Q86 From Q85
============
package example;

import java.util.Scanner;
class Example {

public static void main(String args[]) {


Scanner input=new Scanner(System.in);
System.out.print("Input number 1 : ");
int x=input.nextInt();
System.out.print("Input number 2 : ");
int y=input.nextInt();
System.out.println(x+" "+y);
int temp=x;
x=y;
y=temp;
System.out.println(x+" "+y);
}
}

//////////////////////////////////////////////////////////////////////
Primitive Data types in Java
//////////////////////////////////////////////////////////////////////
Q87
package example;

import java.util.Scanner;

class Example {

public static void main(String args[]) {


int x;
x=1.5; //Illegal
System.out.println(x);
}
}

//////////////////////////////////////////////////////////////////////
Q88 From Q87
============
package example;

import java.util.Scanner;

class Example {
public static void main(String args[]) {
double x;
x=1.5; //Legal
System.out.println(x);
}
}

//////////////////////////////////////////////////////////////////////
Q89 Type "byte"
===============
class Example{
public static void main(String args[]){
byte b;
b=100; //Legal
System.out.println(b);
b=127; //Legal
System.out.println(b);
b=128; //Illegal
}
}

//////////////////////////////////////////////////////////////////////
Q90
class Example{
public static void main(String args[]){
byte b;
b=-100; //Legal
System.out.println(b);
b=-128; //Legal
System.out.println(b);
b=-129; //Illegal
}
}

//////////////////////////////////////////////////////////////////////
Q91
class Example{
public static void main(String args[]){
byte b;
b=Byte.MAX_VALUE;
System.out.println(b);
b=Byte.MIN_VALUE;
System.out.println(b);
}
}

//////////////////////////////////////////////////////////////////////
Q92
class Example{
public static void main(String args[]){
short s;
s=1200;
System.out.println(s);
s=32767; //Legal
System.out.println(s);
s=32768; //Illegal

s=-32767; //Legal
s=-32768; //Legal
s=-32769; //Illegal

}
}

//////////////////////////////////////////////////////////////////////
Q93
class Example{
public static void main(String args[]){
short s;
s=Short.MAX_VALUE;
System.out.println(s);
s=Short.MIN_VALUE;
System.out.println(s);
}
}

//////////////////////////////////////////////////////////////////////
Q94
class Example{
public static void main(String args[]){
int x;
x=Integer.MAX_VALUE;
System.out.println(x);

x=Integer.MIN_VALUE;
System.out.println(x);
}
}

//////////////////////////////////////////////////////////////////////
Q95
class Example{
public static void main(String args[]){
long x;
x=Long.MAX_VALUE;
System.out.println(x);

x=Long.MIN_VALUE;
System.out.println(x);
}
}

//////////////////////////////////////////////////////////////////////
Q96 Type "float"
================
class Example{
public static void main(String args[]){
float f;
f=100;
System.out.println(f);

f=1.5; //Illegal
f=1.5f; //Legal
System.out.println();
}
}

//////////////////////////////////////////////////////////////////////
Q97
class Example{
public static void main(String args[]){
float f;
f=Float.MAX_VALUE;
System.out.println(f);

f=Float.MIN_VALUE;
System.out.println(f);
}
}
//////////////////////////////////////////////////////////////////////
Q98 Type "double"
=================
class Example{
public static void main(String args[]){
double d;
d=12.343; //Legal
System.out.println(d);
d=12.232232d; //or D Legal
System.out.println(d);

d=Double.MAX_VALUE;
System.out.println(d);

d=Double.MIN_VALUE;
System.out.println(d);
}
}

//////////////////////////////////////////////////////////////////////
Q99 "char"
=========
class Example{
public static void main(String args[]){
char ch;
ch='A';
System.out.println("ch : "+ch);

ch='5';
System.out.println("ch : "+ch);
}
}

//////////////////////////////////////////////////////////////////////
Q100 "boolean"
=============
class Example{
public static void main(String args[]){
boolean b;
b=true;
System.out.println("boolean : "+b);
b=false;
System.out.println("boolean : "+b);

b=10>9;
System.out.println("10>9 : "+b);

double avg=49.99;
boolean isPass=avg>50;
System.out.println("pass : "+isPass);
}
}

//////////////////////////////////////////////////////////////////////
Q101
class Example{
public static void main(String args[]){
char x='A';
System.out.println(x);
x=66;
System.out.println(x); //prints B

x='1';
System.out.println(x); //prints 1
}
}

//////////////////////////////////////////////////////////////////////
Q102 Exercise
============
class Example{
public static void main(String args[]){
char ch;
ch='A';
System.out.println(ch);
ch=66;
System.out.println(ch);

int x;
x='A';
System.out.println(x);
}
}
//////////////////////////////////////////////////////////////////////
Q103
Exercise
============

class Example{
public static void main(String args[]){
char x='A';
System.out.println("A"+100); //prints A100
System.out.println('A'+100); //prints 165
}
}

//////////////////////////////////////////////////////////////////////
Q104
Exercise
============
class Example{
public static void main(String args[]){
char x='A';
System.out.println(x+x); //130
System.out.println('x'+x); //
System.out.println('x'+'x'); //
System.out.println("x"+"x"); //
}
}

//////////////////////////////////////////////////////////////////////
Q105 Exercise
=============
class Example{
public static void main(String args[]){
boolean b=true;
System.out.println(b);
b=100<99;
System.out.println(b);
b=1; //Illegal
b=0; //Illegal
}
}

//////////////////////////////////////////////////////////////////////
Q106 String (not a primitive data type)
======================================
class Example{
public static void main(String args[]){
String s="Hello Java";
System.out.println(s);

String name=new String("Niroth");


System.out.println(name);
}
}

//////////////////////////////////////////////////////////////////////
Q107
Exercise
============
import java.util.*;
class Example{
public static void main(String args[]){
Scanner input=new Scanner(System.in);
byte b;
b=input.nextByte();
short s;
s=input.nextShort();

int x;
x=input.nextInt();

long y;
y=input.nextLong();

float f;
f=input.nextFloat();
double d;
d=input.nextDouble();

boolean bool;
bool=input.nextBoolean();

String name;
name=input.next();
}
}
//////////////////////////////////////////////////////////////////////
Q108 String
===========
import java.util.*;
class Example{
public static void main(String args[]){
Scanner input=new Scanner(System.in);

String name;
System.out.print("Input your name : ");

name=input.nextLine(); //name=input.next();
System.out.println("your name : "+name);
}
}

//////////////////////////////////////////////////////////////////////
Java Identifiers, Reserve Words, Keywords
//////////////////////////////////////////////////////////////////////
Q109 Exercise
============
class Example{
public static void main(String args[]){
int customerTotal; //LEgal
int customer total; //Illeal
}
}

//////////////////////////////////////////////////////////////////////
Q110 Exercise
============
class Example{
public static void main(String args[]){
int age, my_age;
int $count;
int age#123; //Illegal
}
}

//////////////////////////////////////////////////////////////////////
Q111 Exercise
============
class Example{
public static void main(String args[]){
int aaaaaaaaaaaaaaaaaaaaaaaa; //Legal
int fdssdsdfsdsdfsdfsdfdsfsdf; //Legal
}
}

//////////////////////////////////////////////////////////////////////
Q112 Exercise
============
class Example{
public static void main(String args[]){
int For,Class, Int ;//Legal
int for; //Illegl
int byte; //Illegal
}
}

//////////////////////////////////////////////////////////////////////
Q113 Exercise
============
class Example{
public static void main(String args[]){
int count2, x1,x2; //Legal

int 2count; //Illegal


int 2x; //Illegal

}
}

//////////////////////////////////////////////////////////////////////
Unicode Character System
//////////////////////////////////////////////////////////////////////
Q114
package example;
class Example {

public static void main(String args[]) {


System.out.println('A'); //A
System.out.println(65); //65
System.out.println('\u0041'); //Unicode Characters System
}
}
//////////////////////////////////////////////////////////////////////
Q115
package example;
class Example {

public static void main(String args[]) {


int \u0061; //ASCII of 'a' ->97
\u0061=100;
System.out.println(a);
}
}

//////////////////////////////////////////////////////////////////////
Q116
package example;
class Example {

public static void main(String args[]) {


char \u0061\u0062\u0063;
abc='\u0061';
System.out.println(\u0061\u0062\u0063);
}
}

//////////////////////////////////////////////////////////////////////
Final Variables in Java
//////////////////////////////////////////////////////////////////////
Q117
package example;
class Example {

public static void main(String args[]) {


int x=100;
final int y=200;
System.out.println(x+" "+y);

x=10;
y=20; //Illegal

System.out.println(x+" "+y);
}
}
//////////////////////////////////////////////////////////////////////
Conversion and Casting
//////////////////////////////////////////////////////////////////////
Q118
package example;
class Example {

public static void main(String args[]) {


byte b=10;
short s;
s=b;
System.out.println(b+" "+s);

b=s; //Illegal
}
}

//////////////////////////////////////////////////////////////////////
Q119
package example;
class Example {

public static void main(String args[]) {


byte b=10;
short s;
s=b;
System.out.println(b+" "+s);

//b=s; //Illegal
b=(byte) s; //Casting
System.out.println(b+" "+s);
}
}

//////////////////////////////////////////////////////////////////////
Q120 Automatic Conversion
=========================
class Example{
public static void main(String args[]){
byte b=100;
int x;
x=b; //automatic conversion
System.out.println(b+" "+x);
}
}

//////////////////////////////////////////////////////////////////////
Q121
class Example{
public static void main(String args[]){
byte b=100;
double d;
d=b; //automatic conversion
System.out.println(b+" "+d);
}
}

//////////////////////////////////////////////////////////////////////
Q122 Exercise
=============
class Example{
public static void main(String args[]){
char ch='A';
int code;
code=ch; //automatic conversion
System.out.println("ASCII of A : "+code);;

double d;
d=ch; //automatic
System.out.println(ch+" "+d);//A : 65.0
}
}

//////////////////////////////////////////////////////////////////////
Q123 Exercise
=============
class Example{
public static void main(String args[]){
byte b=65;
char ch;
//ch=b; //Illegal
short s=65;
//s=ch; //Illegal

int x=65;
ch=x; //Illegal
}
}

//////////////////////////////////////////////////////////////////////
Q124 Exercise
=============
class Example{
public static void main(String args[]){
char ch='A';
byte b;
b=ch; //Illegal

short s;
s=ch; //Illegal

int x;
x=ch; //Legal
}
}

//////////////////////////////////////////////////////////////////////
Q125 Exercis
============
class Example{
public static void main(String args[]){
short s=127;
byte b;
b=127; //Legal
b='A'; //Legal
b=s; //Illegal
}
}

//////////////////////////////////////////////////////////////////////
Q126 Exercise
=============
class Example{
public static void main(String args[]){
short s=100;
byte b;

//b=s; //Illegal
b=(byte)s;
System.out.println(s+" "+b);

s=127;
b=(byte)s;
System.out.println(s+" "+b);

s=128;
b=(byte)s;
System.out.println(s+" "+b); //128 -128

s=256;
b=(byte)s;
System.out.println(s+" "+b); //256 0

s=255;
b=(byte)s;
System.out.println(s+" "+b); //255 -1

}
}

//////////////////////////////////////////////////////////////////////
Q127 Exercise
============
class Example{
public static void main(String args[]){
int x=Integer.MAX_VALUE;
short s;
s=(byte)x;
System.out.println(x+" "+s);
}
}

//////////////////////////////////////////////////////////////////////
Q128 Exercise
=============
class Example{
public static void main(String args[]){
double d=255.1234;
byte b;
b=(byte)d;
System.out.println(d+" : "+b);
short s;
s=(short)d;
System.out.println(d+" : "+s);
}
}

//////////////////////////////////////////////////////////////////////
Q129 Exercise
=============
class Example{
public static void main(String args[]){
double d;
int x;

d=1.23E3; //->1230.0
x=(int)d;
System.out.println(d+" : "+x);

d=123.0E-3; //->0.123
x=(int)d;
System.out.println(d+" : "+x);

}
}

//////////////////////////////////////////////////////////////////////
Q130 Wider Conversion
=====================
class Example{
public static void main(String args[]){
byte b=10;
long x;
x=b; //Wider Conversion
System.out.println(b+" : "+x);
}
}

//////////////////////////////////////////////////////////////////////
Q131 Narrow Conversion
=====================
class Example{
public static void main(String args[]){
int age=19;
int x=10;
//age=age+x; //Illegal
age+=x; //Legal (Narrow Conversion)
System.out.println(age);
}
}

//////////////////////////////////////////////////////////////////////
Q132 Narrow Casting
==================
class Example{
public static void main(String args[]){
int age=19;
byte b;
b=(byte)age;
System.out.println(age);
}
}

//////////////////////////////////////////////////////////////////////
Q133 Wider Casting
==================
class Example{
public static void main(String args[]){
int total=756;
int n=10; //no of students
double avg;
avg=total/n;
System.out.println(avg);//75.0

avg=(double)total/n;
System.out.println(avg);//75.6

avg=total/(double)n;
System.out.println(avg);//75.6
}
}

//////////////////////////////////////////////////////////////////////
Q134 Exercise
=============
import java.util.*;
class Example{
public static void main(String args[]){
final int x=127;
final int y;
y=127;
int z=127;
byte b;
b=127; //Legal
b=x; //Legal
b=y; //Illegal
b=z; //Illegal
}
}

//////////////////////////////////////////////////////////////////////
Q135
package example;
class Example {

public static void main(String args[]) {


double d=67.1213;
System.out.println(d);
System.out.println((char)d);//C
}
}

//////////////////////////////////////////////////////////////////////
Q136 Exercise
=============
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input=new Scanner(System.in);
int x0,x1,x2,x3,x4,x5,x6,x7,x8,x9;
System.out.print("Input number 1 : ");
x0=input.nextInt();
System.out.print("Input number 2 : ");
x1=input.nextInt();
System.out.print("Input number 3 : ");
x2=input.nextInt();
System.out.print("Input number 4 : ");
x3=input.nextInt();
System.out.print("Input number 5 : ");
x4=input.nextInt();
System.out.print("Input number 6 : ");
x5=input.nextInt();
System.out.print("Input number 7 : ");
x6=input.nextInt();
System.out.print("Input number 8 : ");
x7=input.nextInt();
System.out.print("Input number 9 : ");
x8=input.nextInt();
System.out.print("Input number 10: ");
x9=input.nextInt();

int total;

total=x0+x1+x2+x3+x4+x5+x6+x7+x8+x9;
double avg=(double)total/10;

System.out.println("["+x0+","+x1+","+x2+","+x3+","+x4+","+x5+","+x6+","+x7+","+x8+","+x9+"]");
System.out.println("Total : "+total);
System.out.println("Average : "+avg);
}
}

//////////////////////////////////////////////////////////////////////
Arithmetic Operators +,-, /, * %, ++, --
//////////////////////////////////////////////////////////////////////
Q137
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input=new Scanner(System.in);
int num1,num2;
System.out.print("Input number 1 : ");
num1=input.nextInt();
System.out.print("Input number 2 : ");
num2=input.nextInt();
int result;
result=num1+num2;
System.out.println(num1+" + "+num2+" = "+result);
result=num1-num2;
System.out.println(num1+" - "+num2+" = "+result);
result=num1*num2;
System.out.println(num1+" * "+num2+" = "+result);
result=num1/num2;
System.out.println(num1+" / "+num2+" = "+result);
}
}

//////////////////////////////////////////////////////////////////////
Q138 integer divison vs floating-point division
===============================================
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


System.out.println(10/4); //2
System.out.println(10.0/4); //2.5
System.out.println(10/4.0); //2.5
System.out.println((double)10/4); //2.5
}
}

//////////////////////////////////////////////////////////////////////
Q139 Unary +, -
===============
package example;
public class Example {

public static void main(String[] args) {


int x=100;
System.out.println(x);
System.out.println(+x);
System.out.println(-x);

x=+x;
System.out.println(x);
x=-x;
System.out.println(x);
}
}

//////////////////////////////////////////////////////////////////////
Q140 Operator assignment (op=)
==============================
package example;
public class Example {

public static void main(String[] args) {


int age=12;
System.out.println("age : "+age);
age+=10; //age=age+10;
System.out.println("age : "+age);
}
}

//////////////////////////////////////////////////////////////////////
Q141 Exercise
=============
package example;
public class Example {

public static void main(String[] args) {


double salary=45000.00;
float rate=20.0f; //20%
System.out.println("Current salary : "+salary);
//find salary increment by 20%
//
System.out.println("New salary : "+salary);
}
}

//////////////////////////////////////////////////////////////////////
Q142 Exercise
=============
package example;
public class Example {

public static void main(String[] args) {


double salary=1000.00;
float rate=20.0f; //20%
System.out.println("Current salary : "+salary);
//find salary increment by 20%
salary=salary*(100+rate)/100;
System.out.println("New salary : "+salary);
System.out.printf("New Salary : %.2f\n", salary);
}
}

//////////////////////////////////////////////////////////////////////
Q143
package example;
public class Example {

public static void main(String[] args) {


int x;
x=10/3;
System.out.println("10/3 : "+x);
x=10%3;
System.out.println("10%3 : "+x);
}
}

//////////////////////////////////////////////////////////////////////
Q144
package example;
public class Example {

public static void main(String[] args) {


int x;
x=10%1;
System.out.println("10/1 : "+x);
x=10%2;
System.out.println("10%2 : "+x);
x=10%3;
System.out.println("10%3 : "+x);
x=10%4;
System.out.println("10%4 : "+x);
x=10%5;
System.out.println("10%5 : "+x);
x=10%6;
System.out.println("10%6 : "+x);
x=10%7;
System.out.println("10%7 : "+x);
x=10%8;
System.out.println("10%8 : "+x);
x=10%9;
System.out.println("10%9 : "+x);
x=10%10;
System.out.println("10%10 : "+x);

}
}

//////////////////////////////////////////////////////////////////////
Q145
package example;
public class Example {

public static void main(String[] args) {


int x;
x=10%13;
System.out.println("10/13 : "+x);
}
}

//////////////////////////////////////////////////////////////////////
Q146
package example;
public class Example {

public static void main(String[] args) {


double d;
d=4.8 %1.1;
System.out.println("4.8 % 1.1 : "+d);
System.out.printf("4.8 %% 1.1 : %.2f\n",d);
}
}

//////////////////////////////////////////////////////////////////////
Q147 Exercise
=============
package example;

import java.util.Scanner;
public class Example {

public static void main(String[] args) {


Scanner input=new Scanner(System.in);
System.out.print("Input a salary : ");
int salary=input.nextInt();
int r5000,r2000,r1000,r500,r200,r100,r50,r20,r10,r5,r2,r1;

// Find minimum note and coins

System.out.println("R5000 : "+r5000);
System.out.println("R2000 : "+r2000);
System.out.println("R1000 : "+r1000);
System.out.println("R500 : "+r500);
//
//
//
//
System.out.println("R1 : "+r1);

}
}

//////////////////////////////////////////////////////////////////////
Q148 From Q147
=============
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input=new Scanner(System.in);
System.out.print("Input a salary : ");
int salary=input.nextInt();
int r5000,r2000,r1000,r500,r200,r100,r50,r20,r10,r5,r2,r1;

// Find minimum note and coins


r5000=salary/5000;
salary=salary%5000; //salary%=5000;
r2000=salary/2000;
salary%=2000;
r1000=salary/1000;
salary%=1000;
r500=salary/500;
salary%=500;
r200=salary/200;
salary%=200;
r100=salary/100;
salary%=100;
r50=salary/50;
salary%=50;
r20=salary/20;
salary%=20;
r10=salary/10;
salary%=10;
r5=salary/5;
salary%=5;
r2=salary/2;
salary%=2;
r1=salary;
System.out.println("R5000 : "+r5000);
System.out.println("R2000 : "+r2000);
System.out.println("R1000 : "+r1000);
System.out.println("R500 : "+r500);
System.out.println("R200 : "+r200);
System.out.println("R100 : "+r100);
System.out.println("R50 : "+r50);
System.out.println("R20 : "+r20);
System.out.println("R10 : "+r10);
System.out.println("R5 : "+r5);
System.out.println("R2 : "+r2);
System.out.println("R1 : "+r1);
}
}

//////////////////////////////////////////////////////////////////////
Q149 Incrementer(++) and decrementer (--)
=========================================
package example;

import java.util.Scanner;
public class Example {

public static void main(String[] args) {


int x=100;
System.out.println(x); //100
x++; //increment by 1 (x=x+1, x+=1)
System.out.println(x); //101
++x; //increment by 1 (x=x+1, x+=1)
System.out.println(x); //102
x--; //x=x-1; //x-=1
System.out.println(x); //101
--x;
System.out.println(x); //100
}
}

//////////////////////////////////////////////////////////////////////
Q150 x++(post fix) vs ++x (pre fix)
===================================
package example;
public class Example {

public static void main(String[] args) {


int x=100;
int y;
y=x++;
System.out.println(x+" "+y); //101 100
System.out.println("-----------");
//
x=100;
y=++y;
System.out.println(x+" "+y); //101 101
}
}

//////////////////////////////////////////////////////////////////////
Q151 Exercise
=============
package example;
public class Example {

public static void main(String[] args) {


int x=100;
System.out.println(x); //100

System.out.println(x++); //post -->100


System.out.println(++x); //pre -->102
}
}

//////////////////////////////////////////////////////////////////////
Q152 Exercise
=============
package example;
public class Example {

public static void main(String[] args) {


int a=1,b=2,c=3,d=4;
int x;
x=a++ + ++b + ++c + d++;
System.out.println(x+" "+a+" "+b+" "+c+" "+d);
}
}

//////////////////////////////////////////////////////////////////////
Q153 Exercise
=============
package example;
public class Example {

public static void main(String[] args) {


int x=1;
x=x++ + ++x + x++ + ++x;
System.out.println(x);
}
}

//////////////////////////////////////////////////////////////////////
Java Flow Control "if"
//////////////////////////////////////////////////////////////////////
Q154
package example;

import java.util.Scanner;
public class Example {

public static void main(String[] args) {


Scanner input=new Scanner(System.in);
System.out.print("Input a number : ");
int x=input.nextInt();
System.out.println("A");
System.out.println("B");
System.out.println("C");
System.out.println("D");
System.out.println("E");
}
}

//////////////////////////////////////////////////////////////////////
Q155
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input=new Scanner(System.in);
System.out.print("Input a number : ");
int x=input.nextInt();
System.out.println("A");
System.out.println("B");
if(x>10) System.out.println("C");
System.out.println("D");
System.out.println("E");
}
}

//////////////////////////////////////////////////////////////////////
Q156
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input=new Scanner(System.in);
System.out.print("Input a number : ");
int x=input.nextInt();
System.out.println("A");
System.out.println("B");
if(x>10)
System.out.println("C");
System.out.println("D");
System.out.println("E");
}
}

//////////////////////////////////////////////////////////////////////
Q157
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input=new Scanner(System.in);
System.out.print("Input a number : ");
int x=input.nextInt();
System.out.println("A");
System.out.println("B");
if(x>10)
System.out.println("C");
System.out.println("D");
System.out.println("E");
}
}

//////////////////////////////////////////////////////////////////////
Q158
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Input a number : ");
int x = input.nextInt();
System.out.println("A");
System.out.println("B");
if (x > 10) { //using "code block"
System.out.println("C");
System.out.println("D");
}
System.out.println("E");
}
}

//////////////////////////////////////////////////////////////////////
Q159
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Input number 1 : ");
int x = input.nextInt();
System.out.print("Input number 2 : ");
int y = input.nextInt();
System.out.print("Input number 3 : ");
int z = input.nextInt();

//find the max of x,y and z


int max;

System.out.println("Max : "+max);
}
}

//////////////////////////////////////////////////////////////////////
Q160 From Q159
=============
package example;

import java.util.Scanner;
public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Input number 1 : ");
int x = input.nextInt();
System.out.print("Input number 2 : ");
int y = input.nextInt();
System.out.print("Input number 3 : ");
int z = input.nextInt();

//find the max of x,y and z


int max;
max = x;
if (y > max) {
max = y;
}
if (z > max) {
max = z;
}
System.out.println("Max : " + max);
}
}

//////////////////////////////////////////////////////////////////////
Relational Operators in Java
----------------------------
< - less than
> - greater than
<= less than or equals (if(x<=0))
>= greater than or equals to
== if(x==y) -> if x equals to y
!= if(x!=y) -> if x not equals to y

//////////////////////////////////////////////////////////////////////
Java Flow Control "if/else"
//////////////////////////////////////////////////////////////////////
Q161 Exercise
=============
package example;

import java.util.Scanner;
public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Input an integer : ");
int x = input.nextInt();
int mod=x%2;
if(mod==0){
System.out.println("even");
}
if(mod!=0){
System.out.println("odd");
}
}
}

//////////////////////////////////////////////////////////////////////
Q162 From Q161
==============
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);

System.out.print("Input an integer : ");


int x = input.nextInt();

if(x%2==0){
System.out.println("even");
}
if(x%2!=0){
System.out.println("odd");
}
}
}

//////////////////////////////////////////////////////////////////////
Q163 From Q62 Using "if/else"
=============================
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);

System.out.print("Input an integer : ");


int x = input.nextInt();

if(x%2==0){
System.out.println("even");
}else{
System.out.println("odd");
}
}
}

//////////////////////////////////////////////////////////////////////
Q164 Exercise assignment op(=) vs equals Op (==)
================================================
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int x=100;
System.out.println(x=100); //Line 1 =>100
System.out.println(x==100);//Line 2 =>true
System.out.println(x!=100);//Line 3 =>false
}
}

//////////////////////////////////////////////////////////////////////
Q165 Exercise
=============
package example;
import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int x=100;
if(x==0){
System.out.println("x==0");
}
if(x=0){ //Illegal

}
}
}

//////////////////////////////////////////////////////////////////////
Q166
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int x=100;
boolean b;
b=x==0;
if(b){
System.out.println("x==0");
}else{
System.out.println("x!=0");
}

}
}

//////////////////////////////////////////////////////////////////////
Q167
package example;

import java.util.Scanner;
public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
boolean b;
//System.out.println(b);//Illegal =>initialization error
System.out.println(b=true);
System.out.println(b);
System.out.println(b==true);
System.out.println(b==false);
}
}

//////////////////////////////////////////////////////////////////////
Q168 Exercise
=============
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Input student's average : ");
double avg=input.nextDouble();
if(avg>=50){
System.out.println("Pass");
}else{
System.out.println("Fail");
}
}
}

//////////////////////////////////////////////////////////////////////
Q169 From Q168
==============
package example;

import java.util.Scanner;

public class Example {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Input student's average : ");
double avg=input.nextDouble();
boolean isPass=avg>=50;
if(isPass){
System.out.println("Pass");
}else{
System.out.println("Fail");
}
}
}

//////////////////////////////////////////////////////////////////////
Q170 From Q169
==============
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Input student's average : ");
double avg=input.nextDouble();
boolean isPass=avg>=50;
if(isPass==true){ //
System.out.println("Pass");
}else{
System.out.println("Fail");
}
}
}

//////////////////////////////////////////////////////////////////////
Q171 Exercise
=============
package example;
public class Example {
public static void main(String[] args) {
int x=99;
if(x==100){
System.out.println("A");
}
if(x++==100){
System.out.println("B");
}
x=99;
if(++x==100){
System.out.println("C");
}
}
}

//////////////////////////////////////////////////////////////////////
Java Flow Control if/else if
//////////////////////////////////////////////////////////////////////
Q172 Exercise
=============
package example;
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Input average : ");
double avg=input.nextDouble();
if(avg>=75){
System.out.println("A");
}
if(avg>=65){
System.out.println("B");
}
if(avg>=45){
System.out.println("C");
}
if(avg>=25){
System.out.println("D");
}else{
System.out.println("E");
}
}
}

//////////////////////////////////////////////////////////////////////
Q173 From Q172 Using "if/else if" (if ladder)
=============================================
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Input average : ");
double avg = input.nextDouble();
if (avg >= 75) {
System.out.println("A");
} else if (avg >= 65) {
System.out.println("B");
} else if (avg >= 45) {
System.out.println("C");
} else if (avg >= 25) {
System.out.println("D");
} else {
System.out.println("E");
}
}
}

//////////////////////////////////////////////////////////////////////
Q174 Exercise
=============
import java.util.*;
class Example{
public static void main(String args[]){
int x=100;
int y=200;
if(x==y){ //x is equals to y
//Insert code
}
if(x!=y){//x is not equals to y

}
if(x=y){ //illegal

}
if((x=y)==0){ //Legal

}
}
}

//////////////////////////////////////////////////////////////////////
Q175 Exercise
=============
class Example{
public static void main(String args[]){
int x;
x=9;
System.out.println(x==x);

x=9;
System.out.println(x++==x);//

x=9;
System.out.println(++x==x);

x=9;
System.out.println(x==++x);

x=9;
System.out.println(x==x++);

x=9;
System.out.println(x++==x++);

x=9;
System.out.println(++x==++x);

x=9;
System.out.println(++x==x++);

x=9;
System.out.println(x++==++x);

}
}

//////////////////////////////////////////////////////////////////////
17. Boolean Logical Operators
& - Boolean AND
| - Boolean OR
^ - Boolean XOR
! - Boolean NOT
&&- Short Circuit AND
||- Short Circuit OR
:?- Ternary

//////////////////////////////////////////////////////////////////////
Q176
import java.util.*;
class Example{
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.print("Input Student average : ");
double avg=input.nextDouble();
System.out.print("Input Student attendence : ");
double atten=input.nextDouble();
if(avg>=50 | atten>=80){
System.out.println("eligible");
}else{
System.out.println("not eligible");
}
}
}

//////////////////////////////////////////////////////////////////////
Q177 Short Circuit OR "||" VS boolean OR "|"
===========================================
package example;
public class Example {
public static void main(String[] args) {
int x=5, y=3;
System.out.println(x+" "+y); //5 3
if(x++==5 | y++==5){
System.out.println("won");
}
System.out.println(x+" "+y); //6 4
}
}

//////////////////////////////////////////////////////////////////////
Q178 Short Circuit OR "||" VS boolean OR "|"
===========================================
package example;
public class Example {
public static void main(String[] args) {
int x=5, y=3;
System.out.println(x+" "+y); //5 3
if(x++==5 || y++==5){
System.out.println("won");
}
System.out.println(x+" "+y); //6 3
}
}

//////////////////////////////////////////////////////////////////////
Q179 Short Circuit AND "&&" VS boolean AND "&"
==============================================
package example;
public class Example {
public static void main(String[] args) {
int x=3, y=5;
System.out.println(x+" "+y); //3 5
if(x++==5 & y++==5){
System.out.println("won");
}
System.out.println(x+" "+y); //4 6
}
}

//////////////////////////////////////////////////////////////////////
Q180 Short Circuit AND "&&" VS boolean AND "&"
==============================================
package example;
public class Example {
public static void main(String[] args) {
int x=3, y=5;
System.out.println(x+" "+y); //3 5
if(x++==5 && y++==5){
System.out.println("won");
}
System.out.println(x+" "+y); //4 5
}
}
//////////////////////////////////////////////////////////////////////
Random Numbers
//////////////////////////////////////////////////////////////////////
Q181
package example;

import java.util.Random;

public class Example {


public static void main(String[] args) {
Random input=new Random();
int x=input.nextInt(); //min of int to max of int
System.out.println("x : "+x);
}
}

//////////////////////////////////////////////////////////////////////
Q182
package example;

import java.util.Random;

public class Example {


public static void main(String[] args) {
Random input=new Random();
int x=input.nextInt(5); //0 to 4
System.out.println("x : "+x);
}
}

//////////////////////////////////////////////////////////////////////
Q183
package example;

import java.util.Random;

public class Example {


public static void main(String[] args) {
Random input=new Random();
int x=input.nextInt(); //min of int to max of int
int y;
if(x<0){
y=-x;
}else{
y=x;
}
System.out.println(x+" "+y);

}
}

//////////////////////////////////////////////////////////////////////
Q184
package example;

import java.util.Random;

public class Example {


public static void main(String[] args) {
Random input=new Random();
int x=input.nextInt(); //min of int to max of int
int y;
/*
if(x<0){
y=-x;
}else{
y=x;
}
*/
y=x<0 ? -x : x; //if then else operator
System.out.println(x+" "+y);

}
}

//////////////////////////////////////////////////////////////////////
Q185import java.util.*;
class Example{
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.print("Input student average : ");
double avg=input.nextDouble();
/*if(avg>=50){
System.out.println("Pass");
}else{
System.out.println("Fail");
}*/
System.out.println(avg>=50 ? "Pass" : "Fail");
}
}

//////////////////////////////////////////////////////////////////////
Operator Precedence
(), [], .
++, --, !, ~ //right to left
*, /, %
+, -
<, >, <=, >=, ==, !=
&, |, ^
:?
op= //right to left
=
//////////////////////////////////////////////////////////////////////
Java Flow Control "for-loop"
//////////////////////////////////////////////////////////////////////
Q186package example;
public class Example {
public static void main(String[] args) {
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
System.out.println("Hello Java");
}
}

//////////////////////////////////////////////////////////////////////
Q187
package example;
public class Example {
public static void main(String[] args) {
System.out.println("Start");
for(int i=0; i<10; i++) System.out.println("Hello Java");
System.out.println("End");
}
}

//////////////////////////////////////////////////////////////////////
Q188
package example;
public class Example {
public static void main(String[] args) {
System.out.println("Start");
for(int i=0; i<10; i++)
System.out.println("A");
System.out.println("B");
System.out.println("End");
}
}

//////////////////////////////////////////////////////////////////////
Q189
package example;
public class Example {
public static void main(String[] args) {
System.out.println("Start");
for(int i=0; i<10; i++){
System.out.println("A");
System.out.println("B");
}
System.out.println("End");
}
}

//////////////////////////////////////////////////////////////////////
Q190 Exercise
=============
package example;
public class Example {
public static void main(String[] args) {
System.out.println("Start");
for(int i=0; i<100; i++){
System.out.println("Hello Java");
}
System.out.println("End");
}
}

//////////////////////////////////////////////////////////////////////
Q191 Exercise
=============
package example;
public class Example {
public static void main(String[] args) {
System.out.println("Start");
for(int i=0; i<10; i+=2){
System.out.println("Hello Java");
}
System.out.println("End");
}
}

//////////////////////////////////////////////////////////////////////
Q192 Exercise
=============
package example;
public class Example {
public static void main(String[] args) {
System.out.println("Start");
for(int i=5; i<10; i++){
System.out.println("Hello Java");
}
System.out.println("End");
}
}

//////////////////////////////////////////////////////////////////////
Q193 Exercise
=============
package example;
public class Example {
public static void main(String[] args) {
System.out.println("Start");
for(int i=10; i>0; i--){
System.out.println("Hello Java");
}
System.out.println("End");
}
}
//////////////////////////////////////////////////////////////////////
Q194 Exercise
=============
package example;
public class Example {
public static void main(String[] args) {
System.out.println("Start");
for(int i=10; i>0; i++){
System.out.println("Hello Java");
}
System.out.println("End");
}
}

//////////////////////////////////////////////////////////////////////
Q195 Exercise
=============
package example;
public class Example {
public static void main(String[] args) {
System.out.println("Start");
for(int i=10; ; i++){ //default condition "true"
System.out.println("Hello Java");
}
//System.out.println("End");
}
}

//////////////////////////////////////////////////////////////////////
Q196
package example;
public class Example {
public static void main(String[] args) {
System.out.println("Start");
int i=0;
for(;i<10;){ //default condition "true"
System.out.println("Hello Java");
i++;
}
System.out.println("End");
}
}
//////////////////////////////////////////////////////////////////////
Q197 Exercise
=============
package example;
public class Example {
public static void main(String[] args) {
System.out.println("Start");
int i=0;
for(System.out.println("A");i<10;System.out.println("C")){
System.out.println("B");
i++;
}
System.out.println("End");
}
}

//////////////////////////////////////////////////////////////////////
Variable Scope and lifetime
//////////////////////////////////////////////////////////////////////
Q198
package example;
public class Example {
public static void main(String[] args) {
int x=100;
System.out.println(100);
{
int y=1;
System.out.println(x);
System.out.println(y);
x++;
y++;
}
System.out.println(x);
System.out.println(y); //Illegal
}
}

//////////////////////////////////////////////////////////////////////
Q199 Exercise
=============
package example;
public class Example {
public static void main(String[] args) {
int x=100;
System.out.println(100);
{ //----------Start code block..............
int y=1;
System.out.println(x);
System.out.println(y);
x++;
y++;
int y=1; //Illegal
}//---------end code block.................
int y=9; //Legal
int x=12; //Illegal
System.out.println(x);
System.out.println(y); //Legal
}
}

//////////////////////////////////////////////////////////////////////
Q200
package example;
public class Example {
public static void main(String[] args) {
int x=100;
System.out.println(x); //Line 1
System.out.println(y); //Line 2
System.out.println(z); //Line 3
{
int y=200;
System.out.println(x); //Line 4
System.out.println(y); //Line 5
System.out.println(z); //Line 6
{
int z=300;
System.out.println(x); //Line 7
System.out.println(y); //Line 8
System.out.println(z); //Line 9
}
System.out.println(x); //Line 10
System.out.println(y); //Line 11
System.out.println(z); //Line 12
}
System.out.println(x); //Line 13
System.out.println(y); //Line 14
System.out.println(z); //Line 15
}
}

//////////////////////////////////////////////////////////////////////
Q201
package example;
public class Example {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
int k=100;
System.out.println("k : "+k);
k++;
}
}
}

//////////////////////////////////////////////////////////////////////
Q202
package example;
public class Example {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
System.out.println(i); //Illegal
}
}

//////////////////////////////////////////////////////////////////////
Q203
package example;
public class Example {
public static void main(String[] args) {
for (int i = 0; i < 10; i++)
System.out.println(i); //Legal
System.out.println(i); //Illegal
}
}

//////////////////////////////////////////////////////////////////////
Q204
package example;

public class Example {

public static void main(String[] args) {


for (int i = 0; i < 10; i++) {
int j = 0;
System.out.println(i+" "+j);
j++;
}
//System.out.println("i : " + i); //Illegal
//System.out.println("j : " + j); //Illegal

}
}

//////////////////////////////////////////////////////////////////////
Q205 Exercise I
===============
package example;
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int x0,x1,x2,x3,x4,x5,x6,x7,x8,x9;
System.out.print("Input number 1 : ");
x0=input.nextInt();
System.out.print("Input number 2 : ");
x1=input.nextInt();
System.out.print("Input number 3 : ");
x2=input.nextInt();
System.out.print("Input number 4 : ");
x3=input.nextInt();
System.out.print("Input number 5 : ");
x4=input.nextInt();
System.out.print("Input number 6 : ");
x5=input.nextInt();
System.out.print("Input number 7 : ");
x6=input.nextInt();
System.out.print("Input number 8 : ");
x7=input.nextInt();
System.out.print("Input number 9 : ");
x8=input.nextInt();
System.out.print("Input number 10: ");
x9=input.nextInt();

int total;
total=x0+x1+x2+x3+x4+x5+x6+x7+x8+x9;
System.out.println("Total : "+total);
}
}

//////////////////////////////////////////////////////////////////////
Q206
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int x, total;
System.out.print("Input number 1 : ");
x = input.nextInt();
total = x;

System.out.print("Input number 2 : ");


x = input.nextInt();
total = total + x;

System.out.print("Input number 3 : ");


x = input.nextInt();
total = total + x;

System.out.print("Input number 4 : ");


x = input.nextInt();
total = total + x;
System.out.print("Input number 5 : ");
x = input.nextInt();
total = total + x;
System.out.print("Input number 6 : ");
x = input.nextInt();
total = total + x;
System.out.print("Input number 7 : ");
x = input.nextInt();
total = total + x;
System.out.print("Input number 8 : ");
x = input.nextInt();
total = total + x;
System.out.print("Input number 9 : ");
x = input.nextInt();
total = total + x;
System.out.print("Input number 10: ");
x = input.nextInt();
total = total + x;

System.out.println("Total : "+total);
}
}

//////////////////////////////////////////////////////////////////////
Q207 From Q206
==============
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int x, total=0;

System.out.print("Input number 1 : ");


x = input.nextInt();
total = total + x;

System.out.print("Input number : ");


x = input.nextInt();
total = total + x;

System.out.print("Input number : ");


x = input.nextInt();
total = total + x;

System.out.print("Input number 4 : ");


x = input.nextInt();
total = total + x;
System.out.print("Input number 5 : ");
x = input.nextInt();
total = total + x;

System.out.print("Input number 6 : ");


x = input.nextInt();
total = total + x;

System.out.print("Input number 7 : ");


x = input.nextInt();
total = total + x;

System.out.print("Input number 8 : ");


x = input.nextInt();
total = total + x;

System.out.print("Input number 9 : ");


x = input.nextInt();
total = total + x;

System.out.print("Input number 10: ");


x = input.nextInt();
total = total + x;

System.out.println("Total : "+total);
}
}

//////////////////////////////////////////////////////////////////////
Q208 From Q207
==============
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int x, total=0;

for(int i=0; i<10; i++){


System.out.print("Input number 1 : ");
x = input.nextInt();
total = total + x;
}
/*
System.out.print("Input number : ");
x = input.nextInt();
total = total + x;

System.out.print("Input number : ");


x = input.nextInt();
total = total + x;

System.out.print("Input number 4 : ");


x = input.nextInt();
total = total + x;

System.out.print("Input number 5 : ");


x = input.nextInt();
total = total + x;

System.out.print("Input number 6 : ");


x = input.nextInt();
total = total + x;

System.out.print("Input number 7 : ");


x = input.nextInt();
total = total + x;

System.out.print("Input number 8 : ");


x = input.nextInt();
total = total + x;

System.out.print("Input number 9 : ");


x = input.nextInt();
total = total + x;

System.out.print("Input number 10: ");


x = input.nextInt();
total = total + x;
*/
System.out.println("Total : "+total);
}
}

//////////////////////////////////////////////////////////////////////
Q209 Exercise
=============
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int x, total=0;

for(int i=0; i<10; i++){


System.out.print("Input a number : ");
x = input.nextInt();
total = total + x;
}
System.out.println("Total : "+total);
}
}

//////////////////////////////////////////////////////////////////////
Q210
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int x, total=0;

for(int i=0; i<10; i++){


System.out.print("Input number "+i+1+" : ");
x = input.nextInt();
total = total + x;
}
System.out.println("Total : "+total);
}
}

//////////////////////////////////////////////////////////////////////
Q211
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int x, total=0;

for(int i=0; i<10; i++){


System.out.print("Input number "+(i+1)+" : ");
x = input.nextInt();
total = total + x;
}
System.out.println("Total : "+total);
}
}

//////////////////////////////////////////////////////////////////////
Q212
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int x, total = 0;

for (int i = 0; i < 10; i++) {


System.out.print("Input number " + (i + 1) + " : ");
x = input.nextInt();
total = total + x;
}
System.out.println("Total : " + total);
}
}
//////////////////////////////////////////////////////////////////////
Q213 Exercise 1
===============
package example;
public class Example {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.println(i);
}
}
}

//////////////////////////////////////////////////////////////////////
Q214 Exercise 2
===============
package example;
public class Example {
public static void main(String[] args) {
for (int i = 100; i >0; i--) {
System.out.println(i);
}
}
}

//////////////////////////////////////////////////////////////////////
Q215 Exercise 3
===============
package example;
public class Example {
public static void main(String[] args) {
for (int i = 0; i <10; i++) {
System.out.print(i+" ");
}
}
}

//////////////////////////////////////////////////////////////////////
Q216 Exercise 4
===============
package example;
public class Example {
public static void main(String[] args) {
System.out.print("[");
for (int i = 0; i <10; i++) {
System.out.print(i+", ");
}
System.out.println("\b\b]");
}
}

//////////////////////////////////////////////////////////////////////
Q217 Exercise 5
===============
package example;

import java.util.Scanner;

public class Example {


public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Input a number : ");
int num=input.nextInt();

System.out.print("[");
for (int i = 0; i <=num; i++) {
System.out.print(i+", ");
}
System.out.println("\b\b]");
}
}

//////////////////////////////////////////////////////////////////////
Q218 Exercise 6
===============
package example;

import java.util.Scanner;

public class Example {


public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Input a number : ");
int num=input.nextInt();

System.out.print("[");
for (int i = num; i > 0; i--) {
System.out.print(i+", ");
}
System.out.println("\b\b]");
}
}

//////////////////////////////////////////////////////////////////////
Q219 Exercise 7
===============
package example;

import java.util.Scanner;

public class Example {


public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Input lower bound : ");
int lower=input.nextInt();
System.out.print("Input upper bound : ");
int upper=input.nextInt();
System.out.print("[");
for (int i = lower; i<=upper; i++) {
System.out.print(i+", ");
}
System.out.println("\b\b]");
}
}

//////////////////////////////////////////////////////////////////////
Q220 Exercise 8
===============
package example;

import java.util.Scanner;

public class Example {


public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Input upper bound : ");
int upper=input.nextInt();
int total=0;
for (int i =1 ;i<=upper ; i++) {
System.out.print(i+" + ");
total+=i; //total=total+i;
}
System.out.println("\b\b\b = "+total);
}
}

//////////////////////////////////////////////////////////////////////
Q221 Exercise 9
===============
package example;
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Input an integer : ");
int num=input.nextInt();
int fact=1;
for (int i = num; i > 0; i--) {
System.out.print(i+" * ");
fact=fact*i;
}
System.out.println("\b\b\b : "+fact);
}
}

//////////////////////////////////////////////////////////////////////
Q222 Exercise 10
================
package example;
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Input student's marks : ");
int marks=input.nextInt();
int total=0;
for(; marks!=-1;){
total+=marks;
System.out.print("Input student's marks : ");
marks=input.nextInt();
}
System.out.println("Total : "+total);
}
}

//////////////////////////////////////////////////////////////////////
Q223 From Q222
==============
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Input student's marks : ");
int marks = input.nextInt();
int total = 0;
int count=0; //no of students
for (; marks != -1;) {
total += marks;
System.out.print("Input student's marks : ");
marks = input.nextInt();
count++;
}
double avg=(double)total/count;
System.out.println("Total : " + total);
System.out.println("Average : "+avg);
}
}

//////////////////////////////////////////////////////////////////////
Java Flow Control "while-loop"
//////////////////////////////////////////////////////////////////////
Q224
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Input student's marks : ");
int marks = input.nextInt();
int total = 0;
int count=0; //no of students
while(marks != -1) {
total += marks;
System.out.print("Input student's marks : ");
marks = input.nextInt();
count++;
}
double avg=(double)total/count;
System.out.println("Total : " + total);
System.out.println("Average : "+avg);
}
}

//////////////////////////////////////////////////////////////////////
Q225 From Exercise 1
====================
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


int i=0;
while(i<100){
System.out.println(i++);
//i++;
}
}
}

//////////////////////////////////////////////////////////////////////
Q226 From Exercise 2
====================

//////////////////////////////////////////////////////////////////////
Q227 From Exercise 3
====================

//////////////////////////////////////////////////////////////////////
Q228 From Exercise 4
====================

//////////////////////////////////////////////////////////////////////
Q229 From Exercise 5
====================

//////////////////////////////////////////////////////////////////////
Q230 From Exercise 6
====================

//////////////////////////////////////////////////////////////////////
Q231 Exercise
=============
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Input an integer : ");
int num = input.nextInt();

//Find digits count


int count=0;
//
//
//
System.out.println("No of digits : "+count);
}
}

//////////////////////////////////////////////////////////////////////
Q232 From Q231
==============
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Input an integer : ");
int num = input.nextInt();

//Find digits count


int count = 0;

while (num != 0) {
num = num / 10;
count++;
}
System.out.println("No of digits : " + count);
System.out.println("num : "+num);
}
}

//////////////////////////////////////////////////////////////////////
Java Flow Control "do/while"
//////////////////////////////////////////////////////////////////////
Q233
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Input an integer : ");
int num = input.nextInt();

//Find digits count


int count = 0;

do{
num = num / 10;
count++;
}while (num != 0) ;

System.out.println("No of digits : " + count);


}
}
//////////////////////////////////////////////////////////////////////
Q234
package example;
public class Example {
public static void main(String[] args) {
int i=0;
while(i<10){
System.out.println("while.. : "+i);
i++;
}
System.out.println("-------------");
int j=0;
do{
System.out.println("do/while.. : "+j);
j++;
}while(j<10);
}
}

//////////////////////////////////////////////////////////////////////
Q235 do/while vs while
======================
package example;
public class Example {
public static void main(String[] args) {
int i=10;
while(i<10){
System.out.println("while.. : "+i);
i++;
}
System.out.println("-------------");

int j=10;
do{
System.out.println("do/while.. : "+j);
j++;
}while(j<10);
}
}

//////////////////////////////////////////////////////////////////////
keyword "break", "continue"
//////////////////////////////////////////////////////////////////////
Q236
package example;
public class Example {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println("i : "+i);
if(i==5){
break;
}
}
}
}

//////////////////////////////////////////////////////////////////////
Q237
package example;

public class Example {

public static void main(String[] args) {


for (int i = 0; i < 100; i++) {
if (i == 5) {
break;
}
System.out.println("i : " + i);

}
}
}

//////////////////////////////////////////////////////////////////////
Q238
package example;
public class Example {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
if (i %2 ==0) {
continue;
}
System.out.println("i : " + i);
}
}
}
//////////////////////////////////////////////////////////////////////
Q239
package example;

public class Example {

public static void main(String args[]) {


for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.print("* ");
//System.out.println();
}
System.out.println();
}
//3System.out.println();
}
}

//////////////////////////////////////////////////////////////////////
Q240
package example;
public class Example {
//--------------Start myMethod-----------------
public static void myMethod(){
System.out.println("myMethod");
}
//--------------End myMethod-----------------

//--------------Start main method--------------


public static void main(String args[]) {
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
}
//--------------End main method-----------------
}

//////////////////////////////////////////////////////////////////////
Q241
package example;
public class Example {
//--------------Start myMethod-----------------
public static void myMethod(){
System.out.println("myMethod");
}
//--------------End myMethod-----------------

//--------------Start main method--------------


public static void main(String args[]) {
myMethod();

myMethod();
}
//--------------End main method-----------------
}

//////////////////////////////////////////////////////////////////////
Q242
package example;

public class Example {

//--------------Start main method--------------


public static void main(String args[]) {
System.out.println("Niroth Lakmal Samarawickrama,");
System.out.println("No 500/10, ");
System.out.println("Galle Rd, ");
System.out.println("Panadura.");

System.out.println("Niroth Lakmal Samarawickrama,");


System.out.println("No 500/10, ");
System.out.println("Galle Rd, ");
System.out.println("Panadura.");

System.out.println("Niroth Lakmal Samarawickrama,");


System.out.println("No 500/10, ");
System.out.println("Galle Rd, ");
System.out.println("Panadura.");
}
//--------------End main method-----------------
}

//////////////////////////////////////////////////////////////////////
Q243
package example;

public class Example {


//-----------------Start printMyAddress method declaration------
public static void printMyAddress(){
System.out.println("Niroth Lakmal Samarawickrama,");
System.out.println("No 500/10, ");
System.out.println("Galle Rd, ");
System.out.println("Panadura.");
}
//-----------------End printMyAddress---------------------------

//--------------Start main method--------------


public static void main(String args[]) {
printMyAddress(); //Method Calling statement

printMyAddress(); //Method Calling statement

printMyAddress(); //Method Calling statement


}
//--------------End main method-----------------
}

//////////////////////////////////////////////////////////////////////
Q244
package example;
class MyTask{
//-----------------Start printMyAddress method declaration------
public static void printMyAddress(){
System.out.println("Niroth Lakmal Samarawickrama,");
System.out.println("No 500/10, ");
System.out.println("Galle Rd, ");
System.out.println("Panadura.");
}
//-----------------End printMyAddress---------------------------
}
public class Example {

//--------------Start main method--------------


public static void main(String args[]) {
MyTask.printMyAddress(); //Method Calling statement
MyTask.printMyAddress(); //Method Calling statement

MyTask.printMyAddress(); //Method Calling statement


}
//--------------End main method-----------------
}

//////////////////////////////////////////////////////////////////////
Q245 Exercise (Parameterless method)
====================================
package example;

import java.util.Scanner;

public class Example {

public static void printTotal() {


Scanner input=new Scanner(System.in);

System.out.print("Input number 1 : ");


int num1=input.nextInt();
System.out.print("Input number 2 : ");
int num2=input.nextInt();

int total;
total=num1+num2;

System.out.println(num1+" + "+num2+" = "+total);


}

public static void main(String args[]) {


printTotal();
}
}

//////////////////////////////////////////////////////////////////////
Q246 Parameterized Methods
==========================
package example;

import java.util.Scanner;

public class Example {


//-------------------Parameterized method--------------
public static void printTotal(int a, int b) {
int total;
total=a+b;

System.out.println(a+" + "+b+" = "+total);


}
//-------------------Parameterized method--------------

public static void main(String args[]) {


Scanner input=new Scanner(System.in);

System.out.print("Input number 1 : ");


int num1=input.nextInt();
System.out.print("Input number 2 : ");
int num2=input.nextInt();

printTotal(num1,num2); //Methoda Calling Statement

System.out.print("Input number 1 : ");


num1=input.nextInt();
System.out.print("Input number 2 : ");
num2=input.nextInt();
printTotal(12,45);
}
}

//////////////////////////////////////////////////////////////////////
Q247 Exercise
=============
package example;

public class Example {


public static void printDigitsCount(int num){
int count=0;
do{
count++;
}while((num/=10)!=0);
System.out.println("Count : "+count);
}
public static void main(String args[]) {
printDigitsCount(1232);
printDigitsCount(10000);
}
}

//////////////////////////////////////////////////////////////////////
Q248 Exercise
=============
package example;

public class Example {


public static void printReverse(int num){
int reverse=0;

System.out.println("Reverse : "+reverse);
}
public static void main(String args[]) {
printReverse(1234); //4321
}
}

//////////////////////////////////////////////////////////////////////
Q249 Exercise
=============
package example;

public class Example {


public static void printStudentStatus(double average){
System.out.println(average>=50 ? "Pass":"Fail");
}
public static void main(String args[]) {
double avg=76.9;
printStudentStatus(avg);
printStudentStatus(49.999);
}
}

//////////////////////////////////////////////////////////////////////
Q250 Exercise
=============
package example;

public class Example {


public static void printStudentGrade(double average){
if(average>=75){
System.out.println("A");
}else if(average>=65){
System.out.println("B");
}else if(average>=45){
System.out.println("C");
} else if(average>=25){
System.out.println("D");
}else{
System.out.println("E");
}
}
public static void main(String args[]) {
double avg=76.9;
printStudentGrade(avg); //A
printStudentGrade(65.00);//B
printStudentGrade(45.00); //C
printStudentGrade(25.00); //D
printStudentGrade(10); //E
}
}

//////////////////////////////////////////////////////////////////////
Return types Methods
//////////////////////////////////////////////////////////////////////
Q251
package example;

import java.util.Scanner;

public class Example {


//-------------------Parameterized method--------------
public static int findTotal(int a, int b) {
int total;
total=a+b;
return total;
}
//-------------------Parameterized method--------------

public static void main(String args[]) {


Scanner input=new Scanner(System.in);

System.out.print("Input number 1 : ");


int num1=input.nextInt();
System.out.print("Input number 2 : ");
int num2=input.nextInt();

int tot;
tot=findTotal(num1,num2);
System.out.println(num1+" + "+num2+" = "+tot);
}
}

//////////////////////////////////////////////////////////////////////
Q252 Exercise
=============
package example;

public class Example {


public static void printDigitsCount(int num){
int count=0;
do{
count++;
}while((num/=10)!=0);
System.out.println("Count : "+count);
}
public static void main(String args[]) {
int num=1234;
int count=findDigitsCount(num);
System.out.println("No of digits for "+num+" : "+count);
}
}

//////////////////////////////////////////////////////////////////////
Q253 From Q252
==============
package example;

public class Example {


public static int findDigitsCount(int num){
int count=0;
do{
count++;
}while((num/=10)!=0);
return count;
}
public static void main(String args[]) {
int num=1234;
int count=findDigitsCount(num);
System.out.println("No of digits for "+num+" : "+count);
}
}

//////////////////////////////////////////////////////////////////////
Q254 Exercise
=============
package example;

import java.util.Scanner;

public class Example {


public static void printReverse(int num){
int reverse=0;
//
//
//
System.out.println("Reverse : "+reverse);
}
public static void main(String args[]) {
Scanner input=new Scanner(System.in);
System.out.print("Input an integer : ");
int num=input.nextInt();
int rev=reverse(num);
System.out.println("Reverse of "+num+" is "+rev);
}
}

//////////////////////////////////////////////////////////////////////
Q255 From Q254
==============

//////////////////////////////////////////////////////////////////////
Q256 Exercise
=============
package example;

public class Example {


public static void printStudentStatus(double average){
System.out.println(average>=50 ? "Pass":"Fail");
}
public static void main(String args[]) {
double avg=76.9;
if(isPass(avg)){
System.out.println("pass");
}else{
System.out.println("Fail");
}

}
}

//////////////////////////////////////////////////////////////////////
Q257 From Q256
==============
package example;

public class Example {


public static boolean isPass(double average){
boolean b;
if(average>=50){
b=true;
}else{
b=false;
}
return b;
}
public static void main(String args[]) {
double avg=76.9;
if(isPass(avg)){
System.out.println("pass");
}else{
System.out.println("Fail");
}
}
}
//---------------------------------------------------------------------
package example;

public class Example {


public static boolean isPass(double average){
boolean b;
b=average>=50 ? true : false;
return b;
}
public static void main(String args[]) {
double avg=76.9;
if(isPass(avg)){
System.out.println("pass");
}else{
System.out.println("Fail");
}
}
}

//---------------------------------------------------------------------
package example;

public class Example {


public static boolean isPass(double average){
if(average>=50){
return true;
}else{
return false;
}
}
public static void main(String args[]) {
double avg=76.9;
if(isPass(avg)){
System.out.println("pass");
}else{
System.out.println("Fail");
}
}
}

//---------------------------------------------------------------------
package example;

public class Example {


public static boolean isPass(double average){
return average>=50 ? true : false;
}
public static void main(String args[]) {
double avg=76.9;
if(isPass(avg)){
System.out.println("pass");
}else{
System.out.println("Fail");
}
}
}

//--------------------------------------------------------------------
package example;

public class Example {


public static boolean isPass(double average){
return average>=50;
}
public static void main(String args[]) {
double avg=76.9;
if(isPass(avg)){
System.out.println("pass");
}else{
System.out.println("Fail");
}
}
}

//////////////////////////////////////////////////////////////////////
Q258 Exerxcise
==============
package example;

public class Example {


public static void printStudentGrade(double average){
if(average>=75){
System.out.println("A");
}else if(average>=65){
System.out.println("B");
}else if(average>=45){
System.out.println("C");
} else if(average>=25){
System.out.println("D");
}else{
System.out.println("E");
}
}
public static void main(String args[]) {
double avg=76.9;
char grade;
grade=findStudentGrade(avg);
System.out.println(avg+" : "+grade);
}
}

//////////////////////////////////////////////////////////////////////
Q259 From Q258
==============
package example;

public class Example {


public static char findStudentGrade(double average){
char grade;
if(average>=75){
grade='A';
}else if(average>=65){
grade='B';
}else if(average>=45){
grade='C';
} else if(average>=25){
grade='D';
}else{
grade='E';
}
return grade;
}
public static void main(String args[]) {
double avg=76.9;
char grade;
grade=findStudentGrade(avg);
System.out.println(avg+" : "+grade);
}
}

//////////////////////////////////////////////////////////////////////
Method Calling Stack
//////////////////////////////////////////////////////////////////////
Q260 Case I (Parameterlessmethods)
==================================
package example;
public class Example {
public static void myMethod(){
System.out.println("Start myMethod");
int x=100;
System.out.println(x);
x++;
System.out.println(x);
System.out.println("End myMethod");
}
public static void main(String args[]) {
System.out.println("Start main");
int x=100;
System.out.println(x);
myMethod();
x++;
System.out.println(x);
System.out.println("End main");
}
}

//////////////////////////////////////////////////////////////////////
Q261 Exercise
=============
package example;
public class Example {
public static void increment(){
int x=100;
System.out.println(x);
x++;
System.out.println(x);
}
public static void main(String args[]) {
int x=100;
System.out.println(x);
increment();
System.out.println(x);
}
}

//////////////////////////////////////////////////////////////////////
Q262 Case II (Parameterized Methods)
====================================
package example;
public class Example {
public static void printTotal(int a, int b) {
int total;
total=a+b;
System.out.println(a+" + "+b+" = "+total);
}
public static void main(String args[]) {
int num1=100;
int num2=200;
printTotal(num1,num2);
System.out.println("End.");
}
}

//////////////////////////////////////////////////////////////////////
Q263 Exercise
=============
package example;
public class Example {
public static void increment(int x){
System.out.println(x);//100
x++;
System.out.println(x);//101
}
public static void main(String args[]) {
int x=100;
System.out.println(x); //100
increment(x);
System.out.println(x); //100
}
}

//////////////////////////////////////////////////////////////////////
Q264 Case III (Return types Methods)
====================================
package example;

public class Example {


public static int getTotal(int a, int b) {
int total;
total=a+b;
return total;
}
public static void main(String args[]) {
int num1=100;
int num2=200;
int total;
total=getTotal(num1,num2);
System.out.println("Total : "+total);
}
}

//////////////////////////////////////////////////////////////////////
Q265 Exercise
=============
package example;
public class Example {
public static int increment(int x){
System.out.println(x);
x++;
System.out.println(x);
return x;
}
public static void main(String args[]) {
int x=100;
System.out.println(x);
x=increment(x);
System.out.println(x);
}
}

//////////////////////////////////////////////////////////////////////
Q266
package example;

public class Example {

public static int fact(int num) {


return num == 0 ? 1 : num * fact(num - 1);
}

public static void main(String args[]) {


for (int i = 0; i < 10; i++) {
System.out.println(i + " ! " + fact(i));
}
}
}

//////////////////////////////////////////////////////////////////////
rules for method declarations
//////////////////////////////////////////////////////////////////////
Q267 Case I
===========
package example;
public class Example {
public static void myMethod(){
System.out.println("myMethod");
}
public static void main(String args[]) {
//myMethod();
}
}

//////////////////////////////////////////////////////////////////////
Q268 Case II
============
package example;
public class Example {

public static void main(String args[]) {


myMethod();
}
public static void myMethod(){
System.out.println("myMethod");
}
}

//////////////////////////////////////////////////////////////////////
Q269 Case III
=============
package example;
public class Example {

public static void main(String args[]) {


public static void myMethod(){ //Illegal
System.out.println("myMethod");
}
myMethod();
}
}

//////////////////////////////////////////////////////////////////////
Q270 Case IV
============
package example;

public class Example {

public static void myMethod(int x) {


System.out.println("myMethod");
}
public static void main(String args[]) {
//myMethod(); //parameterless (default method) //Illegal
myMethod(100);
long y=1000;
//myMethod(y); //Illegal
short s=100;
char ch='A';
myMethod(s); //Legal
myMethod(ch); //Legal
}
}

//////////////////////////////////////////////////////////////////////
Q271 Caase V
============
package example;

public class Example {

public static void myMethod(int x,y) { //Illegal


int a,b; //Legal
System.out.println("myMethod");
}
public static void main(String args[]) {

}
}

//////////////////////////////////////////////////////////////////////
Q272 Case VI
============
package example;

public class Example {

public static void myMethod(int x=10,int y=30) { //Illegal


int a=10,b=20; //LEgal
System.out.println("myMethod");
}
public static void main(String args[]) {

}
}

//////////////////////////////////////////////////////////////////////
Q273 Case VII
=============
package example;

public class Example {

public static void myMethod(int x,int y) {


int a=10,b=20;
int x=100;
System.out.println("myMethod");
}
public static void main(String args[]) {

}
}

//////////////////////////////////////////////////////////////////////
Q274 Case VIII
=============
package example;
import java.util.*;
class Example{
public static double average(int total, int n){
double avg=(double)total/n;
return avg;
}
public static void main(String args[]){
int totalMarks=899;
int no_of_students=10;
double avg=average(no_of_students,totalMarks);
System.out.println("Average : "+avg);

avg=average(totalMarks,no_of_students);
System.out.println("Average : "+avg);
}
}

//////////////////////////////////////////////////////////////////////
Q275 Exercise
=============
package example;
import java.util.*;
class Example{
public static void main(String args[]){
double d;
d=Math.pow(7,2); //Wrong method call -(arguments)
System.out.println("2^7 : "+d);

d=Math.pow(2,7); //
System.out.println("2^7 : "+d);
}
}

//////////////////////////////////////////////////////////////////////
Q276 Case IX
============
package example;
import java.util.*;
class Example{
public static void myMethod(int x){
System.out.println("A");
System.out.println("B");
if(x<50){return;}
System.out.println("C");
System.out.println("D");
}
public static void main(String args[]){
myMethod(49);
System.out.println("============");
myMethod(100);
}
}

//////////////////////////////////////////////////////////////////////
Q277 Exercise
=============
package example;
import java.util.*;
class Example{
public static void myMethod(int x){
System.out.println("A");
System.out.println("B");
return;
System.out.println("C");
System.out.println("D");
}
public static void main(String args[]){
myMethod(49);
System.out.println("============");
myMethod(100);
}
}

//////////////////////////////////////////////////////////////////////
Q278 Case X
===========
package example;
import java.util.*;
class Example{
public static void printTotal(int x,int y){
int z;
z=x+y;
System.out.println(x+" + "+y+" = "+z);
return z;
}

public static void main(String args[]){

}
}

//////////////////////////////////////////////////////////////////////
Q279 Case XI
============
package example;
import java.util.*;
class Example{
public static int getTotal(int x,int y){
int z;
z=x+y;
System.out.println(x+" + "+y+" = "+z);
}

public static void main(String args[]){

}
}

//////////////////////////////////////////////////////////////////////
Q280 Case XII
=============
package example;

import java.util.*;

class Example {

public static int getTotal(int x, int y) {


int z;
z = x + y;
System.out.println(x + " + " + y + " = " + z);
return z;
System.out.println("end"); //Illegal
}

public static void main(String args[]) {

}
}

//////////////////////////////////////////////////////////////////////
Q281 Case XIII
==============
package example;

class Example {
public static int getTotal(int x, int y) {
double total;
total = x + y;
return total; //Illegal
}

public static void main(String args[]) {

}
}

//////////////////////////////////////////////////////////////////////
Q282 Case XIV
=============
package example;
class Example{
public static char findGrade(double avg){
if(avg>=75){
return 'A';
}else if(avg>=65){
return 'B';
}else if(avg>=55){
return 'C';
}else if(avg>=45){
return 'D';
}
}

public static void main(String args[]){

}
}

//////////////////////////////////////////////////////////////////////
Q283 Option I
=============
package example;

class Example {

public static char findGrade(double avg) {


if (avg >= 75) {
return 'A';
} else if (avg >= 65) {
return 'B';
} else if (avg >= 55) {
return 'C';
} else if (avg >= 45) {
return 'D';
}else{
return 'E';
}
}

public static void main(String args[]) {

}
}

//////////////////////////////////////////////////////////////////////
Q284 Option II for Q282
=======================
package example;

class Example {

public static char findGrade(double avg) {


if (avg >= 75) {
return 'A';
} else if (avg >= 65) {
return 'B';
} else if (avg >= 55) {
return 'C';
} else if (avg >= 45) {
return 'D';
}
return 'E';

public static void main(String args[]) {

}
}
//////////////////////////////////////////////////////////////////////
Q285 Option III for Q282 (Illegal)
==================================
package example;

class Example {

public static char findGrade(double avg) {


if (avg >= 75) {
return 'A';
} else if (avg >= 65) {
return 'B';
} else if (avg >= 55) {
return 'C';
} else if (avg >= 45) {
return 'D';
}else{
return 'E';
}
return 'F'; //Illegal
}

public static void main(String args[]) {

}
}

//////////////////////////////////////////////////////////////////////
Q286 Case XV
============
package example;
import java.util.*;
class Example{
public static int findTotal(int x, int y){
int z=x+y;
return z;
}
public static void printTotal(int x, int y){
int z=x+y;
System.out.println("Total : "+z);
}
public static void main(String args[]){
findTotal(10,20); //Line 1 ->Legal
printTotal(10,20); //Line 2 ->Legal
}
}

//////////////////////////////////////////////////////////////////////
Q287 Case XVI
=============
package example;
class Example{
public static int findTotal(int x, int y){
int z=x+y;
return z;
}
public static void printTotal(int x, int y){
int z=x+y;
System.out.println("Total : "+z);
}
public static void main(String args[]){
int tot;
tot=findTotal(10,20); //Line 1
tot=printTotal(10,20); //Line 2
System.out.println(findTotal(10,20)); //Line 3
System.out.println(printTotal(10,20));//Line 4
}
}

//////////////////////////////////////////////////////////////////////
Method Overloading
//////////////////////////////////////////////////////////////////////
Q288 Exercise
=============
package example;
import java.util.*;
class MyClass{

class Example{
public static void main(String args[]){
int x=-100;
long y=-200;
float f=300.0f;
double d=400.0;
int absX=MyClass.abs(x);
long absY=MyClass.abs(y);
float absF=MyClass.abs(f);
double absD=MyClass.abs(d);

System.out.println("absolute value of "+x+" is : "+absX); //100


System.out.println("absolute value of "+y+" is : "+absY); //200
System.out.println("absolute value of "+f+" is : "+absF); //300.0
System.out.println("absolute value of "+d+" is : "+absD); //400.0
}
}

//////////////////////////////////////////////////////////////////////
Q289
package example;

class MyClass {
public static int abs(int num){
return num<0 ? -num : num;
}
public static long abs(long num){
return num<0 ? -num : num;
}
public static float abs(float num){
return num<0 ? -num : num;
}
public static double abs(double num){
return num<0 ? -num : num;
}
}

class Example {

public static void main(String args[]) {


int x = -100;
long y = -200;
float f = 300.0f;
double d = 400.0;

int absX = MyClass.abs(x);


long absY = MyClass.abs(y);
float absF = MyClass.abs(f);
double absD = MyClass.abs(d);

System.out.println("absolute value of " + x + " is : " + absX); //100


System.out.println("absolute value of " + y + " is : " + absY); //200
System.out.println("absolute value of " + f + " is : " + absF); //300.0
System.out.println("absolute value of " + d + " is : " + absD); //400.0
}
}

//////////////////////////////////////////////////////////////////////
Q290
package example;
public class Example {
public static void myMethod(int x){
System.out.println("myMethod(int)");
}
public static void myMethod(int x){
System.out.println("myMethod(int)");
}
public static void main(String args[]) {
myMethod(100);
}
}

//////////////////////////////////////////////////////////////////////
Q291 From Q289
==============
package example;

class MyClass {
/*
public static int abs(int num){
return num<0 ? -num : num;
}
public static long abs(long num){
return num<0 ? -num : num;
}
public static float abs(float num){
return num<0 ? -num : num;
} */
public static double abs(double num){
return num<0 ? -num : num;
}
}

class Example {

public static void main(String args[]) {


int x = -100;
long y = -200;
float f = 300.0f;
double d = 400.0;

System.out.println("absolute value of " + x + " is : " + MyClass.abs(x)); //100.0


System.out.println("absolute value of " + y + " is : " + MyClass.abs(y)); //200.0
System.out.println("absolute value of " + f + " is : " + MyClass.abs(f)); //300.0
System.out.println("absolute value of " + d + " is : " + MyClass.abs(d)); //400.0
}
}

//////////////////////////////////////////////////////////////////////
Q292 Case I
===========
package example;

public class Example {

public static void myMethod(int x) {


System.out.println("myMethod(int)");
}

public static void myMethod(double x) {


System.out.println("myMethod(double)");
}

public static void main(String args[]) {


myMethod(100);
myMethod(100.0);
}
}

//////////////////////////////////////////////////////////////////////
Q293 Case II
============
package example;
public class Example {

public static void myMethod(int x) {


System.out.println("myMethod(int)");
}

public static void myMethod(int x, int y) {


System.out.println("myMethod(int,int)");
}

public static void main(String args[]) {


myMethod(100);
myMethod(100,200);
}
}

//////////////////////////////////////////////////////////////////////
Q294 Case III
=============
package example;

public class Example {

public static void myMethod(int x,double y) {


System.out.println("myMethod(int,double)");
}

public static void myMethod(double x, int y) {


System.out.println("myMethod(double,int)");
}

public static void main(String args[]) {


myMethod(100,100.0);
myMethod(100.0,200);
}
}

//////////////////////////////////////////////////////////////////////
Q295 Case IV
============
package example;
public class Example {

public static void myMethod(int age) {


System.out.println("myMethod(int-age)");
}

public static void myMethod(int total) {


System.out.println("myMethod(int-totl)");
}

public static void main(String args[]) {

}
}
//////////////////////////////////////////////////////////////////////
Q296 Case V
===========
package example;

public class Example {

public static int myMethod() {


System.out.println("myMethod()");
return 100;
}

public static double myMethod() {


System.out.println("myMethod()");
return 1.23;
}

public static void main(String args[]) {

}
}

//////////////////////////////////////////////////////////////////////
Q297 Case VI
============
package example;

public class Example {


public static void myMethod(int x,double y) {
System.out.println("myMethod(int,double)");
}

public static void myMethod(double x, int y) {


System.out.println("myMethod(double,int)");
}

public static void main(String args[]) {


myMethod(100, 100);
}
}

//////////////////////////////////////////////////////////////////////
Q298 From Q297
==============
package example;

public class Example {

public static void myMethod(int x,double y) {


System.out.println("myMethod(int,double)");
}
public static void myMethod(int x,int y) {
System.out.println("myMethod(int,int)");
}
public static void myMethod(double x, int y) {
System.out.println("myMethod(double,int)");
}

public static void main(String args[]) {


myMethod(100, 100);
}
}

//////////////////////////////////////////////////////////////////////
Q299 Case VII
=============
package example;
class Example{
public static void myMethod(float x){
System.out.println("myMethod(float)");
}
/*
public static void myMethod(int x){
System.out.println("myMethod(int)");
}
*/
public static void myMethod(long x){
System.out.println("myMethod(long)");
}
public static void myMethod(double d){
System.out.println("myMethod(double)");
}
public static void main(String args[]){
int x=200;
myMethod(x);
}
}

//////////////////////////////////////////////////////////////////////
Arrays
//////////////////////////////////////////////////////////////////////
Q300 Exercise
=============
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int x0, x1, x2, x3, x4, x5, x6, x7, x8, x9;
System.out.print("Input number 1 : ");
x0 = input.nextInt();

System.out.print("Input number 2 : ");


x1 = input.nextInt();

System.out.print("Input number 3 : ");


x2 = input.nextInt();

System.out.print("Input number 4 : ");


x3 = input.nextInt();
System.out.print("Input number 5 : ");
x4 = input.nextInt();

System.out.print("Input number 6 : ");


x5 = input.nextInt();

System.out.print("Input number 7 : ");


x6 = input.nextInt();

System.out.print("Input number 8 : ");


x7 = input.nextInt();

System.out.print("Input number 9 : ");


x8 = input.nextInt();

System.out.print("Input number 10: ");


x9 = input.nextInt();

int total = 0;
total += x0;
total += x1;
total += x2;
total += x3;
total += x4;
total += x5;
total += x6;
total += x7;
total += x8;
total += x9;

int max=x0;
if(x1>max){
max=x1;
}
if(x2>max){
max=x2;
}
if(x3>max){
max=x3;
}
if(x4>max){
max=x4;
}
if(x5>max){
max=x5;
}
if(x6>max){
max=x6;
}
if(x7>max){
max=x7;
}
if(x8>max){
max=x8;
}
if(x9>max){
max=x9;
}

System.out.println("Total : " + total);


System.out.println("Max : " + max);
}
}

//////////////////////////////////////////////////////////////////////
Q301 From Q300 Using an int array
=================================
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int[] x=new int[10];//int x0, x1, x2, x3, x4, x5, x6, x7, x8, x9;

System.out.print("Input number 1 : ");


x[0] = input.nextInt();

System.out.print("Input number 2 : ");


x[1] = input.nextInt();

System.out.print("Input number 3 : ");


x[2] = input.nextInt();
System.out.print("Input number 4 : ");
x[3] = input.nextInt();

System.out.print("Input number 5 : ");


x[4] = input.nextInt();

System.out.print("Input number 6 : ");


x[5] = input.nextInt();

System.out.print("Input number 7 : ");


x[6] = input.nextInt();

System.out.print("Input number 8 : ");


x[7] = input.nextInt();

System.out.print("Input number 9 : ");


x[8] = input.nextInt();

System.out.print("Input number 10: ");


x[9] = input.nextInt();

//
int total = 0;
total += x[0];
total += x[1];
total += x[2];
total += x[3];
total += x[4];
total += x[5];
total += x[6];
total += x[7];
total += x[8];
total += x[9];

int max=x[0];

if(x[1]>max){
max=x[1];
}
if(x[2]>max){
max=x[2];
}
if(x[3]>max){
max=x[3];
}
if(x[4]>max){
max=x[4];
}
if(x[5]>max){
max=x[5];
}
if(x[6]>max){
max=x[6];
}
if(x[7]>max){
max=x[7];
}
if(x[8]>max){
max=x[8];
}
if(x[9]>max){
max=x[9];
}

System.out.println("Total : " + total);


System.out.println("Max : " + max);
//System.out.println(x0+" "+x1+" "+x2);
}
}

//////////////////////////////////////////////////////////////////////
Q302 From Q301 Using array iteration
====================================
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int[] x=new int[10]; //int x0, x1, x2, x3, x4, x5, x6, x7, x8, x9;
for (int i = 0; i < 10; i++) {
System.out.print("Input number "+(i+1)+" : ");
x[i] = input.nextInt();
}
/*
System.out.print("Input number 2 : ");
x[1] = input.nextInt();

System.out.print("Input number 3 : ");


x[2] = input.nextInt();

System.out.print("Input number 4 : ");


x[3] = input.nextInt();

System.out.print("Input number 5 : ");


x[4] = input.nextInt();

System.out.print("Input number 6 : ");


x[5] = input.nextInt();

System.out.print("Input number 7 : ");


x[6] = input.nextInt();

System.out.print("Input number 8 : ");


x[7] = input.nextInt();

System.out.print("Input number 9 : ");


x[8] = input.nextInt();

System.out.print("Input number 10: ");


x[9] = input.nextInt();
*/
//
//Find total
int total = 0;
for (int i = 0; i < 10; i++) {
total += x[i];
}
/*
total += x[1];
total += x[2];
total += x[3];
total += x[4];
total += x[5];
total += x[6];
total += x[7];
total += x[8];
total += x[9];
*/

//Find the max


int max=x[0];
for (int i = 1; i < 10; i++) {
if(x[i]>max){
max=x[i];
}
}
/*
if(x[2]>max){
max=x[2];
}
if(x[3]>max){
max=x[3];
}
if(x[4]>max){
max=x[4];
}
if(x[5]>max){
max=x[5];
}
if(x[6]>max){
max=x[6];
}
if(x[7]>max){
max=x[7];
}
if(x[8]>max){
max=x[8];
}
if(x[9]>max){
max=x[9];
}
*/
System.out.println("Total : " + total);
System.out.println("Max : " + max);
//System.out.println(x0+" "+x1+" "+x2);
}
}
//////////////////////////////////////////////////////////////////////
Q303 From Q302
==============
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int[] x=new int[10];

//Read data
for (int i = 0; i < 10; i++) {
System.out.print("Input number "+(i+1)+" : ");
x[i] = input.nextInt();
}

//Find total
int total = 0;
for (int i = 0; i < 10; i++) {
total += x[i];
}

//Find the max


int max=x[0];
for (int i = 1; i < 10; i++) {
if(x[i]>max){
max=x[i];
}
}

//Find min

//Print the result


System.out.println("Total : " + total);
System.out.println("Max : " + max);

}
}

//////////////////////////////////////////////////////////////////////
Q304 from Q303
==============
package example;

import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int[] x=new int[10];

//Read data
for (int i = 0; i < 10; i++) {
System.out.print("Input number "+(i+1)+" : ");
x[i] = input.nextInt();
}

//Find total
int total = 0;
for (int i = 0; i < 10; i++) {
total += x[i];
}

//Find the max


int max=x[0];
for (int i = 1; i < 10; i++) {
if(x[i]>max){
max=x[i];
}
}

//Find the min


int min=x[0];
for (int i = 1; i < 10; i++) {
if(x[i]<min){
min=x[i];
}
}

//Print the result


//print data [21,34,65,34,65,78,68,45....]
System.out.print("[");
for (int i = 0; i < 10; i++) {
System.out.print(x[i]+", ");
}
System.out.println("\b\b]");
System.out.println("Total : " + total);
System.out.println("Max : " + max);
System.out.println("Min : " + min);

}
}

//////////////////////////////////////////////////////////////////////
Q305 From Q304 (Read random number)
===================================
package example;

import java.util.Random;
import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int[] x=new int[10];

//Read data
Random r=new Random();
for (int i = 0; i < 10; i++) {
//System.out.print("Input number "+(i+1)+" : ");
x[i] = r.nextInt(101); //0 to 100
}

//Find total
int total = 0;
for (int i = 0; i < 10; i++) {
total += x[i];
}

//Find the max


int max=x[0];
for (int i = 1; i < 10; i++) {
if(x[i]>max){
max=x[i];
}
}

//Find the min


int min=x[0];
for (int i = 1; i < 10; i++) {
if(x[i]<min){
min=x[i];
}
}

//Print the result


//print data [21,34,65,34,65,78,68,45....]
System.out.print("[");
for (int i = 0; i < 10; i++) {
System.out.print(x[i]+", ");
}
System.out.println("\b\b]");
System.out.println("Total : " + total);
System.out.println("Max : " + max);
System.out.println("Min : " + min);
}
}

//////////////////////////////////////////////////////////////////////
Q306 Case I
==========
package example;
public class Example {
public static void main(String[] args) {
int x;
x=100;
System.out.println(x);

int[] xr; //Create an array reference


xr=new int[5]; //create an array Object
System.out.println(xr);
xr[0]=100;
System.out.println(xr[0]);
}
}

//////////////////////////////////////////////////////////////////////
Q307 Case II
============
package example;
public class Example {
public static void main(String[] args) {
int[] xr; //Array Declaration
xr=new int[5]; //Array Construction
xr[0]=21; //Array Initialization
xr[1]=34;
xr[2]=334;
}
}

//////////////////////////////////////////////////////////////////////
Q308 Case III
============
package example;
public class Example {
public static void main(String[] args) {
int[] a; //Legal
int []b; //Legal
int c[]; //Legal
//int d[4]; //Illegal
}
}

//////////////////////////////////////////////////////////////////////
Q309 Case IV Default Values
===========================
package example;
public class Example {
public static void main(String[] args) {
int x;
//System.out.println(x); //Illegal

int[] a=new int[3];


System.out.println(a[0]+" "+a[1]+" "+a[2]);

long[] b=new long[3];


System.out.println(b[0]+" "+b[1]+" "+b[2]);

float[] c=new float[3];


System.out.println(c[0]+" "+c[1]+" "+c[2]);
double[] d=new double[3];
System.out.println(d[0]+" "+d[1]+" "+d[2]);

boolean[] e=new boolean[3];


System.out.println(e[0]+" "+e[1]+" "+e[2]);
}
}

//////////////////////////////////////////////////////////////////////
Q310 Default Value of char (Null Character - ASCII 0)
=====================================================
package example;
public class Example {
public static void main(String[] args) {
char[] ch=new char[3];
System.out.println(ch[0]+" "+ch[1]+" "+ch[2]);

int code=ch[0];
System.out.println("ch[0] : "+code);
}
}

//////////////////////////////////////////////////////////////////////
Q311 Default Value of a Class type (null)
========================================
package example;

public class Example {


public static void main(String[] args) {
String[] names=new String[3];
System.out.println(names[0]+" "+names[1]+" "+names[2]);

}
}

//////////////////////////////////////////////////////////////////////
Q312 Case V
===========
package example;

public class Example {


public static void main(String[] args) {
int[] a=new int[5];
//int[] b=new int[];

int[] c={43,56,78,90,4};
System.out.println(c[0]+" "+c[1]+" "+c[2]);

int[] d;
//d={43,56,78,90,4}; //Illegal

int[] e=new int[]{43,56,78,90,4};


int[] f;
f=new int[]{43,56,78,90,4}; //Legal
}
}

//////////////////////////////////////////////////////////////////////
Q313 Case VI (Length of an array)
=================================
package example;
public class Example {
public static void main(String[] args) {
int[] a=new int[5];
System.out.println("length (no of elements) : "+a.length );

int[] c={43,56,78,90,4,56,78,45,56};
//insert code to print array [43,56,78,90,4,56,78,45,56]

}
}

//////////////////////////////////////////////////////////////////////
Q314 From Q313
==============
package example;
public class Example {
public static void main(String[] args) {
int[] a=new int[5];
System.out.println("length (no of elements) : "+a.length );

int[] c={43,56,78,90,4,56,78,45,56};
//insert code to print array [43,56,78,90,4,56,78,45,56]
for (int i = 0; i < c.length; i++) {
System.out.print(c[i]+" ");
}
}
}

//////////////////////////////////////////////////////////////////////
Q315 Case VII (Index error)
===========================
package example;
public class Example {
public static void main(String[] args) {
int[] a=new int[3];
System.out.println("length (no of elements) : "+a.length );

a[0]=233;
a[1]=344;
a[2]=353;
a[3]=343; //Runtime error (Exception -->ArrayIndexOutOfBoundsException)
a[6]=353;

a[-12]=454;

}
}

//////////////////////////////////////////////////////////////////////
Q316 Exercis
============
package example;
public class Example {
public static void main(String[] args) {

int[] c={43,56,78,90,4,56,78,45,56};

for (int i = 0; i <12; i++) {


System.out.print(c[i]+" ");
}
}
}

//////////////////////////////////////////////////////////////////////
Q317 Case VIII
==============
package example;
public class Example {
public static void main(String[] args) {
int[] a={43,45,65,67,89};
System.out.println(a[0]+" "+a[1]+" "+a[2]+" "+a[3]+" "+a[4]);

a=new int[]{1,2,3,4,5,6};
System.out.println(a[0]+" "+a[1]+" "+a[2]+" "+a[3]+" "+a[4]);
}
}

//////////////////////////////////////////////////////////////////////
Q318 Case IX
============
package example;
public class Example {
public static void main(String[] args) {
int[] a={43,45,65,67,89};
System.out.println(a[0]+" "+a[1]+" "+a[2]+" "+a[3]+" "+a[4]);

int[] b={43,45,65,67,89};
System.out.println(b[0]+" "+b[1]+" "+b[2]+" "+b[3]+" "+b[4]);

System.out.println(a==b); //false

int[] c=a;
System.out.println(a==c); //true
}
}

//////////////////////////////////////////////////////////////////////
Q319 Case X
===========
package example;
public class Example {
public static void main(String[] args) {
int[] xr=new int[4];
double[] dr=new double[4];

dr[0]=xr[0]; //Legal
//xr[0]=dr[0];
xr[0]=(int) dr[0]; //Legal

dr=xr; //Illegal
xr=dr; //Illegal
}
}

//////////////////////////////////////////////////////////////////////
Q320 Case XI (for-each)
=======================
package example;
public class Example {
public static void main(String[] args) {
int[] ar={12,32,34,54,56,76,78,98};
for (int i = 0; i < ar.length; i++) {
System.out.print(ar[i]+" ");
}
System.out.println();

for (int b : ar) {


System.out.print(b+" ");
}
}
}

//////////////////////////////////////////////////////////////////////
Q321 Case XII (Passing an array to a method)
============================================
package example;
public class Example {
public static void printArray(int[] a){
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
}
public static void main(String[] args) {
int[] ar={12,32,34,54,56,76,78,98};
printArray(ar);
}
}

//////////////////////////////////////////////////////////////////////
Q322 Exercise
=============
package example;
import java.util.Random;
import java.util.Scanner;

public class Example {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int[] x=new int[10];

//Read data
readArray(x);

//Find total
int total;
total=findTotal(x);

//Find the max


int max;
max=findMax(x);

//Find the min


int min;
min=findMin(x);

printArray(x)

System.out.println("Total : " + total);


System.out.println("Max : " + max);
System.out.println("Min : " + min);
}
}

//////////////////////////////////////////////////////////////////////
Q323 Exercise
=============
package example;
class Example {
public static void increment(int x, int y, int z){
x++;
y++;
z++;
}
public static void increment(int x[]){
x[0]++;
x[1]++;
x[2]++;
}
public static void main(String args[]) {
int[] x={100,200,300};
System.out.println(x[0]+" "+x[1]+" "+x[2]);
increment(x[0],x[1],x[2]);
System.out.println(x[0]+" "+x[1]+" "+x[2]);
increment(x);
System.out.println(x[0]+" "+x[1]+" "+x[2]);
}
}

//////////////////////////////////////////////////////////////////////
Q324 Exercise (for-loop vs foreach)
==================================
package example;
class Example {
public static void incrementForEachLoop(int xr[]){
for (int a : xr) {
a++;
}
}
public static void incrementTradiForLoop(int xr[]){
for (int i = 0; i < xr.length; i++) {
xr[i]++;
}
}
public static void main(String args[]) {
int[] x={100,200,300};
System.out.println(x[0]+" "+x[1]+" "+x[2]);
incrementForEachLoop(x);
System.out.println(x[0]+" "+x[1]+" "+x[2]);
incrementTradiForLoop(x);
System.out.println(x[0]+" "+x[1]+" "+x[2]);
}
}

////////////////////////////////////////////////////////--------------11.17.2018---------------/////////////////////////////////////
Q325 From Q324
==============
package example;
class Example {
public static void main(String args[]) {
int[] marks={100,200,300};
System.out.println(marks[0]+" "+marks[1]+" "+marks[2]);
for (int a : marks) {
a++;
}
System.out.println(marks[0]+" "+marks[1]+" "+marks[2]);
}
}

//////////////////////////////////////////////////////////////////////
Array Searching
//////////////////////////////////////////////////////////////////////
Q326 Exercise
=============
package example;
class Example {
public static void main(String args[]) {
int[] xr={1,0,2,9,3,8,3,7,4,6,5,2,8};
int index;
index=searchArray(xr, 9);
System.out.println("Index of 9 : "+index); //3

index=searchArray(xr, 11);
System.out.println("Index of 11: "+index); //-1
}
}

//////////////////////////////////////////////////////////////////////
Q327 From Q326
==============
package example;
class Example {
public static int searchArray(int[] x, int key){
int index=-1;
for (int i = 0; i < x.length; i++) {
if(x[i]==key){
index=i;
}
}
return index;
}
public static void main(String args[]) {
int[] xr={1,0,2,9,3,8,3,7,4,6,5,2,8};
int index;
index=searchArray(xr, 9);
System.out.println("Index of 9 : "+index); //3

index=searchArray(xr, 11);
System.out.println("Index of 11: "+index); //-1
}
}

//////////////////////////////////////////////////////////////////////
Q328 (Last index of)
====================
package example;
class Example {
public static int searchArray(int[] x, int key){
int index=-1;
for (int i = 0; i < x.length; i++) {
if(x[i]==key){
index=i;
}
}
return index;
}
public static void main(String args[]) {
int[] xr={1,0,2,9,3,8,3,7,4,6,5,2,8};
int index;
index=searchArray(xr, 8);
System.out.println("Index of 8 : "+index); //5 12
}
}

//////////////////////////////////////////////////////////////////////
Q329 From Q327 (First index of)
===============================
package example;
class Example {
public static int searchArray(int[] x, int key){
int index=-1;
for (int i = 0; i < x.length; i++) {
if(x[i]==key){
index=i;
break;
}
}
return index;
}
public static void main(String args[]) {
int[] xr={1,0,2,9,3,8,3,7,4,6,5,2,8};
int index;
index=searchArray(xr, 8);
System.out.println("Index of 8 : "+index); //5 12
}
}

//////////////////////////////////////////////////////////////////////
Q330 From Q329 (first index of)
==============================
package example;
class Example {
public static int searchArray(int[] x, int key){
for (int i = 0; i < x.length; i++) {
if(x[i]==key){
return i;
}
}
return -1;
}
public static void main(String args[]) {
int[] xr={1,0,2,9,3,8,3,7,4,6,5,2,8};
int index;
index=searchArray(xr, 8);
System.out.println("Index of 8 : "+index); //5 12
}
}

//////////////////////////////////////////////////////////////////////
Q331 Exercise
=============
package example;
class Example {

public static void main(String args[]) {


int[] xr={1,0,2,9,3,8,3,7,4,6,5,2,8};
boolean exists=searchArray(xr, 8);
System.out.println(" 8 : "+exists); //true

exists=searchArray(xr, 11);
System.out.println(" 11 : "+exists); //false
}
}

//////////////////////////////////////////////////////////////////////
Q332 From Q331
==============
package example;
class Example {
public static boolean searchArray(int []xr, int key){
for (int y : xr) {
if(y==key){
return true;
}
}
return false;
}
public static void main(String args[]) {
int[] xr={1,0,2,9,3,8,3,7,4,6,5,2,8};
boolean exists=searchArray(xr, 8);
System.out.println(" 8 : "+exists); //true

exists=searchArray(xr, 11);
System.out.println(" 11 : "+exists); //false
}
}

//////////////////////////////////////////////////////////////////////
Reverse Array
//////////////////////////////////////////////////////////////////////
Q333
package example;
class Example {
public static void printArray(int[] xr){
System.out.print("[");
for (int y : xr) {
System.out.print(y+", ");
}
System.out.println("\b\b]");
}
//Insert code to reverse an array
public static void main(String args[]) {
int[] xr={1,0,2,9,3,8,3,7,4,6,5,2,8};
printArray(xr); //[1,0,2,9,3,8,3,7,4,6,5,2,8]
reverseArray(xr);
printArray(xr); //[8,2,5,6,4,7,4,8,3,9,2,0,1]
}
}

//////////////////////////////////////////////////////////////////////
Q334 From Q333
==============

//////////////////////////////////////////////////////////////////////
Class Arrays (java.util.Arrays)
//////////////////////////////////////////////////////////////////////
Q335
package example;

import java.util.Arrays;

class Example {
public static void main(String args[]) {
int[] xr={1,0,2,9,3,8,3,7,4,6,5,2,8,4};
String toString = Arrays.toString(xr);
System.out.println(toString);
Arrays.sort(xr);
System.out.println(Arrays.toString(xr));
}
}

//////////////////////////////////////////////////////////////////////
Array Sorting (Bubble Sort)
//////////////////////////////////////////////////////////////////////
Q336 Step I
===========
package example;

import java.util.Arrays;
class Example {
public static void sortArray(int[] xr){
}
public static void main(String args[]) {
int[] xr={99,11,33,66,55,44,77,33,22,88,0};
System.out.println(Arrays.toString(xr));
sortArray(xr);
System.out.println(Arrays.toString(xr));
}
}

//////////////////////////////////////////////////////////////////////
Q337 Step II
===========
package example;

import java.util.Arrays;
class Example {
public static void sortArray(int[] x){
if(x[0]>x[1]){
int t=x[0];
x[0]=x[1];
x[1]=t;
}
if(x[1]>x[2]){
int t=x[1];
x[1]=x[2];
x[2]=t;
}
if(x[2]>x[3]){
int t=x[2];
x[2]=x[3];
x[3]=t;
}
if(x[3]>x[4]){
int t=x[3];
x[3]=x[4];
x[4]=t;
}
if(x[4]>x[5]){
int t=x[4];
x[4]=x[5];
x[5]=t;
}
}
public static void main(String args[]) {
int[] xr={99,11,33,66,55,44,77,33,22,88,0};
System.out.println(Arrays.toString(xr));
sortArray(xr);
System.out.println(Arrays.toString(xr));
}
}

//////////////////////////////////////////////////////////////////////
Q338 Step III (Runtime Error)
=============================
package example;

import java.util.Arrays;
class Example {
public static void sortArray(int[] x){
for(int i=0; i<x.length;i++){
if(x[i]>x[i+1]){
int t=x[i];
x[i]=x[i+1];
x[i+1]=t;
}
}
}
public static void main(String args[]) {
int[] xr={99,11,33,66,55,44,77,33,22,88,0};
System.out.println(Arrays.toString(xr));
sortArray(xr);
System.out.println(Arrays.toString(xr));
}
}

//////////////////////////////////////////////////////////////////////
Q339 Step IV
============
package example;

import java.util.Arrays;
class Example {
public static void sortArray(int[] x){
for(int i=0; i<x.length-1;i++){
if(x[i]>x[i+1]){
int t=x[i];
x[i]=x[i+1];
x[i+1]=t;
}
}
}
public static void main(String args[]) {
int[] xr={99,11,33,66,55,44,77,33,22,88,0};
System.out.println(Arrays.toString(xr));
sortArray(xr);
System.out.println(Arrays.toString(xr));
}
}

//////////////////////////////////////////////////////////////////////
Q340 Step V
===========
package example;

import java.util.Arrays;
class Example {
public static void sortArray(int[] x){
for(int i=0; i<x.length-1;i++){
if(x[i]>x[i+1]){
int t=x[i];
x[i]=x[i+1];
x[i+1]=t;
}
}
for(int i=0; i<x.length-1;i++){
if(x[i]>x[i+1]){
int t=x[i];
x[i]=x[i+1];
x[i+1]=t;
}
}
for(int i=0; i<x.length-1;i++){
if(x[i]>x[i+1]){
int t=x[i];
x[i]=x[i+1];
x[i+1]=t;
}
}
for(int i=0; i<x.length-1;i++){
if(x[i]>x[i+1]){
int t=x[i];
x[i]=x[i+1];
x[i+1]=t;
}
}
}
public static void main(String args[]) {
int[] xr={99,11,33,66,55,44,77,33,22,88,0};
System.out.println(Arrays.toString(xr));
sortArray(xr);
System.out.println(Arrays.toString(xr));
}
}

//////////////////////////////////////////////////////////////////////
Q341 Step VI
============
package example;

import java.util.Arrays;
class Example {
public static void sortArray(int[] x){
for(int j=0;j<x.length-1; j++){
for(int i=0; i<x.length-1;i++){
if(x[i]>x[i+1]){
int t=x[i];
x[i]=x[i+1];
x[i+1]=t;
}
}
}
}
public static void main(String args[]) {
int[] xr={99,11,33,66,55,44,77,33,22,88,0};
System.out.println(Arrays.toString(xr));
sortArray(xr);
System.out.println(Arrays.toString(xr));
}
}

//////////////////////////////////////////////////////////////////////
Q342 Step VII
=============
package example;

import java.util.Arrays;
class Example {
public static void sortArray(int[] x){
for(int j=0;j<x.length-1; j++){
for(int i=0; i<x.length-1;i++){
if(x[i]>x[i+1]){
int t=x[i];
x[i]=x[i+1];
x[i+1]=t;
}
System.out.println(Arrays.toString(x));
try{Thread.sleep(500);}catch(InterruptedException ex){}
}
}
}
public static void main(String args[]) {
int[] xr={99,11,33,66,55,44,77,33,22,88,0};
System.out.println(Arrays.toString(xr));
sortArray(xr);
System.out.println(Arrays.toString(xr));
}
}

//////////////////////////////////////////////////////////////////////
Q343 Step VIII
==============

//////////////////////////////////////////////////////////////////////
Array Sorting (Selection Sort)
//////////////////////////////////////////////////////////////////////
Q344 Step I
===========
package example;

import java.util.Arrays;
class Example {
public static void sortArray(int[] x){
int max=x[0];
int index=0;
for (int j = 0; j < x.length; j++) {
if(x[j]>max){
max=x[j];
index=j;
}
}
x[index] =x[x.length-1];
x[x.length-1]=max;
}
public static void main(String args[]) {
int[] xr={30,11,99,66,55,44,77,33,22,88,0};
System.out.println(Arrays.toString(xr));
sortArray(xr);
System.out.println(Arrays.toString(xr));
}
}

//////////////////////////////////////////////////////////////////////
Q345 Step II
============
package example;

import java.util.Arrays;

class Example {

public static void sortArray(int[] x) {


int max = x[0];
int index = 0;
for (int j = 0; j < x.length; j++) {
if (x[j] > max) {
max = x[j];
index = j;
}
}
x[index] = x[x.length - 1];
x[x.length - 1] = max;

//---------------------------------------------
max = x[0];
index = 0;
for (int j = 0; j < x.length - 1; j++) {
if (x[j] > max) {
max = x[j];
index = j;
}
}
x[index] = x[x.length - 2];
x[x.length - 2] = max;

//---------------------------------------------
max = x[0];
index = 0;
for (int j = 0; j < x.length - 2; j++) {
if (x[j] > max) {
max = x[j];
index = j;
}
}
x[index] = x[x.length - 3];
x[x.length - 3] = max;
}

public static void main(String args[]) {


int[] xr = {30, 11, 99, 66, 55, 44, 77, 33, 22, 88, 0};
System.out.println(Arrays.toString(xr));
sortArray(xr);
System.out.println(Arrays.toString(xr));
}
}

//////////////////////////////////////////////////////////////////////
Q346 Step III
============

//////////////////////////////////////////////////////////////////////
Two D Arrays
//////////////////////////////////////////////////////////////////////
Q347
package example;
import java.util.*;
class Example{
public static void main(String args[]){
Scanner input=new Scanner(System.in);
int[] st0=new int[4];
int[] st1=new int[4];
int[] st2=new int[4];
int[] st3=new int[4];
int[] st4=new int[4];
int[] st5=new int[4];
int[] st6=new int[4];
int[] st7=new int[4];
int[] st8=new int[4];
int[] st9=new int[4];

System.out.println("Input marks for student 1 ...");


for(int j=0;j<4;j++){
System.out.print("\tSubject "+(j+1)+" : ");
st0[j]=input.nextInt();
}

System.out.println("Input marks for student 2 ...");


for(int j=0;j<4;j++){
System.out.print("\tSubject "+(j+1)+" : ");
st1[j]=input.nextInt();
}

System.out.println("Input marks for student 3 ...");


for(int j=0;j<4;j++){
System.out.print("\tSubject "+(j+1)+" : ");
st2[j]=input.nextInt();
}

System.out.println("Input marks for student 4 ...");


for(int j=0;j<4;j++){
System.out.print("\tSubject "+(j+1)+" : ");
st4[j]=input.nextInt();
}

}
}

//////////////////////////////////////////////////////////////////////
Q348 From Q347 (Using iterations)
=================================

import java.util.*;

class Example {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int[] st0 = new int[4];
int[] st1 = new int[4];
int[] st2 = new int[4];
int[] st3 = new int[4];
int[] st4 = new int[4];
int[] st5 = new int[4];
int[] st6 = new int[4];
int[] st7 = new int[4];
int[] st8 = new int[4];
int[] st9 = new int[4];

for (int i = 0; i < 10; i++) {


System.out.println("Input marks for student " + (i + 1) + "...");
for (int j = 0; j < 4; j++) {
System.out.print("\tSubject " + (j + 1) + " : ");
sti[j] = input.nextInt(); //IllegaL
}
}
/*
System.out.println("Input marks for student 2 ...");
for (int j = 0; j < 4; j++) {
System.out.print("\tSubject " + (j + 1) + " : ");
st1[j] = input.nextInt();
}

System.out.println("Input marks for student 3 ...");


for (int j = 0; j < 4; j++) {
System.out.print("\tSubject " + (j + 1) + " : ");
st2[j] = input.nextInt();
}

System.out.println("Input marks for student 4 ...");


for (int j = 0; j < 4; j++) {
System.out.print("\tSubject " + (j + 1) + " : ");
st4[j] = input.nextInt();
}
*/
}
}

//////////////////////////////////////////////////////////////////////
Q349 From Q348 Using two dimensional arrays
===========================================
package example;
import java.util.*;

class Example {

public static void main(String args[]) {


Scanner input = new Scanner(System.in);
/*
int[] st0 = new int[4];
int[] st1 = new int[4];
int[] st2 = new int[4];
int[] st3 = new int[4];
int[] st4 = new int[4];
int[] st5 = new int[4];
int[] st6 = new int[4];
int[] st7 = new int[4];
int[] st8 = new int[4];
int[] st9 = new int[4];*/
int[][] st=new int[10][4];

for (int i = 0; i < 10; i++) {


System.out.println("Input marks for student " + (i + 1) + "...");
for (int j = 0; j < 4; j++) {
System.out.print("\tSubject " + (j + 1) + " : ");
st[i][j] = input.nextInt();
}
}
}
}

//////////////////////////////////////////////////////////////////////
Q350 Memory Representaion a two dimensional array
=================================================
package example;
class Example {
public static void main(String args[]) {
int[] xr;
xr=new int[4];
System.out.println(xr);
System.out.println(xr[0]);
//-----------------------------
int[][]br;
br=new int[4][3];
System.out.println(br);
System.out.println(br[0]+" "+br[1]+" "+br[2]);
System.out.println(br.length); //4
System.out.println(br[0].length); //3
System.out.println(br[1].length); //3

System.out.println(br[0][0]+" "+br[0][1]+" "+br[0][2]);


System.out.println(br[1][0]+" "+br[1][1]+" "+br[1][2]);
System.out.println(br[2][0]+" "+br[2][1]+" "+br[2][2]);
System.out.println(br[3][0]+" "+br[3][1]+" "+br[3][2]);
}
}

//////////////////////////////////////////////////////////////////////
Q351
package example;
class Example {
public static void main(String args[]) {

int[][]br=new int[4][];
System.out.println(br);
System.out.println(br[0]+" "+br[1]+" "+br[2]+" "+br[3]);

//
br[0]=new int[3];
br[1]=new int[5];
br[2]=new int[7];
br[3]=new int[2];
System.out.println(br[0]+" "+br[1]+" "+br[2]+" "+br[3]);

System.out.println("br[0] : "+br[0].length); //3


System.out.println("br[1] : "+br[1].length); //5
System.out.println("br[2] : "+br[2].length); //7
}
}

//////////////////////////////////////////////////////////////////////
Q352 Exercise
=============
package example;
import java.util.Scanner;

class Example {
public static void main(String args[]) {
Scanner input=new Scanner(System.in);
System.out.print("Input no of students : ");
final int N=input.nextInt();
System.out.print("Input no of subjects : ");
final int S=input.nextInt();
int[][] marks=new int[N][S];

//Read marks (Read random number 0 to 100)

}
}

//////////////////////////////////////////////////////////////////////
Q353 from exercis 352
=====================
package example;

import java.util.Scanner;

class Example {

public static void main(String args[]) {


Scanner input = new Scanner(System.in);
System.out.print("Input no of students : ");
final int N = input.nextInt();
System.out.print("Input no of subjects : ");
final int S = input.nextInt();
int[][] marks = new int[N][S];

//Read marks (Read random number 0 to 100)


Random r=new Random();
/*for (int j = 0; j < S; j++) {
marks[0][j]=r.nextInt(101);
}
for (int j = 0; j < S; j++) {
marks[1][j]=r.nextInt(101);
}
for (int j = 0; j < S; j++) {
marks[2][j]=r.nextInt(101);
} */
for (int i = 0; i < N; i++) {
for (int j = 0; j < S; j++) {
marks[i][j] = r.nextInt(101);
}
}

//Print marks

}
}

//////////////////////////////////////////////////////////////////////
Q354 From Q353
==============
package example;

import java.util.Random;
import java.util.Scanner;

class Example {

public static void main(String args[]) {


Scanner input = new Scanner(System.in);
System.out.print("Input no of students : ");
final int N = input.nextInt();
System.out.print("Input no of subjects : ");
final int S = input.nextInt();
int[][] marks = new int[N][S];

//Read marks (Read random number 0 to 100)


Random r = new Random();
for (int i = 0; i < N; i++) {
for (int j = 0; j < S; j++) {
marks[i][j] = r.nextInt(101);
}
}

//Print marks
for (int i = 0; i < N; i++) {
for (int j = 0; j < S; j++) {
System.out.print(marks[i][j] + "\t");
}
System.out.println("");
}

}
}

//////////////////////////////////////////////////////////////////////
Q355 Exercise
=============
package example;

import java.util.Random;
import java.util.Scanner;

class Example {

public static void main(String args[]) {


Scanner input = new Scanner(System.in);
System.out.print("Input no of students : ");
final int N = input.nextInt();
int[][] marks = new int[N][];
for (int i = 0; i < N; i++) {
System.out.print("Input no of subjects for student "+(i+1)+" : ");
int S = input.nextInt();
marks[i]=new int[S];
}

//Read marks (Read random number 0 to 100)


Random r = new Random();
for (int i = 0; i < N; i++) {
for (int j = 0; j < marks[i].length; j++) {
marks[i][j] = r.nextInt(101);
}
}

//Print marks
for (int i = 0; i < N; i++) {
for (int j = 0; j < marks[i].length; j++) {
System.out.print(marks[i][j] + "\t");
}
System.out.println("");
}

}
}
//////////////////////////////////////////////////////////////////////
Q356

//////////////////////////////////////////////////////////////////////
Q357

//////////////////////////////////////////////////////////////////////
Q358

//////////////////////////////////////////////////////////////////////
Q359

//////////////////////////////////////////////////////////////////////
Q360

You might also like