0% found this document useful (0 votes)
77 views

Laborator 2

The document discusses various Java code examples involving variables, expressions, and instructions. It includes code snippets to demonstrate the values of variables after each line of execution. It also contains questions about control flow, loops, conditional statements, and operators in Java.

Uploaded by

isis3489
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Laborator 2

The document discusses various Java code examples involving variables, expressions, and instructions. It includes code snippets to demonstrate the values of variables after each line of execution. It also contains questions about control flow, loops, conditional statements, and operators in Java.

Uploaded by

isis3489
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Laborator 2.

Elemente de baza: variabile, expresii, instructiuni


1. Consideram urmatoarea secventa de cod:
1. int j=1; int k=0;
2. boolean b=true;
3. double d=3.141;
4. k=j++;
5. k=j- -;
6. k=++j;
7. k=--j;
8. k=+(-1);
9. b=!true;
10. k=(int) d;
11. j=10;
12. k=k+j*j-k/j%k;
13. k=j<<3;
14. k=j>>4;
15. k=j>>>5;
16. b=j<k;
17. b=j>k;
18. b=j<=k;
19. b=j!=k;
20. b=j==k;
21. k=j|k;
22. k=j&k;
23. k=j^k;
24. b=(j<0)&&((j%10)==3);
25. k=(j<0)?-j:j;
26. k+=j;
27. k-=j;
28. j^=j;
Care sunt valorile variabilelor dupa executarea fiecarei linii?
2. Care din urmatoarele fragmente de cod vor compila cu succes si vor afisa Egal la
executie ?
a) int x=100; float y=100.0F;
if (x==y) {System.out.println(“Egal”);}
b) int x=100; float y=100.0F; x=y;
if (x==y) {System.out.println(“Egal”);}
c) int x=100; long y=100L; x=(int)y;
if (x==y) {System.out.println(“Egal”);}

3. Sa scrie folosind editorul Notepad urmatorul program:


public class BunaZiua{
public static void main(String[] args){
System.out.println(„Buna ziua tuturor”);
}
}
a) Sa se salveze programul in fisierul BunaZiua.java

1
b) Sa se compileze programul din linia de comanda
c) Sa se execute programul

4. Sa se compileze urmatorul program:


public class Erori {
public static void main (String[] args){
int a;
a=5;
int b;
int c;
c=a/b;
System.out.println( c);
}
}
Daca sunt erori, sa se corecteze.

5. Sa se completeze urmatorul program pentru a calcula si afisa media aritmetica a 3 numere


reale.
public class Medie {
public static void main(String[] args) {
double a, b, c;
a=2.2;
b=3.3;
c=10;
//de completat
}
}
6. Se considera urmatoarea secventa de program:
1. int n=0, x=1;
2. n=x++ + x++;
3. n=n++ - x++;
4. n=x-- - x++;
Care sunt valorile lui n si x dupa executarea fiecarei instructiuni?
7. Se considera urmatoarea secventa de program:
1. int n=0, x=1;
2. n=++x + x++;
3. n=n++ -(++ x);
4. n=x-- - (--x);
Care sunt valorile lui n si x dupa executarea fiecarei instructiuni?
8. Se considera urmatoarea secventa de cod:
1. for (int i=0, j=0 ; i<4 && j<3 ; ++i, j++){
2. do{
3. if (i+j<2) break;
4. else {i++; continue;}
5. }while (i<2);
6.
7. }//for
Ce valori au variabilele contor i si j pe linia 6?

2
a) i=0; j=0; d) i=2; j=2;
b) i=1; j=1; e) i=3; j=2;
c) i=2; j=1; f) i=4; j=2
9. Se considera urmatoarea secventa de program:
1. for (int i=0 ; i<2 ; i++){
2. for (int j=0 ; j<3 ; j++){
3. if (i= =j) continue;
4.
5. }
6. }
Care sunt valorile variabilelor i si j pe linia 4?
a) i=0; j=0
b) i=0; j=1
c) i=0; j=2
d) i=1; j=0
e) i=1; j=1
f) i=1; j=2
10. Se considera urmatoarea secventa de cod:
char c=‟a‟;
while (c<‟c‟){
switch(c){
case „c „: c++ ; break ;
case „a‟ : c++ ;
case „b‟ : c++ ;
}//switch
}//while
Ce valoare are variabila c dupǎ executarea secvenţei de cod?
c=‟a‟ ; b) c=‟b‟ ; c) c=‟c‟ ;
11. Sa se scrie o secventǎ de program Java care calculeaza produsul numerelor intregi impare
de la 1 la 15.
12. Să se scrie o secventǎ de program Java care traduce numerele de la 1 la 12 în numele
lunilor corespunzătoare: Ianuarie, Februarie, …, Decembrie.

You might also like